<?php

namespace App\Controller\Admin;

use App\Service\MeilisearchService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;

#[Route('/admin', name: 'app_admin_')]
#[IsGranted('ROLE_EMPLOYE')]
class DashboardController extends AbstractController
{
    private const CLIENT_URL_PREFIX = '/admin/clients/';

    #[Route('', name: 'dashboard')]
    public function index(): Response
    {
        return $this->render('admin/dashboard.html.twig', [
            'breadcrumbs' => [
                ['name' => 'Administration'],
            ],
        ]);
    }

    #[Route('/global-search', name: 'global_search', methods: ['GET'])]
    public function globalSearch(Request $request, MeilisearchService $meilisearch): JsonResponse
    {
        $q = trim($request->query->getString('q'));

        if (\strlen($q) < 2) {
            return new JsonResponse([]);
        }

        $results = [];

        foreach ($meilisearch->searchCustomers($q, 5) as $h) {
            $results[] = ['type' => 'client', 'label' => $h['fullName'] ?? $h['raisonSociale'] ?? '', 'sub' => $h['email'] ?? '', 'url' => self::CLIENT_URL_PREFIX.$h['id']];
        }

        foreach ($meilisearch->searchDomains($q, 5) as $h) {
            $results[] = ['type' => 'ndd', 'label' => $h['fqdn'] ?? '', 'sub' => $h['customerName'] ?? '', 'url' => self::CLIENT_URL_PREFIX.($h['customerId'] ?? 0).'?tab=ndd'];
        }

        foreach ($meilisearch->searchWebsites($q, 5) as $h) {
            $results[] = ['type' => 'site', 'label' => $h['name'] ?? '', 'sub' => $h['customerName'] ?? '', 'url' => self::CLIENT_URL_PREFIX.($h['customerId'] ?? 0).'?tab=sites'];
        }

        foreach ($meilisearch->searchContacts($q, 5) as $h) {
            $results[] = ['type' => 'contact', 'label' => $h['fullName'] ?? '', 'sub' => ($h['role'] ?? '').($h['email'] ? ' — '.$h['email'] : ''), 'url' => self::CLIENT_URL_PREFIX.($h['customerId'] ?? 0).'?tab=contacts'];
        }

        foreach ($meilisearch->searchRevendeurs($q, 3) as $h) {
            $results[] = ['type' => 'revendeur', 'label' => $h['fullName'] ?? $h['raisonSociale'] ?? '', 'sub' => $h['codeRevendeur'] ?? '', 'url' => '/admin/revendeurs/'.($h['id'] ?? 0).'/edit'];
        }

        foreach ($meilisearch->searchDevis($q, 3) as $h) {
            $results[] = ['type' => 'devis', 'label' => $h['numOrder'] ?? '', 'sub' => ($h['customerName'] ?? '').' — '.($h['totalTtc'] ?? '0').' EUR', 'url' => self::CLIENT_URL_PREFIX.($h['customerId'] ?? 0).'?tab=devis'];
        }

        foreach ($meilisearch->searchAdverts($q, 3) as $h) {
            $results[] = ['type' => 'avis', 'label' => $h['numOrder'] ?? '', 'sub' => ($h['customerName'] ?? '').' — '.($h['totalTtc'] ?? '0').' EUR', 'url' => self::CLIENT_URL_PREFIX.($h['customerId'] ?? 0).'?tab=avis'];
        }

        foreach ($meilisearch->searchFactures($q, 3) as $h) {
            $results[] = ['type' => 'facture', 'label' => $h['invoiceNumber'] ?? '', 'sub' => ($h['customerName'] ?? '').' — '.($h['totalTtc'] ?? '0').' EUR', 'url' => self::CLIENT_URL_PREFIX.($h['customerId'] ?? 0).'?tab=factures'];
        }

        return new JsonResponse($results);
    }
}
