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

defined( 'ABSPATH' ) || exit;

/**
 * Age range for children
 */
final class AgeRange
{
    private int $minAge;
    private int $maxAge;

    public function __construct(int $minAge, int $maxAge)
    {
        if ($minAge < 0 || $maxAge < 0 || $minAge > $maxAge) {
            throw new \InvalidArgumentException(
                sprintf(
                    'Invalid age range: %1$d-%2$d',
                    $minAge,
                    $maxAge
                )
            );
        }

        $this->minAge = $minAge;
        $this->maxAge = $maxAge;
    }

    public function contains(int $age): bool
    {
        return $age >= $this->minAge && $age <= $this->maxAge;
    }

    public function getMinAge(): int
    {
        return $this->minAge;
    }

    public function getMaxAge(): int
    {
        return $this->maxAge;
    }

    public function getKey(): string
    {
        return "{$this->minAge}x{$this->maxAge}";
    }

    public static function fromKey(string $key): self
    {
        $parts = explode('x', $key);
        if (count($parts) !== 2) {
            throw new \InvalidArgumentException(
                sprintf(
                    'Invalid age range key: %s',
                    $key
                )
            );
        }

        return new self((int)$parts[0], (int)$parts[1]);
    }

    public function equals(AgeRange $other): bool
    {
        return $this->minAge === $other->minAge && $this->maxAge === $other->maxAge;
    }
}
