<?php

namespace App\Dto\Input;

use ApiPlatform\Metadata\ApiProperty;
use App\Dto\Output\RemoteCalendarOutput;
use App\Entity\RemoteCalendarRule;
use App\Validator\Constraints\RemoteCalendarRuleUnique;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;


#[RemoteCalendarRuleUnique]
final class RemoteCalendarRuleInput
{
    #[Assert\NotNull()]
    #[Groups(['remote-calendar-rule:write'])]
    #[ApiProperty(description: 'The remote calendar of the remote calendar rule')]
    public ?RemoteCalendarOutput $remoteCalendar = null;

    #[Groups(['remote-calendar-rule:write'])]
    #[ApiProperty(description: 'The busy week days of the remote calendar rule', example: [1, 2, 3])]
    public ?array $busyWeekDays = null;

    #[Groups(['remote-calendar-rule:write'])]
    #[ApiProperty(description: 'The busy from hour of the remote calendar rule', example: "2021-10-10T10:00:00+00:00")]
    public ?string $busyFromHour =  null;

    #[Groups(['remote-calendar-rule:write'])]
    #[ApiProperty(description: 'The busy to hour of the remote calendar rule', example: "2021-10-10T10:00:00+00:00")]
    public ?string $busyToHour = null;

    #[Groups(['remote-calendar-rule:write'])]
    #[ApiProperty(description: 'The remote availability of the remote calendar rule', example: true)]
    public ?bool $isRemoteAvailable = false;

    #[Groups(['remote-calendar-rule:write'])]
    #[ApiProperty(description: 'The available from date of the remote calendar rule', example: "2021-10-10T10:00:00+00:00")]
    public ?\DateTimeInterface $availableFromDate = null;

    #[Groups(['remote-calendar-rule:write'])]
    #[ApiProperty(description: 'The available to date of the remote calendar rule', example: "2021-10-10T10:00:00+00:00")]
    public ?\DateTimeInterface $availableToDate = null;

    #[Groups(['remote-calendar-rule:write'])]
    #[ApiProperty(description: 'The reason of the remote calendar rule', example: "reason")]
    public ?string $availableReason = null;

    public function __construct(?RemoteCalendarRule $remoteCalendarRule = null)
    {
        if (!$remoteCalendarRule) {
            return;
        }

        $this->remoteCalendar = new RemoteCalendarOutput($remoteCalendarRule->getRemoteCalendar());
        $this->busyWeekDays = $remoteCalendarRule->getBusyWeekDays();
        $this->busyFromHour = $remoteCalendarRule->getBusyFromHour();
        $this->busyToHour = $remoteCalendarRule->getBusyToHour();
        $this->isRemoteAvailable = $remoteCalendarRule->isRemoteAvailable();
        $this->availableFromDate = $remoteCalendarRule->getAvailableFromDate();
        $this->availableToDate = $remoteCalendarRule->getAvailableToDate();
        $this->availableReason = $remoteCalendarRule->getAvailableReason();
    }

    /**
     * @throws \Exception
     */
    public function createOrUpdateEntity(?RemoteCalendarRule $remoteCalendarRule = null): RemoteCalendarRule
    {
        if (!$remoteCalendarRule) {
            $remoteCalendarRule = new RemoteCalendarRule();
        }

        $remoteCalendarRule->setRemoteCalendar($this->remoteCalendar->getEntity());
        $remoteCalendarRule->setBusyWeekDays($this->busyWeekDays);
        $remoteCalendarRule->setBusyFromHour($this->busyFromHour);
        $remoteCalendarRule->setBusyToHour($this->busyToHour);
        $remoteCalendarRule->setRemoteAvailable($this->isRemoteAvailable);
        $remoteCalendarRule->setAvailableFromDate($this->availableFromDate);
        $remoteCalendarRule->setAvailableToDate($this->availableToDate);
        $remoteCalendarRule->setAvailableReason($this->availableReason);

        return $remoteCalendarRule;
    }
}