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

require_once('db_connection.php');

try {
    $data = [
        'general' => [],
        'reviews' => [],
        'links' => []
    ];

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

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

        // Obtener la lista de reseñas
        $stmtReviews = $pdo->prepare("SELECT review_avatar, review_name, review_rating, review_text FROM cms_reviews WHERE social_proof_id = ? ORDER BY id ASC");
        $stmtReviews->execute([$socialProofId]);
        $data['reviews'] = $stmtReviews->fetchAll(PDO::FETCH_ASSOC);

        // Obtener la lista de enlaces
        $stmtLinks = $pdo->prepare("SELECT link_platform, link_text, link_url FROM cms_external_links WHERE social_proof_id = ? ORDER BY id ASC");
        $stmtLinks->execute([$socialProofId]);
        $data['links'] = $stmtLinks->fetchAll(PDO::FETCH_ASSOC);
    } 
    // NOTA: Se elimin\u00f3 el bloque 'else' que conten\u00eda los datos por defecto.
    // Si $generalData es false, 'general', 'reviews' y 'links' se devuelven como arrays vac\u00edos.

    echo json_encode(['success' => true, 'data' => $data]);
} catch (PDOException $e) {
    http_response_code(500);
    // Mensaje de error m\u00e1s espec\u00edfico en caso de fallo en la BD
    echo json_encode(['success' => false, 'message' => 'Error de Base de Datos al leer Prueba Social: ' . $e->getMessage()]);
} catch (Exception $e) {
    http_response_code(500);
    echo json_encode(['success' => false, 'message' => 'Error Al Cargar Los Datos ' . $e->getMessage()]);
}
?>