<?php

namespace App\Tests\Command;

use App\Command\MeilisearchSetupCommand;
use App\Entity\Customer;
use App\Entity\Revendeur;
use App\Entity\User;
use App\Repository\CustomerRepository;
use App\Repository\RevendeurRepository;
use App\Service\MeilisearchService;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Tester\CommandTester;

class MeilisearchSetupCommandTest extends TestCase
{
    public function testExecute(): void
    {
        $meilisearch = $this->createStub(MeilisearchService::class);
        $customerRepo = $this->createStub(CustomerRepository::class);
        $revendeurRepo = $this->createStub(RevendeurRepository::class);

        $customerRepo->method('findAll')->willReturn([]);
        $revendeurRepo->method('findAll')->willReturn([]);

        $command = new MeilisearchSetupCommand($meilisearch, $customerRepo, $revendeurRepo);
        $tester = new CommandTester($command);
        $tester->execute([]);

        $this->assertSame(0, $tester->getStatusCode());
        $this->assertStringContainsString('Meilisearch configure et donnees indexees', $tester->getDisplay());
    }

    public function testExecuteWithData(): void
    {
        $meilisearch = $this->createMock(MeilisearchService::class);
        $customerRepo = $this->createStub(CustomerRepository::class);
        $revendeurRepo = $this->createStub(RevendeurRepository::class);

        $user = $this->createStub(User::class);
        $customer = new Customer($user);
        $revendeur = new Revendeur($user, 'REF-123');

        $customerRepo->method('findAll')->willReturn([$customer]);
        $revendeurRepo->method('findAll')->willReturn([$revendeur]);

        $meilisearch->expects($this->once())->method('indexCustomer')->with($customer);
        $meilisearch->expects($this->once())->method('indexRevendeur')->with($revendeur);

        $command = new MeilisearchSetupCommand($meilisearch, $customerRepo, $revendeurRepo);
        $tester = new CommandTester($command);
        $tester->execute([]);

        $this->assertStringContainsString('1 client(s) indexe(s)', $tester->getDisplay());
        $this->assertStringContainsString('1 revendeur(s) indexe(s)', $tester->getDisplay());
    }
}
