<?php

declare(strict_types=1);

/*
 * Chill is a software for social workers
 *
 * For the full copyright and license information, please view
 * the LICENSE file that was distributed with this source code.
 */

namespace Chill\TicketBundle\Entity;

use Chill\MainBundle\Doctrine\Model\TrackCreationInterface;
use Chill\MainBundle\Doctrine\Model\TrackCreationTrait;
use Chill\MainBundle\Doctrine\Model\TrackUpdateInterface;
use Chill\MainBundle\Doctrine\Model\TrackUpdateTrait;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Entity\UserGroup;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation as Serializer;

/**
 * Represents the history entity for addressees in the context of a ticketing system.
 *
 * Tracks information about addressees for a specific ticket, including user or group associability,
 * timestamps marking the start and end of the association, and the removal of assignments.
 * Implements mechanisms for tracking entity creation and updates.
 */
#[ORM\Entity()]
#[ORM\Table(name: 'addressee_history', schema: 'chill_ticket')]
#[Serializer\DiscriminatorMap(typeProperty: 'type', mapping: ['ticket_addressee_history' => AddresseeHistory::class])]
class AddresseeHistory implements TrackUpdateInterface, TrackCreationInterface
{
    use TrackCreationTrait;
    use TrackUpdateTrait;

    #[ORM\Id]
    #[ORM\Column(type: \Doctrine\DBAL\Types\Types::INTEGER, nullable: false)]
    #[ORM\GeneratedValue(strategy: 'AUTO')]
    #[Serializer\Groups(['read'])]
    private ?int $id = null;

    #[ORM\ManyToOne(targetEntity: User::class)]
    #[ORM\JoinColumn(nullable: true)]
    private ?User $addresseeUser = null;

    #[ORM\ManyToOne(targetEntity: UserGroup::class)]
    #[ORM\JoinColumn(nullable: true)]
    private ?UserGroup $addresseeGroup = null;

    #[ORM\Column(type: \Doctrine\DBAL\Types\Types::DATETIME_IMMUTABLE, nullable: true, options: ['default' => 'null'])]
    #[Serializer\Groups(['read'])]
    private ?\DateTimeImmutable $endDate = null;

    #[ORM\ManyToOne(targetEntity: User::class)]
    #[ORM\JoinColumn(nullable: true)]
    #[Serializer\Groups(['read'])]
    private ?User $removedBy = null;

    public function __construct(
        User|UserGroup $addressee,
        #[ORM\Column(type: \Doctrine\DBAL\Types\Types::DATETIME_IMMUTABLE, nullable: false)]
        #[Serializer\Groups(['read'])]
        private \DateTimeImmutable $startDate,
        #[ORM\ManyToOne(targetEntity: Ticket::class)]
        #[ORM\JoinColumn(nullable: false)]
        private Ticket $ticket,
    ) {
        if ($addressee instanceof User) {
            $this->addresseeUser = $addressee;
        } else {
            $this->addresseeGroup = $addressee;
        }

        $this->ticket->addAddresseeHistory($this);
    }

    #[Serializer\Groups(['read'])]
    public function getAddressee(): UserGroup|User
    {
        if (null !== $this->addresseeGroup) {
            return $this->addresseeGroup;
        }

        return $this->addresseeUser;
    }

    public function getAddresseeGroup(): ?UserGroup
    {
        return $this->addresseeGroup;
    }

    public function getAddresseeUser(): ?User
    {
        return $this->addresseeUser;
    }

    public function getEndDate(): ?\DateTimeImmutable
    {
        return $this->endDate;
    }

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

    public function getStartDate(): \DateTimeImmutable
    {
        return $this->startDate;
    }

    public function getTicket(): Ticket
    {
        return $this->ticket;
    }

    public function getRemovedBy(): ?User
    {
        return $this->removedBy;
    }

    public function setRemovedBy(?User $removedBy): self
    {
        $this->removedBy = $removedBy;

        return $this;
    }

    public function setEndDate(?\DateTimeImmutable $endDate): self
    {
        $this->endDate = $endDate;

        return $this;
    }
}
