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

require_once('db_connection.php');

try {
    $data = [
        'hero' => [],
        'badges' => [],
        'links' => [] // Recuperamos los enlaces de la tabla Reviews Hero
    ];

    // 1. Obtener datos de la sección principal (Reviews CTA)
    $stmtHero = $pdo->query("SELECT * FROM cms_reviews_cta WHERE id = 1");
    $data['hero'] = $stmtHero->fetch(PDO::FETCH_ASSOC);

    // 2. Obtener los Badges inferiores ordenados
    $stmtBadges = $pdo->query("SELECT * FROM cms_reviews_cta_badges WHERE cta_id = 1 ORDER BY sort_order ASC");
    $data['badges'] = $stmtBadges->fetchAll(PDO::FETCH_ASSOC);
    
    // 3. Obtener los enlaces de reseña de la tabla Reviews Hero (Reutilización de datos)
    $stmtLinks = $pdo->query("SELECT platform_name, link_url, button_text, button_color, icon_lucide FROM cms_review_links WHERE hero_id = 1 ORDER BY sort_order ASC");
    $data['links'] = $stmtLinks->fetchAll(PDO::FETCH_ASSOC);

    // Fallback si no hay datos
    if ($data['hero'] === false) {
        $data['hero'] = [];
    }

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

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