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

require_once('db_connection.php');

try {
    $data = [
        'general' => [],
        'badges' => [],
        'business_contact' => [] // Array para datos de contacto
    ];

    // Obtener datos de contacto del negocio (business_info)
    $stmtBusiness = $pdo->query("SELECT phone_primary, phone_secondary, site_email FROM business_info LIMIT 1");
    $data['business_contact'] = $stmtBusiness->fetch(PDO::FETCH_ASSOC);

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

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

        // Obtener la lista de insignias de confianza
        $stmtBadges = $pdo->prepare("SELECT badge_icon, badge_text FROM cms_cta_badges WHERE cta_id = ? ORDER BY id ASC");
        $stmtBadges->execute([$ctaId]);
        $data['badges'] = $stmtBadges->fetchAll(PDO::FETCH_ASSOC);
    } 

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