<?php
declare(strict_types=1);

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

try {

    $db = Database::getInstance();

    $stmt = $db->prepare("
        SELECT
            s.id,
            s.hospitality_product_id,
            s.section_title,
            s.section_key,
            s.short_description,
            s.description,
            s.file_type,
            s.preview_type,
            s.file_path,
            s.thumbnail_path,
            s.priority,
            s.status,
            s.approval_status,
            s.created_at,
            p.title AS product_title
        FROM hospitality_product_sections s
        INNER JOIN hospitality_products p
            ON p.id = s.hospitality_product_id
        ORDER BY s.priority ASC, s.id DESC
    ");

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

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

} catch (Throwable $e) {
    Response::error('Server error: ' . $e->getMessage(), 500);
}
