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

require_once('db_connection.php');

try {
    $data = [
        'hero' => [],
        'items' => []
    ];

    // 1. Obtener datos de la sección principal (Textos y Fondo)
    $stmtHero = $pdo->query("SELECT * FROM cms_reviews_why WHERE id = 1");
    $heroData = $stmtHero->fetch(PDO::FETCH_ASSOC);

    // 2. Asignaci\u00f3n robusta y obtenci\u00f3n de \u00edtems
    if ($heroData) {
        $data['hero'] = $heroData;
        $whyId = $heroData['id'];

        // Obtener los \u00edtems ordenados
        $stmtItems = $pdo->prepare("SELECT * FROM cms_reviews_why_items WHERE why_id = ? ORDER BY sort_order ASC");
        $stmtItems->execute([$whyId]);
        $data['items'] = $stmtItems->fetchAll(PDO::FETCH_ASSOC);
    } 

    echo json_encode(['success' => true, 'data' => $data], JSON_UNESCAPED_UNICODE);

} catch (PDOException $e) {
    http_response_code(500);
    error_log("PDO Error in reviews-why-read.php: " . $e->getMessage());
    echo json_encode(['success' => false, 'message' => 'Error Al Cargar Los Datos']);
}
?>