<?php
require_once __DIR__ . '/../../../config/bootstrap.php';
require_once __DIR__ . '/../../../includes/require-auth.php';

use App\Services\AuthService;
use App\Services\TemplateService;

header('Content-Type: application/json');

$auth = new AuthService();
$templateService = new TemplateService();

// Obtener usuario autenticado
$user = $auth->user();
if (!$user) {
    http_response_code(401);
    echo json_encode(['success' => false, 'message' => 'No autenticado']);
    exit;
}

// Verificar permisos de administrador
if ($user['rol'] !== 'administrador') {
    http_response_code(403);
    echo json_encode(['success' => false, 'message' => 'No autorizado']);
    exit;
}

if ($_SERVER['REQUEST_METHOD'] !== 'DELETE') {
    http_response_code(405);
    echo json_encode(['success' => false, 'message' => 'Método no permitido']);
    exit;
}

$templateId = (int)($_GET['id'] ?? 0);

if ($templateId <= 0) {
    echo json_encode(['success' => false, 'message' => 'ID de plantilla inválido']);
    exit;
}

// Eliminar permanentemente
$result = $templateService->permanentDelete($templateId);

http_response_code($result['success'] ? 200 : 400);
echo json_encode($result);
