<?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\Model\AccommodationType\Scheme;

defined( 'ABSPATH' ) || exit;

/**
 * Complete placement scheme for a group
 */
final class AllocationScheme
{
    /** @var Allocation[] */
    private array $allocations = [];

    public function addAllocation(Allocation $allocation): void
    {
        $this->allocations[] = $allocation;
    }

    public function getAllocations(): array
    {
        return $this->allocations;
    }

    public function toArray(): array
    {
        $result = [];

        foreach ($this->allocations as $placement) {
            $typeKey = $placement->getGuestType()->getTypeKey();
            $locationKey = $placement->getLocation()->toString();
            $variantKey = $placement->getGuestType()->getVariantKey();

            $key = "{$typeKey}On" . ucfirst($locationKey) . "Place";
            if (!isset($result[$key])) {
                $result[$key] = [];
            }

            $result[$key][$variantKey] = ($result[$key][$variantKey] ?? 0) + $placement->getQuantity();
        }

        return $result;
    }
}
