<?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\Tests\Entity;

use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Entity\UserGroup;
use Chill\PersonBundle\Entity\Person;
use Chill\TicketBundle\Entity\AddresseeHistory;
use Chill\TicketBundle\Entity\Motive;
use Chill\TicketBundle\Entity\MotiveHistory;
use Chill\TicketBundle\Entity\PersonHistory;
use Chill\TicketBundle\Entity\EmergencyStatusEnum;
use Chill\TicketBundle\Entity\EmergencyStatusHistory;
use Chill\TicketBundle\Entity\StateEnum;
use Chill\TicketBundle\Entity\StateHistory;
use Chill\TicketBundle\Entity\Ticket;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

/**
 * @internal
 *
 * @coversNothing
 */
class TicketTest extends KernelTestCase
{
    public function testGetMotive(): void
    {
        $ticket = new Ticket();
        $motive = new Motive();

        self::assertNull($ticket->getMotive());

        $history = new MotiveHistory($motive, $ticket);

        self::assertSame($motive, $ticket->getMotive());
        self::assertCount(1, $ticket->getMotiveHistories());

        // replace motive
        $motive2 = new Motive();
        $history->setEndDate(new \DateTimeImmutable());
        $history2 = new MotiveHistory($motive2, $ticket);

        self::assertCount(2, $ticket->getMotiveHistories());
        self::assertSame($motive2, $ticket->getMotive());
    }

    public function testGetPerson(): void
    {
        $ticket = new Ticket();
        $person = new Person();

        self::assertEquals([], $ticket->getPersons());

        $history = new PersonHistory($person, $ticket, new \DateTimeImmutable('now'));

        self::assertCount(1, $ticket->getPersons());
        self::assertSame($person, $ticket->getPersons()[0]);

        $history->setEndDate(new \DateTimeImmutable('now'));

        self::assertCount(0, $ticket->getPersons());
    }

    public function testGetAddresse(): void
    {
        $ticket = new Ticket();
        $user = new User();
        $group = new UserGroup();

        self::assertEquals([], $ticket->getCurrentAddressee());

        $history = new AddresseeHistory($user, new \DateTimeImmutable('now'), $ticket);

        self::assertCount(1, $ticket->getCurrentAddressee());
        self::assertSame($user, $ticket->getCurrentAddressee()[0]);

        $history2 = new AddresseeHistory($group, new \DateTimeImmutable('now'), $ticket);

        self::assertCount(2, $ticket->getCurrentAddressee());
        self::assertContains($group, $ticket->getCurrentAddressee());

        $history->setEndDate(new \DateTimeImmutable('now'));

        self::assertCount(1, $ticket->getCurrentAddressee());
        self::assertSame($group, $ticket->getCurrentAddressee()[0]);
    }

    public function testGetAndSetExternalRef(): void
    {
        $ticket = new Ticket();
        $externalRef = 'REF-123';

        // Set the external reference
        $ticket->setExternalRef($externalRef);

        // Verify that getExternalRef returns the correct value
        self::assertSame($externalRef, $ticket->getExternalRef());

        // Change the external reference
        $newExternalRef = 'REF-456';
        $ticket->setExternalRef($newExternalRef);

        // Verify that getExternalRef returns the updated value
        self::assertSame($newExternalRef, $ticket->getExternalRef());
    }

    public function testGetState(): void
    {
        $ticket = new Ticket();

        // Initially, the ticket has no state
        self::assertNull($ticket->getState());

        // Create a state history entry with the open state
        $history = new StateHistory(StateEnum::OPEN, $ticket);

        // Verify that the ticket now has the open state
        self::assertSame(StateEnum::OPEN, $ticket->getState());
        self::assertCount(1, $ticket->getStateHistories());

        // Change the state to closed
        $history->setEndDate(new \DateTimeImmutable());
        $history2 = new StateHistory(StateEnum::CLOSED, $ticket);

        // Verify that the ticket now has the closed state
        self::assertCount(2, $ticket->getStateHistories());
        self::assertSame(StateEnum::CLOSED, $ticket->getState());
    }

    public function testGetEmergencyStatus(): void
    {
        $ticket = new Ticket();

        // Initially, the ticket has no emergency status
        self::assertNull($ticket->getEmergencyStatus());

        // Create an emergency status history entry with the YES status
        $history = new EmergencyStatusHistory(EmergencyStatusEnum::YES, $ticket);

        // Verify that the ticket now has the YES status
        self::assertSame(EmergencyStatusEnum::YES, $ticket->getEmergencyStatus());
        self::assertCount(1, $ticket->getEmergencyStatusHistories());

        // Change the emergency status to NO
        $history->setEndDate(new \DateTimeImmutable());
        $history2 = new EmergencyStatusHistory(EmergencyStatusEnum::NO, $ticket);

        // Verify that the ticket now has the NO status
        self::assertCount(2, $ticket->getEmergencyStatusHistories());
        self::assertSame(EmergencyStatusEnum::NO, $ticket->getEmergencyStatus());
    }
}
