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

declare(strict_types=1);

namespace BookiFlex\Extensions\Cart\Api\Transformer;

use BookiFlex\Core\Service\Aggregator\SummaryAggregator;
use BookiFlex\Extensions\Cart\Model\Cart;

defined( 'ABSPATH' ) || exit;

/**
 * Transform cart totals data for API responses
 */
class CartTotalsTransformer
{
    /**
     * Transform raw totals data to API response format
     *
     * @param Cart  $cart   Cart instance
     * @param array $totals Raw totals data from CartService::calculateTotal()
     * @return array Transformed totals matching OpenAPI Totals schema
     */
    public function transform(Cart $cart, array $totals): array
    {
        if ($cart->isEmpty()) {
            return $this->getEmptyTotals($cart->getCurrency());
        }

        $aggregator = new SummaryAggregator($cart->getCurrency());

        foreach ($totals as $type => $items) {
            foreach ($items as $summary) {
                if ($type === 'extra') {
                    $aggregator->addSummary(
                        $summary['accommodation']['cashier'],
                        'extra',
                        'item',
                        $summary['accommodation']['context'],
                    );
                } else {
                    // Add main accommodation summary
                    $aggregator->addSummary(
                        $summary['accommodation']['cashier'],
                        'cart',
                        'total',
                        $summary['accommodation']['context'],
                    );

                    // Add daily breakdowns
                    foreach ($summary['daily'] as $date => $cashier) {
                        $aggregator->addSummary(
                            $cashier,
                            'accommodation',
                            'daily',
                            [
                                'date' => $date,
                                'id' => $summary['accommodation']['context']['id']
                            ]
                        );
                    }
                }
            }
        }

        return $aggregator->aggregate()->toArray();
    }

    /**
     * Get empty totals structure
     *
     * @param string $currency Currency code
     * @return array Empty totals matching OpenAPI schema
     */
    private function getEmptyTotals(string $currency): array
    {
        return [
            'currency' => $currency,
            'subtotal' => 0,
            'total' => 0,
            'summary' => [
                'base' => 0,
                'taxes' => 0,
                'charges' => 0,
                'discounts' => 0,
            ],
            'modifiers' => [],
            'breakdown' => [],
            'metadata' => [],
        ];
    }
}
