<?php

namespace App\Tests\Event\Signature;

use App\Entity\Customer;
use App\Entity\Devis;
use App\Event\Signature\DevisSend;
use App\Event\Signature\DevisSubscriber;
use App\Service\Mailer\Mailer;
use App\Service\Signature\Client;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations;
use PHPUnit\Framework\TestCase;

#[AllowMockObjectsWithoutExpectations]
class DevisSubscriberTest extends TestCase
{
    private $entityManager;
    private $mailer;
    private $client;
    private $subscriber;

    protected function setUp(): void
    {
        $this->entityManager = $this->createMock(EntityManagerInterface::class);
        $this->mailer = $this->createMock(Mailer::class);
        $this->client = $this->createMock(Client::class);

        $this->subscriber = new DevisSubscriber(
            $this->entityManager,
            $this->mailer,
            $this->client
        );
    }

    public function testOnDevisSend()
    {
        $customer = $this->createMock(Customer::class);
        $customer->method('getEmail')->willReturn('cust@test.com');
        $customer->method('getName')->willReturn('Doe');
        $customer->method('getSurname')->willReturn('John');

        $devis = $this->createMock(Devis::class);
        $devis->method('getCustomer')->willReturn($customer);
        $devis->method('getSignatureId')->willReturn('sign_id_123');

        $event = new DevisSend($devis);

        $this->client->expects($this->once())
            ->method('getLinkSign')
            ->with('sign_id_123')
            ->willReturn('http://sign.link');

        $this->mailer->expects($this->once())
            ->method('send')
            ->with(
                'cust@test.com',
                'Doe John',
                '[Signature Ludikevent] - Signature de votre devis pour votre location',
                'mails/sign/devis.twig',
                $this->callback(function($context) use ($devis) {
                    return $context['devis'] === $devis && $context['signLink'] === 'http://sign.link';
                })
            );

        $this->subscriber->onDevisSend($event);
    }
}
