<?php

declare(strict_types=1);

/*
 * This file is part of the Ferienpass package.
 *
 * (c) Richard Henkenjohann <richard@ferienpass.online>
 *
 * For more information visit the project website <https://ferienpass.online>
 * or the documentation under <https://docs.ferienpass.online>.
 */

namespace Ferienpass\CoreBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Ferienpass\CoreBundle\Entity\Participant\ParticipantInterface;
use Symfony\Bridge\Doctrine\Types\UuidType;
use Symfony\Component\Uid\Uuid;
use Symfony\Component\Workflow\Transition;

#[ORM\Entity]
#[ORM\Index(columns: ['participant_id', 'createdAt'])]
class ParticipantLog implements \Stringable
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer', options: ['unsigned' => true])]
    private ?int $id = null;

    #[ORM\Column(type: UuidType::NAME)]
    private Uuid $uuid;

    #[ORM\Column(type: 'datetime_immutable', options: ['default' => 'CURRENT_TIMESTAMP'])]
    private \DateTimeInterface $createdAt;

    #[ORM\Column(type: 'string', length: 32, nullable: true)]
    private ?string $applicationSystem = null;

    #[ORM\Column(type: 'string', length: 32, nullable: true)]
    private ?string $transitionName = null;
    #[ORM\Column(type: 'string', length: 32, nullable: true)]
    private ?string $transitionFrom = null;
    #[ORM\Column(type: 'string', length: 32, nullable: true)]
    private ?string $transitionTo = null;

    #[ORM\Column(type: 'string', length: 32, nullable: true)]
    private ?string $decisionReason = null;

    public function __construct(#[ORM\ManyToOne(targetEntity: ParticipantInterface::class, inversedBy: 'activity')]
        #[ORM\JoinColumn(name: 'participant_id', referencedColumnName: 'id', onDelete: 'SET NULL')]
        private ?ParticipantInterface $participant, #[ORM\ManyToOne(targetEntity: User::class)]
        #[ORM\JoinColumn(name: 'user_id', referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
        private ?User $createdBy = null, #[ORM\ManyToOne(targetEntity: Attendance::class, inversedBy: 'activity')]
        #[ORM\JoinColumn(name: 'attendance_id', referencedColumnName: 'id', onDelete: 'CASCADE')]
        private ?Attendance $attendance = null, #[ORM\Column(type: 'string', nullable: true)]
        private ?string $comment = null, ?Transition $transition = null, ?string $applicationSystem = null, ?string $decisionReason = null)
    {
        $this->uuid = Uuid::v7();
        $this->applicationSystem = $applicationSystem;
        $this->decisionReason = $decisionReason;

        if ($this->attendance instanceof Attendance && $this->attendance->getParticipant() !== $this->participant) {
            throw new \InvalidArgumentException();
        }

        if ($transition instanceof Transition) {
            $this->transitionName = $transition->getName();
            $this->transitionFrom = $transition->getFroms()[0] ?? null;
            $this->transitionTo = $transition->getTos()[0] ?? null;
        }

        $this->createdAt = new \DateTimeImmutable();
    }

    public function __toString(): string
    {
        return (string) $this->id;
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getUuid(): Uuid
    {
        return $this->uuid;
    }

    public function getCreatedAt(): \DateTimeInterface
    {
        return $this->createdAt;
    }

    public function getParticipant(): ParticipantInterface
    {
        return $this->participant;
    }

    public function setParticipant(ParticipantInterface $participant): void
    {
        $this->participant = $participant;
    }

    public function getAttendance(): ?Attendance
    {
        return $this->attendance;
    }

    public function isComment(): bool
    {
        return null !== $this->comment;
    }

    public function isTransition(): bool
    {
        return null !== $this->transitionName;
    }

    public function getComment(): ?string
    {
        return $this->comment;
    }

    public function getTransitionName(): ?string
    {
        return $this->transitionName;
    }

    public function getTransitionTo(): ?string
    {
        return $this->transitionTo;
    }

    public function getTransition(): ?Transition
    {
        if (!$this->isTransition()) {
            return null;
        }

        $from = ('' === $this->transitionFrom || null === $this->transitionFrom) ? [] : (array) $this->transitionFrom;
        $to = ('' === $this->transitionTo || null === $this->transitionTo) ? [] : (array) $this->transitionTo;

        return new Transition($this->transitionName, $from, $to);
    }

    public function getApplicationSystem(): ?string
    {
        return $this->applicationSystem;
    }

    public function getDecisionReason(): ?string
    {
        return $this->decisionReason;
    }

    public function getCreatedBy(): ?User
    {
        return $this->createdBy;
    }

    public function setCreatedBy(?User $createdBy): void
    {
        $this->createdBy = $createdBy;
    }
}
