<?php

namespace App\Service;

use App\Entity\AppLog;
use App\Entity\User;
use App\Message\AppLogMessage;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\Messenger\MessageBusInterface;

class AppLoggerService
{
    /** @var array<string, string> */
    private const ROUTE_LABELS = [
        'app_admin_dashboard' => 'Consultation du tableau de bord',
        'app_admin_clients_index' => 'Consultation de la liste des clients',
        'app_admin_clients_create' => 'Creation d\'un client',
        'app_admin_clients_show' => 'Consultation d\'un client',
        'app_admin_clients_edit' => 'Modification d\'un client',
        'app_admin_clients_search' => 'Recherche de clients',
        'app_admin_facturation_index' => 'Consultation de la facturation',
        'app_admin_revendeurs_index' => 'Consultation des revendeurs',
        'app_admin_revendeurs_create' => 'Creation d\'un revendeur',
        'app_admin_revendeurs_edit' => 'Modification d\'un revendeur',
        'app_admin_revendeurs_contrat' => 'Generation d\'un contrat revendeur',
        'app_admin_revendeurs_search' => 'Recherche de revendeurs',
        'app_admin_membres' => 'Consultation des membres',
        'app_admin_membres_create' => 'Creation d\'un membre',
        'app_admin_membres_delete' => 'Suppression d\'un membre',
        'app_admin_membres_resend' => 'Renvoi des identifiants d\'un membre',
        'app_admin_stats_index' => 'Consultation des statistiques',
        'app_admin_status_index' => 'Consultation du status des services',
        'app_admin_status_resolve' => 'Resolution d\'un message de service',
        'app_admin_sync_index' => 'Consultation de la synchronisation',
        'app_admin_sync_all' => 'Synchronisation complete Meilisearch',
        'app_admin_sync_customers' => 'Synchronisation des clients Meilisearch',
        'app_admin_sync_revendeurs' => 'Synchronisation des revendeurs Meilisearch',
        'app_admin_sync_prices' => 'Synchronisation des tarifs Meilisearch',
        'app_admin_sync_stripe_prices' => 'Synchronisation des tarifs Stripe',
        'app_admin_sync_stripe_webhooks' => 'Creation des webhooks Stripe',
        'app_admin_profil' => 'Consultation du profil',
        'app_admin_order_number' => 'Consultation de la numerotation',
        'app_admin_order_number_update' => 'Modification de la numerotation',
        'app_admin_tarification' => 'Consultation de la tarification',
        'app_admin_tarification_edit' => 'Modification d\'un tarif',
        'app_admin_services_index' => 'Consultation des services',
        'app_admin_logs' => 'Consultation des logs',
        'app_admin_logs_purge' => 'Suppression de tous les logs',
        'app_admin_logs_delete' => 'Suppression d\'un log',
    ];

    public function __construct(
        private MessageBusInterface $bus,
        private EntityManagerInterface $em,
        #[Autowire('%env(APP_SECRET)%')] private string $hmacSecret,
    ) {
    }

    /**
     * Log asynchrone via Messenger.
     */
    public function log(string $method, string $url, string $route, ?User $user = null, ?string $ip = null): void
    {
        $action = self::ROUTE_LABELS[$route] ?? 'Acces a '.$route;

        if ('POST' === $method) {
            $action .= ' (soumission)';
        }

        $this->bus->dispatch(new AppLogMessage(
            $method,
            $url,
            $route,
            $action,
            $user?->getId(),
            $ip,
        ));
    }

    /**
     * Log synchrone direct (pour les suppressions qui doivent etre tracees immediatement).
     */
    public function logDirect(string $method, string $url, string $route, string $action, ?User $user = null, ?string $ip = null): void
    {
        $log = new AppLog($method, $url, $route, $action, $this->hmacSecret, $user, $ip);

        $this->em->persist($log);
        $this->em->flush();
    }

    public function verifyLog(AppLog $log): bool
    {
        return $log->verifyHmac($this->hmacSecret);
    }
}
