<?php

declare(strict_types=1);

namespace App\Controller\Admin;

use App\Entity\ModelGenerationConfig;
use App\Service\Admin\ModelGenerationConfigAdminService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

#[Route('/admin/model-config')]
final class ModelGenerationConfigController extends AbstractController
{
    #[Route('/', name: 'admin_model_config_list')]
    public function list(ModelGenerationConfigAdminService $svc): Response
    {
        $this->denyAccessUnlessGranted('ROLE_KNOWLEDGE_ADMIN');

        return $this->render('admin/model_config/list.html.twig', [
            'configs' => $svc->list(),
        ]);
    }

    #[Route('/create', name: 'admin_model_config_create', methods: ['GET', 'POST'])]
    public function create(
        Request $request,
        ModelGenerationConfigAdminService $svc
    ): Response {
        $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');

        if ($request->isMethod('POST')) {
            try {
                $svc->create($request->request->all());
                $this->addFlash('success', 'Konfiguration erstellt.');

                return $this->redirectToRoute('admin_model_config_list');
            } catch (\Throwable $e) {
                $this->addFlash('danger', $e->getMessage());
            }
        }

        return $this->render('admin/model_config/create.html.twig');
    }

    #[Route('/{id}/activate', name: 'admin_model_config_activate', methods: ['POST'])]
    public function activate(
        ModelGenerationConfig $config,
        ModelGenerationConfigAdminService $svc
    ): Response {
        $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');

        $svc->activate($config);

        return $this->redirectToRoute('admin_model_config_list');
    }

    #[Route('/{id}/test-retrieval', name: 'admin_model_config_test_retrieval', methods: ['GET', 'POST'])]
    public function testRetrieval(
        ModelGenerationConfig $config,
        Request $request,
        ModelGenerationConfigAdminService $svc
    ): Response {
        $this->denyAccessUnlessGranted('ROLE_KNOWLEDGE_ADMIN');

        $prompt = '';
        $results = [];

        if ($request->isMethod('POST')) {
            $prompt = trim((string)$request->request->get('prompt'));
            $results = $svc->testRetrieval($config, $prompt);
        }

        return $this->render('admin/model_config/test_retrieval.html.twig', [
            'config'  => $config,
            'results' => $results,
            'prompt'  => $prompt,
        ]);
    }

    #[Route('/{id}/delete', name: 'admin_model_config_delete', methods: ['POST'])]
    public function delete(
        ModelGenerationConfig $config,
        Request $request,
        ModelGenerationConfigAdminService $svc
    ): Response {
        $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');

        if (!$this->isCsrfTokenValid(
            'delete_model_config_'.$config->getId(),
            $request->request->get('_token')
        )) {
            throw $this->createAccessDeniedException();
        }

        try {
            $svc->delete($config);
            $this->addFlash('success', 'Konfiguration gelöscht.');
        } catch (\Throwable $e) {
            $this->addFlash('danger', $e->getMessage());
        }

        return $this->redirectToRoute('admin_model_config_list');
    }
}