<?php

namespace App\Tests\Service;

use App\Entity\Devis;
use App\Entity\OrderNumber;
use App\Service\DevisService;
use App\Service\OrderNumberService;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\TestCase;

class DevisServiceTest extends TestCase
{
    public function testCreate(): void
    {
        $orderNumber = new OrderNumber('04/2026-00001');
        $orderNumber->markAsUsed();

        $orderService = $this->createStub(OrderNumberService::class);
        $orderService->method('generateAndUse')->willReturn($orderNumber);

        $em = $this->createMock(EntityManagerInterface::class);
        $em->expects($this->once())->method('persist')->with($this->isInstanceOf(Devis::class));
        $em->expects($this->once())->method('flush');

        $service = new DevisService($orderService, $em, 'test-secret');
        $devis = $service->create();

        $this->assertInstanceOf(Devis::class, $devis);
        $this->assertSame($orderNumber, $devis->getOrderNumber());
        $this->assertSame(Devis::STATE_CREATED, $devis->getState());
        $this->assertNotEmpty($devis->getHmac());
    }

    // --- isTvaEnabled ---

    public function testIsTvaEnabledReturnsTrueForTrueString(): void
    {
        $orderService = $this->createStub(OrderNumberService::class);
        $em = $this->createStub(EntityManagerInterface::class);

        $service = new DevisService($orderService, $em, 'secret', 'true');
        $this->assertTrue($service->isTvaEnabled());
    }

    public function testIsTvaEnabledReturnsTrueForOne(): void
    {
        $orderService = $this->createStub(OrderNumberService::class);
        $em = $this->createStub(EntityManagerInterface::class);

        $service = new DevisService($orderService, $em, 'secret', '1');
        $this->assertTrue($service->isTvaEnabled());
    }

    public function testIsTvaEnabledReturnsFalseByDefault(): void
    {
        $orderService = $this->createStub(OrderNumberService::class);
        $em = $this->createStub(EntityManagerInterface::class);

        $service = new DevisService($orderService, $em, 'secret');
        $this->assertFalse($service->isTvaEnabled());
    }

    // --- getTvaRate ---

    public function testGetTvaRateReturnsFloat(): void
    {
        $orderService = $this->createStub(OrderNumberService::class);
        $em = $this->createStub(EntityManagerInterface::class);

        $service = new DevisService($orderService, $em, 'secret', 'false', '0.20');
        $this->assertSame(0.20, $service->getTvaRate());
    }

    // --- computeTotals ---

    public function testComputeTotalsTvaDisabled(): void
    {
        $orderService = $this->createStub(OrderNumberService::class);
        $em = $this->createStub(EntityManagerInterface::class);

        $service = new DevisService($orderService, $em, 'secret', 'false', '0.20');
        $result = $service->computeTotals('200.00');

        $this->assertSame('200.00', $result['totalHt']);
        $this->assertSame('0.00', $result['totalTva']);
        $this->assertSame('200.00', $result['totalTtc']);
    }

    public function testComputeTotalsTvaEnabled(): void
    {
        $orderService = $this->createStub(OrderNumberService::class);
        $em = $this->createStub(EntityManagerInterface::class);

        $service = new DevisService($orderService, $em, 'secret', 'true', '0.20');
        $result = $service->computeTotals('500.00');

        $this->assertSame('500.00', $result['totalHt']);
        $this->assertSame('100.00', $result['totalTva']);
        $this->assertSame('600.00', $result['totalTtc']);
    }
}
