<?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\Guest;

use BookiFlex\Core\ValueObject\AgeRange;

defined( 'ABSPATH' ) || exit;

/**
 * Child guest
 */
final class ChildGuest extends GuestType
{
    private int $age;
    private ?AgeRange $ageRange;

    public function __construct(int $age, ?AgeRange $ageRange = null)
    {
        if ($age < 0) {
            throw new \InvalidArgumentException("Age cannot be negative");
        }
        $this->age = $age;
        $this->ageRange = $ageRange;
    }

    public function getAge(): int
    {
        return $this->age;
    }

    public function getAgeRange(): ?AgeRange
    {
        return $this->ageRange;
    }

    public function fitsInRange(AgeRange $range): bool
    {
        return $range->contains($this->age);
    }

    public function getTypeKey(): string
    {
        return 'child';
    }

    public function getVariantKey(): string
    {
        return $this->ageRange ? $this->ageRange->getKey() : "age_{$this->age}";
    }

    public function matches(GuestType $other): bool
    {
        if (!$other instanceof self) {
            return false;
        }

        if ($this->ageRange && $other->ageRange) {
            return $this->ageRange->equals($other->ageRange);
        }

        return $this->age === $other->age;
    }
}
