<?php

namespace App\Tests\Command;

use App\Command\SearchCommand;
use App\Entity\Account;
use App\Entity\Contrats;
use App\Entity\Customer;
use App\Entity\Options;
use App\Entity\Product;
use App\Service\Search\Client;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;

#[AllowMockObjectsWithoutExpectations]
class SearchCommandTest extends TestCase
{
    private MockObject&EntityManagerInterface $entityManager;
    private MockObject&Client $client;
    
    // Repositories
    private MockObject&EntityRepository $accountRepo;
    private MockObject&EntityRepository $customerRepo;
    private MockObject&EntityRepository $productRepo;
    private MockObject&EntityRepository $optionsRepo;
    private MockObject&EntityRepository $contratsRepo;

    protected function setUp(): void
    {
        $this->entityManager = $this->createMock(EntityManagerInterface::class);
        $this->client = $this->createMock(Client::class);
        
        $this->accountRepo = $this->createMock(EntityRepository::class);
        $this->customerRepo = $this->createMock(EntityRepository::class);
        $this->productRepo = $this->createMock(EntityRepository::class);
        $this->optionsRepo = $this->createMock(EntityRepository::class);
        $this->contratsRepo = $this->createMock(EntityRepository::class);

        $this->entityManager->method('getRepository')->willReturnMap([
            [Account::class, $this->accountRepo],
            [Customer::class, $this->customerRepo],
            [Product::class, $this->productRepo],
            [Options::class, $this->optionsRepo],
            [Contrats::class, $this->contratsRepo],
        ]);
    }

    public function testExecute()
    {
        // 1. Setup Data
        
        // Account (one ROOT to skip, one normal to index)
        $rootAccount = $this->createMock(Account::class);
        $rootAccount->method('getRoles')->willReturn(['ROLE_ROOT', 'ROLE_USER']);
        
        $adminAccount = $this->createMock(Account::class);
        $adminAccount->method('getRoles')->willReturn(['ROLE_ADMIN']);
        $adminAccount->method('getId')->willReturn(1);
        $adminAccount->method('getName')->willReturn('Admin');
        $adminAccount->method('getFirstName')->willReturn('User'); // surname mapped to getFirstName in command
        $adminAccount->method('getEmail')->willReturn('admin@test.com');

        $this->accountRepo->method('findAll')->willReturn([$rootAccount, $adminAccount]);
        
        // Customer
        $customer = $this->createMock(Customer::class);
        $customer->method('getId')->willReturn(10);
        $customer->method('getName')->willReturn('Cust');
        $customer->method('getSurname')->willReturn('Omer');
        $customer->method('getSiret')->willReturn('123');
        $customer->method('getCiv')->willReturn('Mr');
        $customer->method('getType')->willReturn('pro');
        $customer->method('getPhone')->willReturn('0102030405');
        $customer->method('getEmail')->willReturn('cust@test.com');
        
        $this->customerRepo->method('findAll')->willReturn([$customer]);

        // Product
        $product = $this->createMock(Product::class);
        $product->method('getId')->willReturn(20);
        $product->method('getName')->willReturn('Prod');
        $product->method('getRef')->willReturn('REF001');
        
        $this->productRepo->method('findAll')->willReturn([$product]);

        // Options
        $option = $this->createMock(Options::class);
        $option->method('getId')->willReturn(30);
        $option->method('getName')->willReturn('Opt');

        $this->optionsRepo->method('findAll')->willReturn([$option]);

        // Contrats (Note: command uses findAll on Contrats::class but variable named $options)
        $contrat = $this->createMock(Contrats::class);
        $contrat->method('getId')->willReturn(40);
        $contrat->method('getNumReservation')->willReturn('RES-100');

        $this->contratsRepo->method('findAll')->willReturn([$contrat]);

        // 2. Expectations
        $this->client->expects($this->once())->method('init');
        
        $capturedArgs = [];
        $this->client->expects($this->exactly(5))
            ->method('indexDocuments')
            ->willReturnCallback(function($data, $index) use (&$capturedArgs) {
                $capturedArgs[] = [$data, $index];
                return true;
            });

        // 3. Execute
        $command = new SearchCommand($this->entityManager, $this->client);
        $application = new Application();
        $application->addCommand($command);
        $commandTester = new CommandTester($application->find('app:search'));

        $commandTester->execute([]);
        
        $this->assertStringContainsString('Indexation terminée', $commandTester->getDisplay());
        
        // Check captured args
        $this->assertCount(5, $capturedArgs);
        
        // Admin
        $this->assertEquals(['id' => 1, 'name' => 'Admin', 'surname' => 'User', 'email' => 'admin@test.com'], $capturedArgs[0][0]);
        $this->assertEquals('admin', $capturedArgs[0][1]);
        
        // Customer
        $this->assertEquals(['id' => 10, 'name' => 'Cust', 'surname' => 'Omer', 'siret' => '123', 'civ' => 'Mr', 'type' => 'pro', 'phone' => '0102030405', 'email' => 'cust@test.com'], $capturedArgs[1][0]);
        $this->assertEquals('customer', $capturedArgs[1][1]);
        
        // Product
        $this->assertEquals(['id' => 20, 'name' => 'Prod', 'ref' => 'REF001'], $capturedArgs[2][0]);
        $this->assertEquals('product', $capturedArgs[2][1]);
        
        // Options
        $this->assertEquals(['id' => 30, 'name' => 'Opt'], $capturedArgs[3][0]);
        $this->assertEquals('options', $capturedArgs[3][1]);
        
        // Contrat
        $this->assertEquals(['id' => 40, 'num' => 'RES-100'], $capturedArgs[4][0]);
        $this->assertEquals('contrat', $capturedArgs[4][1]);
    }
}
