<?php
session_start();
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'admin') {
    header("Location: ../auth/login.php");
    exit();
}

require_once __DIR__ . '/../../controllers/CustomerController.php';
require_once __DIR__ . '/../../controllers/NotificationController.php';

$customerController = new CustomerController();
$inactiveCustomers = $customerController->getInactiveCustomers();

if (isset($_GET['action']) && $_GET['action'] == 'send_promo') {
    $notificationController = new NotificationController();
    $notificationController->sendWhatsAppNotification($_GET['customer_id'], '¡Te extrañamos! Vuelve y recibe una promoción especial.');
    header("Location: inactive_customers.php");
    exit();
}
?>
<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <title>Clientes Inactivos</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <div class="container">
        <h1 class="mt-3">Clientes Inactivos</h1>
        <table class="table">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Teléfono</th>
                    <th>Puntos</th>
                    <th>Última Compra</th>
                    <th>Acciones</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($inactiveCustomers as $customer): ?>
                    <tr>
                        <td><?php echo $customer['id']; ?></td>
                        <td><?php echo $customer['phone']; ?></td>
                        <td><?php echo $customer['points']; ?></td>
                        <td><?php echo $customer['last_purchase_date']; ?></td>
                        <td><a href="?action=send_promo&customer_id=<?php echo $customer['id']; ?>" class="btn btn-primary btn-sm">Enviar Promoción</a></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
        <a href="dashboard.php" class="btn btn-primary">Volver al Dashboard</a>
    </div>
</body>
</html>