<?php
declare(strict_types=1);

$DATA_DIR = __DIR__ . '/data';
$SUB_FILE = $DATA_DIR . '/newsletter-subscribers.json';
$UNSUB_FILE = $DATA_DIR . '/newsletter-unsubscribed.json';
if (!is_dir($DATA_DIR)) { @mkdir($DATA_DIR, 0755, true); }
if (!file_exists($SUB_FILE)) { @file_put_contents($SUB_FILE, '{}'); }
if (!file_exists($UNSUB_FILE)) { @file_put_contents($UNSUB_FILE, '{}'); }

function ntr_h(string $s): string { return htmlspecialchars($s, ENT_QUOTES, 'UTF-8'); }
function ntr_json_read(string $file): array { $data = json_decode(@file_get_contents($file) ?: '{}', true); return is_array($data) ? $data : []; }
function ntr_json_write(string $file, array $data): void { @file_put_contents($file, json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT), LOCK_EX); }
function ntr_lang(): string { $ref = $_SERVER['HTTP_REFERER'] ?? ''; return (strpos($ref, '/eng/') !== false || strpos($ref, '/en/') !== false) ? 'en' : 'el'; }
function ntr_page(string $titleEl, string $titleEn, string $messageEl, string $messageEn, string $extra = ''): void {
    $en = ntr_lang() === 'en'; $title = $en ? $titleEn : $titleEl; $message = $en ? $messageEn : $messageEl; $home = $en ? '/eng/' : '/el/'; $homeLabel = $en ? 'Back to home' : 'Επιστροφή στην αρχική';
    echo '<!doctype html><html lang="'.($en?'en':'el').'"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>'.ntr_h($title).'</title><style>body{margin:0;background:radial-gradient(circle at 50% 0,#3b0907,#120304 55%,#070101);color:#fff2e8;font-family:Arial,Helvetica,sans-serif;padding:40px 18px;min-height:100vh;box-sizing:border-box}.card{max-width:760px;margin:40px auto;background:linear-gradient(180deg,#350807,#160303);border-radius:28px;padding:34px;border:1px solid rgba(255,255,255,.12);box-shadow:0 28px 80px rgba(0,0,0,.35)}.logo{width:72px;height:72px;border-radius:20px;background:linear-gradient(135deg,#d71920,#ff6d55);display:flex;align-items:center;justify-content:center;font-size:36px;font-weight:900;margin-bottom:18px}h1{font-size:clamp(34px,8vw,46px);line-height:1.05;margin:0 0 16px}p{font-size:19px;line-height:1.55;color:#ead8cf}.btn{display:inline-block;margin-top:16px;background:linear-gradient(135deg,#d82029,#ff705c);color:#fff;text-decoration:none;border-radius:999px;padding:15px 24px;font-weight:900}.btn.alt{background:#241614}input{width:100%;min-height:50px;border-radius:14px;border:0;padding:0 14px;font-size:16px;box-sizing:border-box}button{margin-top:12px;border:0;border-radius:999px;background:#d82029;color:#fff;padding:14px 22px;font-weight:900;font-size:16px}</style></head><body><div class="card"><div class="logo">N</div><h1>'.ntr_h($title).'</h1><p>'.nl2br(ntr_h($message)).'</p>'.$extra.'<a class="btn" href="'.$home.'">'.$homeLabel.'</a></div></body></html>'; exit;
}

$email = filter_var(trim((string)($_POST['email'] ?? $_GET['email'] ?? '')), FILTER_SANITIZE_EMAIL);
if ($_SERVER['REQUEST_METHOD'] !== 'POST' && (!$email || !filter_var($email, FILTER_VALIDATE_EMAIL))) {
    $form = '<form method="post" action="/newsletter-unsubscribe.php"><input type="email" name="email" placeholder="Email" required><button type="submit">Unsubscribe / Απεγγραφή</button></form>';
    ntr_page('Απεγγραφή newsletter', 'Newsletter unsubscribe', 'Συμπλήρωσε το email σου για απεγγραφή από το newsletter.', 'Enter your email to unsubscribe from the newsletter.', $form);
}
if (!$email || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
    ntr_page('Λάθος email', 'Invalid email', 'Παρακαλώ συμπλήρωσε σωστό email.', 'Please enter a valid email.');
}
$key = strtolower($email);
$subs = ntr_json_read($SUB_FILE);
$unsubs = ntr_json_read($UNSUB_FILE);
unset($subs[$key]);
$unsubs[$key] = ['email'=>$email,'unsubscribed_at'=>date('c'),'ip'=>$_SERVER['REMOTE_ADDR'] ?? ''];
ntr_json_write($SUB_FILE, $subs);
ntr_json_write($UNSUB_FILE, $unsubs);
ntr_page('Η απεγγραφή ολοκληρώθηκε', 'Unsubscribed', 'Το email αφαιρέθηκε από τη λίστα newsletter.', 'This email has been removed from the newsletter list.');
