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

use App\Services\AuthService;
use App\Services\ContractService;
use App\Services\SecurityService;

$auth       = new AuthService();
$contractService = new ContractService();
$security   = new SecurityService();

$user = requireAuth($auth);
$rol = $user['rol'] ?? 'asesor';

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

if ($contractId <= 0) {
    header('Location: ' . base_url('admin/dashboard'));
    exit;
}

$result = $contractService->cancel($contractId, $user['id'], $rol);

if ($result['success']) {
    header('Location: ' . base_url("admin/ver-contrato") . "?id={$contractId}&cancelled=1");
} else {
    header('Location: ' . base_url("admin/ver-contrato") . "?id={$contractId}&error=" . urlencode($result['message']));
}
exit;
