<?php

namespace App\Tests\Service\Signature;

use App\Entity\Contrats;
use App\Entity\Customer;
use App\Service\Signature\Client;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Vich\UploaderBundle\Storage\StorageInterface;

class ClientTest extends TestCase
{
    public function testStatus(): void
    {
        $requestStack = $this->createStub(RequestStack::class);
        $urlGenerator = $this->createStub(UrlGeneratorInterface::class);
        $entityManager = $this->createStub(EntityManagerInterface::class);
        $kernel = $this->createStub(KernelInterface::class);
        $storage = $this->createStub(StorageInterface::class);
        
        $docuseal = $this->createStub(\Docuseal\Api::class);
        $docuseal->method('listTemplates')->willReturn([]);

        $client = new Client($requestStack, $urlGenerator, $entityManager, $kernel, $storage, $docuseal);
        
        $this->assertTrue($client->status());
    }

    public function testCreateSubmissionContrat(): void
    {
        $requestStack = $this->createStub(RequestStack::class);
        $urlGenerator = $this->createStub(UrlGeneratorInterface::class);
        $entityManager = $this->createStub(EntityManagerInterface::class);
        $kernel = $this->createStub(KernelInterface::class);
        $storage = $this->createStub(StorageInterface::class);
        $docuseal = $this->createStub(\Docuseal\Api::class);

        $kernel->method('getProjectDir')->willReturn(sys_get_temp_dir());

        $client = new Client($requestStack, $urlGenerator, $entityManager, $kernel, $storage, $docuseal);

        $contrat = $this->createMock(Contrats::class);
        $customer = $this->createStub(Customer::class);
        
        $contrat->method('getCustomer')->willReturn($customer);
        $contrat->method('getNumReservation')->willReturn('RES-123');
        $contrat->method('getId')->willReturn(1);

        $customer->method('getEmail')->willReturn('test@example.com');
        $customer->method('getName')->willReturn('Doe');
        $customer->method('getSurname')->willReturn('John');

        $docuseal->method('createSubmissionFromPdf')->willReturn([
            'submitters' => [
                1 => ['id' => 'sub_123']
            ]
        ]);
        
        $docuseal->method('getSubmitter')->with('sub_123')->willReturn(['slug' => 'slug_123']);

        $contrat->expects($this->exactly(2))
            ->method('getSignID')
            ->willReturnOnConsecutiveCalls(null, 'sub_123');
            
        $contrat->expects($this->once())->method('setSignID')->with('sub_123');

        $link = $client->createSubmissionContrat($contrat);
        
        $this->assertStringContainsString('/s/slug_123', $link);
    }
}
