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

use BookiFlex\Core\Model\AccommodationType\Accommodation\AccommodationType;
use BookiFlex\Core\Repository\AccommodationTypeRepository;

defined( 'ABSPATH' ) || exit;

/**
 * Finds the maximum guest capacity across all active Accommodation Types.
 */
final class AccommodationCapacityFinder
{
    private const DEFAULT_CAPACITY = 5;

    public function __construct(
        private readonly AccommodationTypeRepository $accommodationTypeRepository
    ) {
    }

    public function findMaxCapacity(): int
    {
        $max = 0;

        /** @var AccommodationType $accommodationType */
        foreach ($this->accommodationTypeRepository->findAll() as $accommodationType) {
            if (!$accommodationType->isActive()) {
                continue;
            }

            $max = max($max, $accommodationType->getCapacity()->getTotalCapacity());
        }

        return $max > 0 ? $max : self::DEFAULT_CAPACITY;
    }
}
