<?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\ORM\Typecast;

use BookiFlex\Core\Model\Reservation\CancellationActor;
use BookiFlex\Core\Model\Reservation\ReservationStatus;
use Cycle\ORM\Parser\CastableInterface;
use Cycle\ORM\Parser\UncastableInterface;

defined( 'ABSPATH' ) || exit;

class ReservationTypecast implements CastableInterface, UncastableInterface
{
    public function setRules(array $rules): array
    {
        // Can set rules here if needed
        return $rules;
    }

    public function cast(array $data): array
    {
        if (isset($data['status']) && is_string($data['status'])) {
            $data['status'] = new ReservationStatus($data['status']);
        }

        if (isset($data['cancelledBy']) && is_string($data['cancelledBy'])) {
            $data['cancelledBy'] = CancellationActor::tryFrom($data['cancelledBy']);
        }

        return $data;
    }

    public function uncast(array $data): array
    {
        if (isset($data['status']) && $data['status'] instanceof ReservationStatus) {
            $data['status'] = (string) $data['status'];
        }

        if (isset($data['cancelledBy']) && $data['cancelledBy'] instanceof CancellationActor) {
            $data['cancelledBy'] = $data['cancelledBy']->value;
        }

        return $data;
    }
}
