<?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\OfferFinder\DTO;

use BookiFlex\Core\Model\DateUnit\RestrictionRulesConfigurator;
use BookiFlex\Core\Provider\LocalTimeZone;
use DateTimeImmutable;

defined( 'ABSPATH' ) || exit;

class OfferRequest
{
    private DateTimeImmutable $checkInDate;

    private DateTimeImmutable $checkOutDate;

    private ?int $adults = null;

    private array $children = [];

    private array $ratePlans = [];

    private array $accommodationTypes = [];

    private array $meta = [];

    /**
     * @var RestrictionRulesConfigurator|null
     */
    private ?RestrictionRulesConfigurator $restrictionRules;


    public function __construct($data, ?RestrictionRulesConfigurator $restrictionRules = null)
    {
        if (isset($data['checkInDate'])) {
            $this->checkInDate = DateTimeImmutable::createFromFormat('Y-m-d', $data['checkInDate'], LocalTimeZone::get());
        }

        if (isset($data['checkOutDate'])) {
            $this->checkOutDate = DateTimeImmutable::createFromFormat('Y-m-d', $data['checkOutDate'], LocalTimeZone::get());
        }

        if (isset($data['ratePlans'])) {
            $this->ratePlans = $data['ratePlans'];
        }

        if (isset($data['accommodationTypes'])) {
            $this->accommodationTypes = $data['accommodationTypes'];
        }

        if (isset($data['adults']) && is_int($data['adults'])) {
            $this->adults = $data['adults'];
        }

        if (isset($data['children'])) {
            $this->children = $data['children'];
        }

        if (isset($data['meta'])) {
            $this->meta = $data['meta'];
        }

        $this->restrictionRules = $restrictionRules;
    }

    /**
     * @return DateTimeImmutable
     */
    public function getCheckInDate(): DateTimeImmutable
    {
        return $this->checkInDate;
    }

    /**
     * @return DateTimeImmutable
     */
    public function getCheckOutDate(): DateTimeImmutable
    {
        return $this->checkOutDate;
    }

    /**
     * @return int|null
     */
    public function getAdults(): ?int
    {
        return $this->adults;
    }

    /**
     * @return int[]
     */
    public function getChildren(): array
    {
        return $this->children;
    }

    public function getRatePlans(): array
    {
        return $this->ratePlans;
    }

    /**
     * @return int[]
     */
    public function getAccommodationTypes(): array
    {
        return $this->accommodationTypes;
    }

    /**
     * @return RestrictionRulesConfigurator|null
     */
    public function getRestrictionRules(): ?RestrictionRulesConfigurator
    {
        return $this->restrictionRules;
    }

    /**
     * @return array
     */
    public function getMeta(): array
    {
        return $this->meta;
    }

    public function __toString()
    {
        $meta = '';

        foreach ($this->getMeta() as $k => $v) {
            $meta .= $k . $v;
        }

        return $this->getCheckOutDate()->format('Y-m-d')
            . $this->getAdults()
            . implode('-', $this->getChildren())
            . implode('-', $this->getAccommodationTypes())
            . $meta;
    }
}
