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

/**
 * History of input for a single ticket.
 *
 * An input is someone who triggered the opening of the ticket. Typically, it's the "caller" of a call.
 */
#[ORM\Entity()]
#[ORM\Table(name: 'input_history', schema: 'chill_ticket')]
class InputHistory
{
    #[ORM\Id]
    #[ORM\Column(type: \Doctrine\DBAL\Types\Types::INTEGER, nullable: false)]
    #[ORM\GeneratedValue(strategy: 'AUTO')]
    private ?int $id = null;

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

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

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

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

    public function __construct(
        Person|ThirdParty $input,
        #[ORM\ManyToOne(targetEntity: Ticket::class)]
        #[ORM\JoinColumn(nullable: false)]
        private Ticket $ticket,
        #[ORM\Column(type: \Doctrine\DBAL\Types\Types::DATETIME_IMMUTABLE, nullable: false)]
        private \DateTimeImmutable $startDate,
    ) {
        if ($input instanceof Person) {
            $this->person = $input;
        } else {
            $this->thirdParty = $input;
        }
    }

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

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

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

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

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

    public function getInput(): Person|ThirdParty
    {
        if (null !== $this->person) {
            return $this->person;
        }

        return $this->thirdParty;
    }
}
