<?php

namespace App\Tests\Entity;

use App\Entity\Contrats;
use App\Entity\ContratsLine;
use App\Entity\ContratsOption;
use App\Entity\ContratsPayments;
use App\Entity\Customer;
use App\Entity\Devis;
use App\Entity\Facture;
use App\Entity\EtatLieux;
use App\Entity\ProductReserve;
use PHPUnit\Framework\TestCase;

class ContratsTest extends TestCase
{
    public function testGettersAndSetters()
    {
        $contrats = new Contrats();
        $customer = new Customer();
        $devis = new Devis();
        $now = new \DateTimeImmutable();

        $contrats->setCustomer($customer);
        $contrats->setDevis($devis);
        $contrats->setNumReservation('RES-001');
        $contrats->setCreateAt($now); // createAt is set in constructor (or entity-level default)
        $contrats->setAddressEvent('123 Event St');
        $contrats->setAddress2Event('Apt 4B');
        $contrats->setAddress3Event('Building C');
        $contrats->setZipCodeEvent('54321');
        $contrats->setTownEvent('Eventville');
        $contrats->setType('Type A');
        $contrats->setDetails('Some details');
        $contrats->setTypeSol('Hard');
        $contrats->setPente('flat');
        $contrats->setAccess('Easy');
        $contrats->setDistancePower(10.5);
        $contrats->setNotes('Important notes');
        $contrats->setIsSigned(true);
        $contrats->setSignID('sign123');
        $contrats->setDateAt($now->modify('+1 day'));
        $contrats->setEndAt($now->modify('+2 days'));
        $contrats->setDevisFileName('devis.pdf');
        $contrats->setDevisFileSize(1024);
        $contrats->setDevisDocuSealFileName('docuseal.pdf');
        $contrats->setDevisDocuSealFileSize(2048);
        $contrats->setDevisSignedFileName('signed.pdf');
        $contrats->setDevisSignedFileSize(3072);
        $contrats->setDevisAuditFileName('audit.pdf');
        $contrats->setDevisAuditFileSize(4096);
        $contrats->setCautionState('pending');



        $this->assertSame($customer, $contrats->getCustomer());
        $this->assertSame($devis, $contrats->getDevis());
        $this->assertEquals('RES-001', $contrats->getNumReservation());
        $this->assertNotNull($contrats->getCreateAt()); // Constructor sets it
        $this->assertEquals('123 Event St', $contrats->getAddressEvent());
        $this->assertEquals('Apt 4B', $contrats->getAddress2Event());
        $this->assertEquals('Building C', $contrats->getAddress3Event());
        $this->assertEquals('54321', $contrats->getZipCodeEvent());
        $this->assertEquals('Eventville', $contrats->getTownEvent());
        $this->assertEquals('Type A', $contrats->getType());
        $this->assertEquals('Some details', $contrats->getDetails());
        $this->assertEquals('Hard', $contrats->getTypeSol());
        $this->assertEquals('flat', $contrats->getPente());
        $this->assertEquals('Easy', $contrats->getAccess());
        $this->assertEquals(10.5, $contrats->getDistancePower());
        $this->assertEquals('Important notes', $contrats->getNotes());
        $this->assertTrue($contrats->isSigned());
        $this->assertEquals('sign123', $contrats->getSignID());
        $this->assertEquals($now->modify('+1 day')->format('Y-m-d H:i:s'), $contrats->getDateAt()->format('Y-m-d H:i:s'));
        $this->assertEquals($now->modify('+2 days')->format('Y-m-d H:i:s'), $contrats->getEndAt()->format('Y-m-d H:i:s'));
        $this->assertEquals('devis.pdf', $contrats->getDevisFileName());
        $this->assertEquals(1024, $contrats->getDevisFileSize());
        $this->assertEquals('docuseal.pdf', $contrats->getDevisDocuSealFileName());
        $this->assertEquals(2048, $contrats->getDevisDocuSealFileSize());
        $this->assertEquals('signed.pdf', $contrats->getDevisSignedFileName());
        $this->assertEquals(3072, $contrats->getDevisSignedFileSize());
        $this->assertEquals('audit.pdf', $contrats->getDevisAuditFileName());
        $this->assertEquals(4096, $contrats->getDevisAuditFileSize());
        $this->assertEquals('pending', $contrats->getCautionState());
    }

