<?php
/**
 * Reactiva un cliente previamente desactivado por soft delete.
 */
require_once '../../core/config.php';
require_once '../../core/session.php';
require_once '../../core/permissions.php';
require_once '../../core/database.php';
require_once '../../services/crm/CustomerLifecycleService.php';

require_login();
require_permission('module.customers');

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    header('Location: /modules/customers/index.php');
    exit;
}

$id = isset($_POST['id']) ? (int) $_POST['id'] : 0;

if ($id <= 0) {
    die('ID de cliente inválido.');
}

try {
    $user = current_user();
    $userName = $user['name'] ?? ($user['email'] ?? 'Sistema');

    $lifecycle = new CustomerLifecycleService(db());
    $lifecycle->restore($id, $userName);

    header('Location: /modules/customers/view.php?id=' . $id . '&restored=1');
    exit;
} catch (Throwable $e) {
    die('Error al reactivar el cliente: ' . $e->getMessage());
}
