<?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\PersonBundle\Entity\Person;
use Chill\ThirdPartyBundle\Entity\ThirdParty;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation as Serializer;

/**
 * Represents the history of a caller associated with a ticket.
 *
 * This entity is used to track the changes in a ticket's caller over time.
 * The caller can be either a Person or a ThirdParty.
 * Implements the TrackCreationInterface for tracking entity lifecycle creation.
 */
#[ORM\Entity]
#[ORM\Table(name: 'caller_history', schema: 'chill_ticket')]
#[Serializer\DiscriminatorMap(typeProperty: 'type', mapping: ['ticket_caller_history' => CallerHistory::class])]
class CallerHistory implements TrackCreationInterface
{
    use TrackCreationTrait;

    #[ORM\Id]
    #[ORM\Column(type: \Doctrine\DBAL\Types\Types::INTEGER, nullable: false)]
    #[ORM\GeneratedValue(strategy: 'AUTO')]
    #[Serializer\Groups(['read'])]
    private ?int $id = 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: Person::class)]
    #[ORM\JoinColumn(nullable: true)]
    #[Serializer\Groups(['read'])]
    private ?Person $person = null;

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

    public function __construct(
        ThirdParty|Person|null $caller,
        #[ORM\ManyToOne(targetEntity: Ticket::class)]
        #[ORM\JoinColumn(nullable: false)]
        private Ticket $ticket,
        #[ORM\Column(type: \Doctrine\DBAL\Types\Types::DATETIME_IMMUTABLE, nullable: false)]
        #[Serializer\Groups(['read'])]
        private \DateTimeImmutable $startDate = new \DateTimeImmutable('now'),
    ) {
        $this->setCaller($caller);
        $ticket->addCallerHistory($this);
    }

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

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

    public function getPerson(): ?Person
    {
        return $this->person;
    }

    public function getThirdParty(): ?ThirdParty
    {
        return $this->thirdParty;
    }

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

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

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

    public function setPerson(?Person $person): self
    {
        $this->person = $person;

        // If setting a person, ensure thirdParty is null
        if (null !== $person) {
            $this->thirdParty = null;
        }

        return $this;
    }

    public function setThirdParty(?ThirdParty $thirdParty): self
    {
        $this->thirdParty = $thirdParty;

        // If setting a thirdParty, ensure person is null
        if (null !== $thirdParty) {
            $this->person = null;
        }

        return $this;
    }

    /**
     * Set the caller.
     *
     * This is a private method and should be only called while instance creation
     */
    private function setCaller(Person|ThirdParty|null $caller): void
    {
        if ($caller instanceof Person) {
            $this->setPerson($caller);
        } elseif ($caller instanceof ThirdParty) {
            $this->setThirdParty($caller);
        }
    }

    /**
     * Get the caller, which can be either a Person or a ThirdParty.
     */
    public function getCaller(): Person|ThirdParty|null
    {
        return $this->person ?? $this->thirdParty;
    }
}
