<?php

declare(strict_types=1);

namespace App\Command;

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 Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(name: 'app:search', description: 'Search')]
class SearchCommand extends Command
{
    public function __construct(private readonly EntityManagerInterface $entityManager,private readonly Client $client,?string $name = null)
    {
        parent::__construct($name);
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $this->client->init();

        $accounts = $this->entityManager->getRepository(Account::class)->findAll();

        foreach ($accounts as $account) {
            // ON IGNORE LES COMPTES AYANT LE RÔLE ROLE_ROOT
            if (in_array('ROLE_ROOT', $account->getRoles(), true)) {
                continue;
            }

            $datas = [
                'id' => $account->getId(),
                'name' => $account->getName(),
                'surname' => $account->getFirstName(),
                'email' => $account->getEmail(),
            ];

            $this->client->indexDocuments($datas, 'admin');
        }

        $customers = $this->entityManager->getRepository(Customer::class)->findAll();

        foreach ($customers as $customer) {

            $datas = [
                'id' => $customer->getId(),
                'name' => $customer->getName(),
                'surname' => $customer->getSurname(),
                'siret' => $customer->getSiret(),
                'civ' => $customer->getCiv(),
                'type' => $customer->getType(),
                'phone' => $customer->getPhone(),
                'email' => $customer->getEmail(),
            ];

            $this->client->indexDocuments($datas, 'customer');
        }

        $products = $this->entityManager->getRepository(Product::class)->findAll();

        foreach ($products as $product) {

            $datas = [
                'id' => $product->getId(),
                'name' => $product->getName(),
                'ref' => $product->getRef(),
            ];

            $this->client->indexDocuments($datas, 'product');
        }
        $options = $this->entityManager->getRepository(Options::class)->findAll();

        foreach ($options as $option) {

            $datas = [
                'id' => $option->getId(),
                'name' => $option->getName(),
            ];

            $this->client->indexDocuments($datas, 'options');
        }

        $options = $this->entityManager->getRepository(Contrats::class)->findAll();

        foreach ($options as $option) {

            $datas = [
                'id' => $option->getId(),
                'num' => $option->getNumReservation(),
            ];

            $this->client->indexDocuments($datas, 'contrat');
        }
        $output->writeln('Indexation terminée (hors ROOT).');

        return Command::SUCCESS;
    }
}
