<?php
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

require_once __DIR__ . '/../../classes/Database.php';
require_once __DIR__ . '/../../includes/auth.php';
require_once __DIR__ . '/../../classes/RenewalReminder.php';
require_once __DIR__ . '/../../classes/Logger.php';

$database = new Database();
$conn     = $database->getConnection();
$auth     = new Auth($conn);
$auth->requireLogin();
$auth->enforceSuperAdminPrivileges($_SESSION['user_id']);

$logger          = new Logger($conn, $_SESSION['user_id'], $_SESSION['username'], $_SESSION['role']);
$reminderService = new RenewalReminder($conn, $logger);

$action = $_GET['action'] ?? '';

if ($action === 'send_alerts') {
    $count = $reminderService->sendContractExpiryAlerts(30);
    header('Location: contract_expiry.php?alerts_sent=' . $count);
    exit;
}

header('Location: contract_expiry.php');
exit;
