<?php
require_once __DIR__ . '/../../connect.php';

$promo_code = filterRequest("promo_code");

if (!empty($promo_code)) {
    $sql = "SELECT `id`, `promo_code`, `amount`, `description`, `passengerID`, `validity_start_date`, `validity_end_date` 
            FROM `promos` 
            WHERE `promo_code` = :promo_code 
            AND CURDATE() BETWEEN validity_start_date AND validity_end_date";
    $stmt = $con->prepare($sql);
    $stmt->bindParam(':promo_code', $promo_code, PDO::PARAM_STR);
} else {
    $sql = "SELECT `id`, `promo_code`, `amount`, `description`, `passengerID`, `validity_start_date`, `validity_end_date` FROM `promos` WHERE `passengerID` IN ('all', 'none', '') ORDER BY id DESC";
    $stmt = $con->prepare($sql);
}

$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);

if ($result || (empty($promo_code) && is_array($result))) {
    jsonSuccess($result);
} else {
    if (!empty($promo_code)) {
        jsonError("Promo code not found or expired");
    } else {
        jsonSuccess([], "No promos found");
    }
}
?>