    public function testContratsPaymentsCollection()
    {
        $contrats = new Contrats();
        $payment = new ContratsPayments();

        $this->assertCount(0, $contrats->getContratsPayments());
        $contrats->addContratsPayment($payment);
        $this->assertCount(1, $contrats->getContratsPayments());
        $this->assertSame($contrats, $payment->getContrat());

        $contrats->removeContratsPayment($payment);
        $this->assertCount(0, $contrats->getContratsPayments());
        $this->assertNull($payment->getContrat());
    }

    public function testContratsLinesCollection()
    {
        $contrats = new Contrats();
        $line = new ContratsLine();

        $this->assertCount(0, $contrats->getContratsLines());
        $contrats->addContratsLine($line);
        $this->assertCount(1, $contrats->getContratsLines());
        $this->assertSame($contrats, $line->getContrat());

        $contrats->removeContratsLine($line);
        $this->assertCount(0, $contrats->getContratsLines());
        $this->assertNull($line->getContrat());
    }

    public function testContratsOptionsCollection()
    {
        $contrats = new Contrats();
        $option = new ContratsOption();

        $this->assertCount(0, $contrats->getContratsOptions());
        $contrats->addContratsOption($option);
        $this->assertCount(1, $contrats->getContratsOptions());
        $this->assertSame($contrats, $option->getContrat());

        $contrats->removeContratsOption($option);
        $this->assertCount(0, $contrats->getContratsOptions());
        $this->assertNull($option->getContrat());
    }

    public function testProductReservesCollection()
    {
        $contrats = new Contrats();
        $productReserve = new ProductReserve();

        $this->assertCount(0, $contrats->getProductReserves());
        $contrats->addProductReserf($productReserve); // Typo: should be addProductReserve
        $this->assertCount(1, $contrats->getProductReserves());
        $this->assertSame($contrats, $productReserve->getContrat());

        $contrats->removeProductReserf($productReserve); // Typo: should be removeProductReserve
        $this->assertCount(0, $contrats->getProductReserves());
        $this->assertNull($productReserve->getContrat());
    }

    public function testSetFacture()
    {
        $contrats = new Contrats();
        $facture = new Facture();

        $this->assertNull($contrats->getFacture());
        $contrats->setFacture($facture);
        $this->assertSame($facture, $contrats->getFacture());
        $this->assertSame($contrats, $facture->getContrat()); // Assuming Facture has setContrat

        $contrats->setFacture(null);
        $this->assertNull($contrats->getFacture());
        $this->assertNull($facture->getContrat());
    }

    public function testSetEtatLieux()
    {
        $contrats = new Contrats();
        $etatLieux = new EtatLieux();

        $this->assertNull($contrats->getEtatLieux());
        $contrats->setEtatLieux($etatLieux);
        $this->assertSame($etatLieux, $contrats->getEtatLieux());
        $this->assertSame($contrats, $etatLieux->getContrat()); // Assuming EtatLieux has setContrat

        $contrats->setEtatLieux(null);
        $this->assertNull($contrats->getEtatLieux());
        $this->assertNull($etatLieux->getContrat());
    }

    public function testIsCaution()
    {
        $contrats = new Contrats();
        $payment1 = (new ContratsPayments())->setType('caution')->setState('pending');
        $payment2 = (new ContratsPayments())->setType('caution')->setState('complete');
        $payment3 = (new ContratsPayments())->setType('accompte')->setState('complete');

        $contrats->addContratsPayment($payment1);
        $contrats->addContratsPayment($payment3);
        $this->assertFalse($contrats->isCaution());

        $contrats->addContratsPayment($payment2);
        $this->assertTrue($contrats->isCaution());
    }

    public function testIsAccompte()
    {
        $contrats = new Contrats();
        $payment1 = (new ContratsPayments())->setType('accompte')->setState('pending');
        $payment2 = (new ContratsPayments())->setType('accompte')->setState('complete');

        $contrats->addContratsPayment($payment1);
        $this->assertFalse($contrats->isAccompte());

        $contrats->addContratsPayment($payment2);
        $this->assertTrue($contrats->isAccompte());
    }

    public function testIsSolde()
    {
        $contrats = new Contrats();
        $payment1 = (new ContratsPayments())->setType('solde')->setState('pending');
        $payment2 = (new ContratsPayments())->setType('solde')->setState('complete');

        $contrats->addContratsPayment($payment1);
        $this->assertFalse($contrats->isSolde());

        $contrats->addContratsPayment($payment2);
        $this->assertTrue($contrats->isSolde());
    }
}
