<?php
declare(strict_types=1);

require_once __DIR__ . '/../config/constants.php';
require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../core/Response.php';

$db = Database::getInstance();

/**
 * ----------------------------
 * Fetch active sliders
 * ----------------------------
 */
$stmt = $db->prepare("
    SELECT
        id,
        title,
        subtitle,
        desktop_image_path,
        mobile_image_path,
        link_url,
        link_text,
        priority
    FROM home_page_sliders
    WHERE is_active = 1
      AND (start_date IS NULL OR start_date <= NOW())
      AND (end_date IS NULL OR end_date >= NOW())
    ORDER BY priority ASC
");

$stmt->execute();
$sliders = $stmt->fetchAll();

/**
 * ----------------------------
 * Format image URLs
 * ----------------------------
 */
foreach ($sliders as &$slider) {
    $slider['desktop_image_url'] = UPLOAD_BASE_URL . '/' . $slider['desktop_image_path'];
    $slider['mobile_image_url']  = UPLOAD_BASE_URL . '/' . $slider['mobile_image_path'];

    unset($slider['desktop_image_path'], $slider['mobile_image_path']);
}

/**
 * ----------------------------
 * Response
 * ----------------------------
 */
Response::success('Home sliders loaded', [
    'sliders' => $sliders
]);
