<?php
declare(strict_types=1);

/* ================= AUTH (API SAFE) ================= */
require_once __DIR__ . '/../../security/auth_guard.php';
require_once __DIR__ . '/../../config/database.php';
require_once __DIR__ . '/../../core/Response.php';

$db = Database::getInstance();

/* ================= FETCH ALL SECTIONS ================= */
$stmt = $db->prepare("
    SELECT
        s.id,
        s.product_id,
        p.product_name,
        s.section_title,
        s.section_key,
        s.file_type,
        s.file_path,
        s.thumbnail_path,
        s.priority,
        s.status,
        s.approval_status,
        s.created_at
    FROM retail_product_sections s
    INNER JOIN retail_products p
        ON p.id = s.product_id
    ORDER BY
        p.product_name ASC,
        s.priority ASC,
        s.id DESC
");

$stmt->execute();

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

/* ================= RESPONSE ================= */
Response::success('Sections fetched successfully', [
    'sections' => $sections
]);
