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

require_once('db_connection.php');

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

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

    // 2. Obtener el logo del negocio (Mantenemos el fallback por ser un elemento visual cr\u00edtico)
    $stmtBusiness = $pdo->query("SELECT logo_url FROM business_info WHERE id = 1");
    $businessData = $stmtBusiness->fetch(PDO::FETCH_ASSOC);

    if ($businessData === false) {
        $data['business'] = ['logo_url' => 'img/default-logo.png']; 
    } else {
        $data['business'] = $businessData;
    }
    
    // Asignaci\u00f3n robusta para datos del Hero
    if ($heroData === false) {
        // NOTA: Se elimin\u00f3 el bloque 'else' que conten\u00eda los datos por defecto.
        // Si no hay registro, 'hero' se devuelve como un array vac\u00edo.
        $data['hero'] = [];
        $data['links'] = [];
    } else {
        $data['hero'] = $heroData;
        $heroId = $heroData['id'];

        // Obtener enlaces de reseñas ordenados
        $stmtLinks = $pdo->prepare("SELECT * FROM cms_review_links WHERE hero_id = ? ORDER BY sort_order ASC");
        $stmtLinks->execute([$heroId]);
        $data['links'] = $stmtLinks->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-hero-read.php: " . $e->getMessage());
    echo json_encode(['success' => false, 'message' => 'Error Al Cargar Los Datos' . $e->getMessage()]);
}