<?php
require_once __DIR__ . '/../core/response.php';
require_once __DIR__ . '/../core/db.php';

$lang = $_GET['lang'] ?? 'id';

$basePath = returnBasePath();

$slides = fetchAll("
    SELECT
        s.IDSlides AS id,
        s.header,
        s.header_en,
        s.description,
        s.description_en,
        CASE 
            WHEN a.FileName IS NOT NULL 
            THEN CONCAT(:basePath, a.Source, '/', a.FileName)
            ELSE NULL
        END AS imagePath,
        s.HideHeaderText,
        s.HideDescriptionText
    FROM slides s
    LEFT JOIN attachment a 
        ON a.IDSource = s.IDSlides
        AND a.Source = 'slides'
        AND a.TipeMedia = 'image'
        AND a.Urutan = 1
    WHERE s.StatusSlides = 1
    ORDER BY s.sort_order ASC
", [
    'basePath' => $basePath
]);

foreach ($slides as &$slide) {

    if ($lang === 'en') {
        // header EN fallback ke ID
        if (!empty($slide['header_en'])) {
            $slide['header'] = $slide['header_en'];
        }

        // description EN fallback ke ID
        if (!empty($slide['description_en'])) {
            $slide['description'] = $slide['description_en'];
        }
    }

    // bersihkan field internal
    unset(
        $slide['header_en'],
        $slide['description_en']
    );
}
unset($slide);

jsonResponse($slides);
