<?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\AccommodationType\Accommodation\AccommodationCapacity;
use BookiFlex\Core\Model\AccommodationType\BookingUnitTypeInterface;
use BookiFlex\Core\Model\AccommodationType\Guest\AdultGuest;
use BookiFlex\Core\Model\AccommodationType\Placement\PlacementOption;
use BookiFlex\Core\Model\AccommodationType\Placement\PlacementRules;
use BookiFlex\Core\ValueObject\PlacementLocation;
use Cycle\ORM\Parser\CastableInterface;
use Cycle\ORM\Parser\UncastableInterface;

defined( 'ABSPATH' ) || exit;

class AccommodationTypeTypecast implements CastableInterface, UncastableInterface
{
    public function setRules(array $rules): array
    {
        return $rules;
    }

    public function cast(array $data): array
    {
        // Create capacity
        $capacity = new AccommodationCapacity(
            $data['mainSleepingCapacity'] ?? 1,
            $data['additionalSleepingPlaces'] ?? 0
        );

        // Create placement rules
        $rules = new PlacementRules();

        // Add adult placement rules
        $allowedOccupancy = $data['mainSleepingAllowedOccupancy'] ?? [1];
        foreach ($allowedOccupancy as $occupancy) {
            $rules->addOption(new PlacementOption(
                new AdultGuest($occupancy),
                PlacementLocation::main(),
                true
            ));
        }

        $metadata = $this->extractMetadata($data);

        return [
            'id' => $data['id'] ?? null,
            'name' => $data['name'] ?? null,
            'accommodationCategory' => $data['accommodationCategory'] ?? null,
            'capacity' => $capacity,
            'rules' => $rules,
            'accommodationAvailableUnits' => $data['accommodationAvailableUnits'] ?? 0,
            'metadata' => $metadata,
        ];
    }

    public function uncast(array $data): array
    {
        return $data;
    }

    protected function extractMetadata(array $data): array
    {
        return [
            'active' => $data['active'] ?? false,
            'shortCode' => $data['shortCode'] ?? '',
            'description' => $data['description'] ?? '',
            'content' => $data['content'] ?? '',
            'livingSpace' => $data['livingSpace'] ?? '',
	        'roomsCount' => $data['roomsCount'] ?? 1,
            'amenities' => $data['amenities'] ?? '',
            'gallery' => $data['gallery'] ?? [],
            'thumbnail' => $data['thumbnail'] ?? [],
            'contactInfo' => [
                'address' => $data['address'] ?? '',
                'phone' => $data['phone'] ?? '',
                'email' => $data['email'] ?? '',
                'longitude' => $data['longitude'] ?? '',
                'latitude' => $data['latitude'] ?? '',
                'checkinInstructions' => $data['checkinInstructions'] ?? '',
            ],
        ];
    }
}
