<?php

namespace App\Tests\Repository;

use App\Entity\Contrats;
use App\Entity\Customer;
use App\Repository\ContratsRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

class ContratsRepositoryTest extends KernelTestCase
{
    private ?EntityManagerInterface $entityManager;
    private ?ContratsRepository $repository;

    protected function setUp(): void
    {
        $kernel = self::bootKernel();

        $this->entityManager = $kernel->getContainer()
            ->get('doctrine')
            ->getManager();
            
        $this->repository = $this->entityManager->getRepository(Contrats::class);
    }

    protected function tearDown(): void
    {
        parent::tearDown();
        $this->entityManager->close();
        $this->entityManager = null;
        $this->repository = null;
    }

    public function testFindBetweenDates()
    {
        $customer = new Customer();
        $customer->setEmail('contrat_' . uniqid() . '@test.com');
        $customer->setName('Test');
        $customer->setSurname('Contrat');
        $customer->setPhone('0600000000');
        $customer->setCiv('Mr');
        $customer->setType('pro');
        $this->entityManager->persist($customer);

        // 1. Create Contrats
        // Target: 2026-06-15
        $contratTarget = new Contrats();
        $contratTarget->setDateAt(new \DateTimeImmutable('2026-06-15 12:00:00'));
        $contratTarget->setEndAt(new \DateTimeImmutable('2026-06-16 12:00:00'));
        $contratTarget->setCreateAt(new \DateTimeImmutable());
        $contratTarget->setNumReservation('RES-TARGET');
        $contratTarget->setAddressEvent('123 Main St');
        $contratTarget->setZipCodeEvent('12345');
        $contratTarget->setTownEvent('City');
        $contratTarget->setType('event');
        $contratTarget->setPente('flat');
        $contratTarget->setIsSigned(false);
        $contratTarget->setCustomer($customer);
        $this->entityManager->persist($contratTarget);

        // Before: 2026-06-01
        $contratBefore = new Contrats();
        $contratBefore->setDateAt(new \DateTimeImmutable('2026-06-01 12:00:00'));
        $contratBefore->setEndAt(new \DateTimeImmutable('2026-06-02 12:00:00'));
        $contratBefore->setCreateAt(new \DateTimeImmutable());
        $contratBefore->setNumReservation('RES-BEFORE');
        $contratBefore->setAddressEvent('123 Main St');
        $contratBefore->setZipCodeEvent('12345');
        $contratBefore->setTownEvent('City');
        $contratBefore->setType('event');
        $contratBefore->setPente('flat');
        $contratBefore->setIsSigned(false);
        $contratBefore->setCustomer($customer);
        $this->entityManager->persist($contratBefore);

        // After: 2026-07-01
        $contratAfter = new Contrats();
        $contratAfter->setDateAt(new \DateTimeImmutable('2026-07-01 12:00:00'));
        $contratAfter->setEndAt(new \DateTimeImmutable('2026-07-02 12:00:00'));
        $contratAfter->setCreateAt(new \DateTimeImmutable());
        $contratAfter->setNumReservation('RES-AFTER');
        $contratAfter->setAddressEvent('123 Main St');
        $contratAfter->setZipCodeEvent('12345');
        $contratAfter->setTownEvent('City');
        $contratAfter->setType('event');
        $contratAfter->setPente('flat');
        $contratAfter->setIsSigned(false);
        $contratAfter->setCustomer($customer);
        $this->entityManager->persist($contratAfter);

        $this->entityManager->flush();

        // 2. Search Interval: 2026-06-10 to 2026-06-20
        $start = new \DateTimeImmutable('2026-06-10 00:00:00');
        $end = new \DateTimeImmutable('2026-06-20 00:00:00');

        $results = $this->repository->findBetweenDates($start, $end);

        $this->assertGreaterThanOrEqual(1, count($results));

        $foundTarget = false;
        foreach ($results as $contrat) {
            if ($contrat->getNumReservation() === 'RES-TARGET') {
                $foundTarget = true;
            }
            if ($contrat->getNumReservation() === 'RES-BEFORE') {
                $this->fail('Contrat BEFORE interval should not be found');
            }
            if ($contrat->getNumReservation() === 'RES-AFTER') {
                $this->fail('Contrat AFTER interval should not be found');
            }
        }

        $this->assertTrue($foundTarget, 'Target contrat not found');
    }
}
