<?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();

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

if ($action === 'import_warranties') {
    $count = $reminderService->importWarrantyReminders($_SESSION['user_id']);
    header('Location: index.php?success=' . urlencode("Imported {$count} warranty reminder(s)."));
    exit;
}

if ($action === 'send_due') {
    $count = $reminderService->sendDueNotifications();
    header('Location: index.php?success=' . urlencode("Sent {$count} due reminder notification(s)."));
    exit;
}

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