<?php
/**
 * BookiFlex Payment Extension
 *
 * @package    BookiFlex
 * @subpackage Payment
 * @author     BookiFlex
 * @copyright  Copyright (c) 2025 BookiFlex
 * @license    GPL-2.0-or-later
 *
 * @link       https://bookiflex.com
 */

declare(strict_types=1);

namespace BookiFlex\Extensions\Payment\Admin\Transformer;

use BookiFlex\Extensions\Payment\Model\Payment;

defined( 'ABSPATH' ) || exit;

/**
 * Transformer for converting Payment domain models to associative arrays for WP_List_Table.
 *
 * Transforms enriched payment data (domain models + allocations) into flat arrays
 * suitable for WordPress table rendering. Handles null safety, date formatting,
 * and allocation structuring.
 *
 * @package BookiFlex\Extensions\Payment\Admin\Transformer
 */
class PaymentTableListTransformer
{
    /**
     * Date format for database timestamps.
     * Used for created_at and updated_at fields.
     */
    public const DATE_FORMAT = 'Y-m-d H:i:s';

    /**
     * Transform enriched payment data to WP_List_Table compatible arrays.
     *
     * Takes domain models and allocation data from service layer and converts
     * to flat associative arrays for table rendering.
     *
     * @param array $data Enriched data: ['payments' => Payment[], 'allocations' => array]
     * @return array Array of associative arrays suitable for WP_List_Table
     */
    public function transform(array $data): array
    {
        $payments = $data['payments'] ?? [];
        $allocations = $data['allocations'] ?? [];

        $rows = [];
        foreach ($payments as $payment) {
            $rows[] = $this->transformPayment($payment, $allocations);
        }

        return $rows;
    }

    /**
     * Transform single Payment entity to array.
     *
     * Converts Payment domain model to flat array with:
     * - Customer information (from relation)
     * - Payment data (raw values for sorting)
     * - Allocation data (structured for display)
     * - Timestamps (formatted for database)
     *
     * @param Payment $payment Payment domain model
     * @param array $allocations Allocation data grouped by payment_id
     * @return array Associative array for WP_List_Table
     */
    private function transformPayment(Payment $payment, array $allocations): array
    {
        $paymentId = $payment->getId();
        $customer = $payment->getClient();

        return [
            'id' => $paymentId,

            // Customer information (null-safe with fallbacks)
            'first_name' => $customer?->getFirstName() ?? '',
            'last_name' => $customer?->getLastName() ?? '',
            'email' => $customer?->getEmail() ?? '',
            'phone' => $customer?->getPhone() ?? '',

            // Payment data (raw values for sorting)
            'total_amount' => $payment->getTotalAmount(),
            'currency_code' => $payment->getCurrencyCode(),

            // Payment metadata
            'status' => (string) $payment->getStatus(),
            'gateway_name' => $payment->getGatewayName(),

            // Allocations (enriched data from service)
            'allocated' => $allocations[$paymentId] ?? [],

            // Timestamps (formatted for display methods)
            'created_at' => $payment->getCreatedAt()?->format(self::DATE_FORMAT) ?? '',
            'updated_at' => $payment->getUpdatedAt()?->format(self::DATE_FORMAT) ?? '',
        ];
    }
}
