<?php
declare(strict_types=1);

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

$db = Database::getInstance();

/**
 * ----------------------------
 * Fetch sliders
 * ----------------------------
 */
$stmt = $db->prepare("
    SELECT
        id,
        title,
        subtitle,
        desktop_image_path,
        mobile_image_path,
        priority,
        link_url,
        link_text,
        is_active,
        start_date,
        end_date,
        created_at
    FROM home_page_sliders
    ORDER BY priority ASC, id DESC
");

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

/**
 * ----------------------------
 * Normalize response
 * ----------------------------
 */
foreach ($sliders as &$slider) {
    // DB already contains full URLs
    $slider['desktop_image_url'] = $slider['desktop_image_path'];
    $slider['mobile_image_url']  = $slider['mobile_image_path'];

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

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