<?php
// cms/schedule-read.php
header('Content-Type: application/json');

require_once('db_connection.php');

try {
    $data = [
        'general' => [],
        'hours' => [],
        'benefits' => []
    ];

    // Obtener datos generales de la sección
    $stmt = $pdo->query("SELECT * FROM cms_schedule ORDER BY updated_at DESC LIMIT 1");
    $generalData = $stmt->fetch(PDO::FETCH_ASSOC);

    if ($generalData) {
        $data['general'] = $generalData;
        $scheduleId = $generalData['id'];

        // Obtener los horarios semanales
        $stmtHours = $pdo->prepare("SELECT day, is_open, open_time, close_time FROM cms_weekly_hours WHERE schedule_id = ? ORDER BY id ASC");
        $stmtHours->execute([$scheduleId]);
        $data['hours'] = $stmtHours->fetchAll(PDO::FETCH_ASSOC);

        // Obtener la lista de beneficios
        $stmtBenefits = $pdo->prepare("SELECT benefit_icon, benefit_text FROM cms_schedule_benefits WHERE schedule_id = ? ORDER BY id ASC");
        $stmtBenefits->execute([$scheduleId]);
        $data['benefits'] = $stmtBenefits->fetchAll(PDO::FETCH_ASSOC);
    } 
    // NOTA: Se elimin\u00f3 el bloque 'else' que conten\u00eda los datos por defecto.
    // Si no hay registro, 'general', 'hours', y 'benefits' se devuelven como arrays vac\u00edos,
    // forzando al editor a inicializar los campos en blanco o con fallbacks del frontend.

    echo json_encode(['success' => true, 'data' => $data]);
} catch (PDOException $e) {
    http_response_code(500);
    echo json_encode(['success' => false, 'error' => 'Error Al Cargar Los Datos: ' . $e->getMessage()]);
}
?>