<?php

namespace App\Dto\Input;

use App\Dto\Output\CommandTaskOutput;
use App\Dto\Output\CommandTaskScheduleOutput;
use App\Entity\CommandTaskSchedule;
use Symfony\Component\Serializer\Annotation\Groups;

final class CommandTaskScheduleInput
{
    #[Groups(['command-task-schedule:write'])]
    public ?CommandTaskOutput $commandTask = null;

    #[Groups(['command-task-schedule:write'])]
    public ?string $recurrenceType = null;

    #[Groups(['command-task-schedule:write'])]
    public ?\DateTimeInterface $executionDate = null;

    #[Groups(['command-task-schedule:write'])]
    public ?\DateTimeInterface $executionTime = null;

    #[Groups(['command-task-schedule:write'])]
    public ?array $recurrenceDetails = null;

    #[Groups(['command-task-schedule:write'])]
    public ?bool $enabled = null;

    public function __construct(?CommandTaskSchedule $commandTaskSchedule = null)
    {
        if (!$commandTaskSchedule) {
            return;
        }

        $this->commandTask = new CommandTaskOutput($commandTaskSchedule->getCommandTask());
        $this->recurrenceType = $commandTaskSchedule->getRecurrenceType();
        $this->executionDate = $commandTaskSchedule->getExecutionDate();
        $this->executionTime = $commandTaskSchedule->getExecutionTime();
        $this->recurrenceDetails = $commandTaskSchedule->getRecurrenceDetails();
        $this->enabled = $commandTaskSchedule->isEnabled();
    }

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

        if ($this->commandTask) {
            $commandTaskSchedule->setCommandTask($this->commandTask->getEntity());
        }
        $commandTaskSchedule->setRecurrenceType($this->recurrenceType);
        $commandTaskSchedule->setExecutionTime($this->executionTime);
        $commandTaskSchedule->setExecutionDate($this->executionDate);
        $commandTaskSchedule->setRecurrenceDetails($this->recurrenceDetails);
        $commandTaskSchedule->setEnabled($this->enabled);

        return $commandTaskSchedule;
    }
}