<?php require_once __DIR__ . "/api/_lib.php"; $cfg = illume_init();
$email = strtolower(trim($_GET["email"] ?? ""));
if ($email && filter_var($email, FILTER_VALIDATE_EMAIL)) {
    $ud_path = $cfg["data_dir"]."/unsubscribed.json";
    $ud = illume_load($ud_path) ?: [];
    if (!in_array($email, $ud)) { $ud[] = $email; illume_save($ud_path, $ud); }
    $sub_path = $cfg["data_dir"]."/subscribers.json";
    $subs = illume_load($sub_path) ?: [];
    $changed = false;
    foreach ($subs as $i => $row) {
        $rowEmail = is_array($row) ? strtolower((string)($row["email"] ?? "")) : strtolower((string)$row);
        if ($rowEmail !== $email) continue;
        if (!is_array($row)) $row = ["email" => $rowEmail];
        $row["unsubscribed"] = true;
        $row["unsubscribed_at"] = illume_now();
        $subs[$i] = $row;
        $changed = true;
    }
    if ($changed) illume_save($sub_path, $subs);
} ?><!doctype html><html><head><meta charset="utf-8"><title>Unsubscribed</title></head><body style="background:#050505;color:#fff;font-family:JetBrains Mono,monospace;text-align:center;padding:60px 20px"><h1 style="color:#39ff14">Unsubscribed</h1><p>You will no longer receive marketing emails<?php if ($email) echo " at <b>".htmlspecialchars($email)."</b>"; ?>.</p><p style="color:#a1a1aa;margin-top:24px;font-size:13px">Changed your mind? Reply to any of our previous emails and we will add you back.</p></body></html>