<?php
header('Content-Type: application/json; charset=UTF-8');

if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    echo json_encode(['success' => false, 'message' => 'ID faltante o inválido']);
    exit;
}

try {
    $host = 'localhost';
    $dbname = 'CORREOS';
    $user = 'root';
    $pass = 'root';

    $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8mb4", $user, $pass, [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
    ]);

    $stmt = $pdo->prepare("SELECT * FROM CORREO WHERE ID_CORREO = ?");
    $stmt->execute([$_GET['id']]);
    $correo = $stmt->fetch(PDO::FETCH_ASSOC);

    if (!$correo) {
        echo json_encode(['success' => false, 'message' => 'Correo no encontrado']);
    } else {
        echo json_encode(['success' => true, 'correo' => $correo]);
    }

} catch (PDOException $e) {
    http_response_code(500); // opcional, mejora para frontend
    echo json_encode(['success' => false, 'message' => $e->getMessage()]);
}
