<?php

declare(strict_types=1);

namespace App\Controller;

use App\Repository\AuditLogRepository;
use App\Repository\ComposantRepository;
use App\Repository\MachineRepository;
use App\Repository\PieceRepository;
use App\Repository\ProductRepository;
use App\Repository\ProfileRepository;
use DateTimeInterface;
use Doctrine\ORM\EntityRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

final class EntityHistoryController extends AbstractController
{
    /** @var array<string, array{repo: EntityRepository<object>, label: string}> */
    private readonly array $entityConfig;

    public function __construct(
        MachineRepository $machines,
        PieceRepository $pieces,
        ComposantRepository $composants,
        ProductRepository $products,
        private readonly AuditLogRepository $auditLogs,
        private readonly ProfileRepository $profiles,
    ) {
        $this->entityConfig = [
            'machine'   => ['repo' => $machines, 'label' => 'Machine introuvable.'],
            'piece'     => ['repo' => $pieces, 'label' => 'Pièce introuvable.'],
            'composant' => ['repo' => $composants, 'label' => 'Composant introuvable.'],
            'product'   => ['repo' => $products, 'label' => 'Produit introuvable.'],
        ];
    }

    #[Route('/api/machines/{id}/history', name: 'api_machine_history', methods: ['GET'])]
    public function machineHistory(string $id): JsonResponse
    {
        return $this->entityHistory('machine', $id);
    }

    #[Route('/api/pieces/{id}/history', name: 'api_piece_history', methods: ['GET'])]
    public function pieceHistory(string $id): JsonResponse
    {
        return $this->entityHistory('piece', $id);
    }

    #[Route('/api/composants/{id}/history', name: 'api_composant_history', methods: ['GET'])]
    public function composantHistory(string $id): JsonResponse
    {
        return $this->entityHistory('composant', $id);
    }

    #[Route('/api/products/{id}/history', name: 'api_product_history', methods: ['GET'])]
    public function productHistory(string $id): JsonResponse
    {
        return $this->entityHistory('product', $id);
    }

    private function entityHistory(string $type, string $id): JsonResponse
    {
        $this->denyAccessUnlessGranted('ROLE_VIEWER');

        $config = $this->entityConfig[$type];
        $entity = $config['repo']->find($id);
        if (!$entity) {
            return new JsonResponse(
                ['message' => $config['label']],
                Response::HTTP_NOT_FOUND,
            );
        }

        $logs = $this->auditLogs->findEntityHistory($type, $id, 200);

        $actorIds = array_values(array_unique(array_filter(array_map(
            static fn ($log) => $log->getActorProfileId(),
            $logs,
        ))));

        $actorMap = [];
        if ([] !== $actorIds) {
            $profiles = $this->profiles->findBy(['id' => $actorIds]);
            foreach ($profiles as $profile) {
                $label = trim(sprintf('%s %s', $profile->getFirstName(), $profile->getLastName()));
                if ('' === $label) {
                    $label = $profile->getEmail() ?? $profile->getId();
                }
                $actorMap[$profile->getId()] = $label;
            }
        }

        $items = array_map(
            static function ($log) use ($actorMap) {
                $actorId = $log->getActorProfileId();

                return [
                    'id'        => $log->getId(),
                    'action'    => $log->getAction(),
                    'createdAt' => $log->getCreatedAt()->format(DateTimeInterface::ATOM),
                    'actor'     => $actorId
                        ? [
                            'id'    => $actorId,
                            'label' => $actorMap[$actorId] ?? $actorId,
                        ]
                        : null,
                    'diff'     => $log->getDiff(),
                    'snapshot' => $log->getSnapshot(),
                ];
            },
            $logs,
        );

        return new JsonResponse([
            'items' => array_values($items),
            'total' => count($items),
        ]);
    }
}
