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

declare(strict_types=1);

namespace BookiFlex\Core\Api\Transformer;

use BookiFlex\Core\Contract\Payment\PaymentAllocationInterface;
use BookiFlex\Core\Model\Reservation\Reservation;
use Brick\Math\Exception\MathException;

defined( 'ABSPATH' ) || exit;

class PublicReservationGroupTransformer
{
    /**
     * @param Reservation[] $reservations
     * @param array $ratePlanNames
     * @param array $accommodationTypes
     * @param PaymentAllocationInterface[] $paymentAllocations
     * @return array
     * @throws MathException
     */
    public function transform(
        array $reservations,
        array $ratePlanNames,
        array $accommodationTypes,
        array $paymentAllocations,
    ): array {
        $transformer = new ReservationGroupTransformer();
        $result = $transformer->transform($reservations, $ratePlanNames, $accommodationTypes, $paymentAllocations);

        return $this->clear($result);
    }

    protected function clear(array $data): array
    {
        unset($data['group']);
        unset($data['totals']['breakdown']);
        unset($data['totals']['metadata']);
        unset($data['totals']['modifiers']);

        foreach ($data['reservations'] as $i => $reservation) {
            unset($data['reservations'][$i]['id']);
            unset($data['reservations'][$i]['specialRequest']);
            unset($data['reservations'][$i]['customer']);

            unset($data['reservations'][$i]['payment']['transactions']);
            unset($data['reservations'][$i]['payment']['method']['id']);
            unset($data['reservations'][$i]['payment']['method']['gateway']);

            unset($data['reservations'][$i]['cancellation']['code']);
            unset($data['reservations'][$i]['cancellation']['url']);

            // Clean penalties (keep only amount + description)
            foreach ($data['reservations'][$i]['cancellation']['penalties'] as $ip => $penalty) {
                unset($data['reservations'][$i]['cancellation']['penalties'][$ip]['id']);
                unset($data['reservations'][$i]['cancellation']['penalties'][$ip]['status']);
                unset($data['reservations'][$i]['cancellation']['penalties'][$ip]['comment']);
                unset($data['reservations'][$i]['cancellation']['penalties'][$ip]['appliedAt']);
                // Keep: amount, description
            }

            // Clean estimatedPenalties (keep only amount + description)
            foreach ($data['reservations'][$i]['cancellation']['estimatedPenalties'] as $ip => $penalty) {
                unset($data['reservations'][$i]['cancellation']['estimatedPenalties'][$ip]['id']);
                unset($data['reservations'][$i]['cancellation']['estimatedPenalties'][$ip]['status']);
                unset($data['reservations'][$i]['cancellation']['estimatedPenalties'][$ip]['comment']);
                unset($data['reservations'][$i]['cancellation']['estimatedPenalties'][$ip]['appliedAt']);
                // Keep: amount, description
            }
        }

        return $data;
    }
}
