<?php
header("Content-Type: application/json");
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST, GET");
header("Access-Control-Allow-Headers: Content-Type");

$imap_server = "zeus.moshipp.com";
$imap_user = "bancolapi@andresplat.com.co";
$imap_password = "altamar599";

$input_data = file_get_contents("php://input");
$post_data = json_decode($input_data, true);

$uid = null;

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($post_data['uid'])) {
    $uid = $post_data['uid'];
} elseif ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['uid'])) {
    $uid = $_GET['uid'];
}

if (!$uid) {
    echo json_encode(["status" => "error", "message" => "UID no proporcionado"]);
    exit();
}

$inbox = imap_open("{".$imap_server.":993/imap/ssl}INBOX", $imap_user, $imap_password);

if (!$inbox) {
    echo json_encode([
        "status" => "error",
        "message" => "No se pudo conectar al correo",
        "imap_error" => imap_last_error()
    ]);
    exit();
}

// Intentar eliminar
if (!imap_delete($inbox, $uid, FT_UID)) {
    echo json_encode([
        "status" => "error",
        "message" => "No se pudo marcar el correo para eliminación",
        "imap_error" => imap_last_error()
    ]);
    imap_close($inbox);
    exit();
}

imap_expunge($inbox);
imap_close($inbox);

echo json_encode(["status" => "success", "message" => "Correo eliminado correctamente"]);

