<?php

declare(strict_types=1);

header('Content-Type: text/html; charset=UTF-8');

require_once __DIR__ . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'app.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'fate_public_session.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'fate_public_member_auth.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'fate_member_email.php';

fate_public_session_start();

if (fate_public_member_current() !== null) {
    header('Location: ./member_profile.php', true, 302);
    exit;
}

$pdo = null;
try {
    require_once __DIR__ . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'pdo.php';
    if (isset($GLOBALS['pdo']) && $GLOBALS['pdo'] instanceof PDO) {
        $pdo = $GLOBALS['pdo'];
    }
} catch (Throwable) {
}

$token = isset($_GET['token']) && is_string($_GET['token']) ? trim($_GET['token']) : '';
$state = 'empty';
$detailMsg = '';

if (!$pdo instanceof PDO) {
    $state = 'error';
    $detailMsg = '無法連線資料庫，請稍後再試。';
} elseif ($token === '') {
    $state = 'empty';
    $detailMsg = '請從 Email 中的驗證連結進入此頁。';
} else {
    $vr = fate_member_verify_email_with_token($pdo, $token);
    if ($vr['ok'] && is_array($vr['member'])) {
        require_once __DIR__ . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'fate_member_session_bridge.php';
        fate_session_assign_cookie_lifetime(fate_session_lifetime_sec());
        fate_public_member_bind_session($vr['member'], true);
        require_once __DIR__ . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'fate_member_profile.php';
        fate_member_profile_hydrate_session($pdo, (int) $vr['member']['id']);
        $_SESSION['fate_email_just_verified'] = true;
        header('Location: ./member_profile.php', true, 302);
        exit;
    }
    $state = 'error';
    $detailMsg = (string) ($vr['detail'] ?? '驗證失敗');
}

?>
<!DOCTYPE html>
<html lang="zh-Hant">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>FATE — Email 驗證</title>
    <?php
    require_once __DIR__ . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'fate_seo.php';
    fate_seo_render(fate_seo_private_page_context(), 'FATE — Email 驗證');
    ?>
    <style>
        :root { --bg:#0c0e12; --fg:#e8eaef; --muted:#8b919d; --accent:#79c0ff; --line:rgba(255,255,255,.08); --radius:10px; --bad:#f85149; }
        * { box-sizing:border-box; margin:0; padding:0; }
        body { font-family:"Segoe UI","Noto Sans TC",system-ui,sans-serif; background:var(--bg); color:var(--fg); min-height:100vh; padding:2rem 1rem; }
        .wrap { max-width:26rem; margin:0 auto; }
        h1 { font-size:1.2rem; margin-bottom:.5rem; }
        .sub { color:var(--muted); font-size:.88rem; margin-bottom:1rem; line-height:1.65; }
        .msg.bad { padding:.55rem .75rem; border-radius:8px; border:1px solid var(--line); color:var(--bad); background:rgba(248,81,73,.08); font-size:.85rem; margin-bottom:.75rem; line-height:1.55;}
        .btn { display:inline-block; margin-top:.5rem; padding:.5rem 1rem; border-radius:8px; border:1px solid var(--line); background:rgba(121,192,255,.15); color:var(--accent); text-decoration:none; font-size:.9rem; }
        p.back { margin-top:1rem; font-size:.85rem; }
        p.back a { color:var(--accent); }
    </style>
    <?php
    require_once __DIR__ . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'fate_mobile_member_bar.php';
    echo fate_mobile_frontend_stylesheet_tags('./');
    ?>
</head>
<body>
    <div class="wrap">
        <h1>Email 驗證</h1>
        <?php if ($state === 'error' && $detailMsg !== ''): ?>
            <div class="msg bad"><?= htmlspecialchars($detailMsg, ENT_QUOTES, 'UTF-8') ?></div>
            <p class="sub">連結可能已過期或已使用過。請<a href="login.php?next=<?= rawurlencode('./member_profile.php') ?>">登入</a>後至「我的帳號」輸入信內 <strong>6 位驗證碼</strong>，或重寄驗證信。</p>
            <a class="btn" href="login.php?next=<?= rawurlencode('./member_profile.php') ?>">登入／我的帳號</a>
        <?php elseif ($state === 'empty'): ?>
            <p class="sub"><?= htmlspecialchars($detailMsg, ENT_QUOTES, 'UTF-8') ?></p>
            <a class="btn" href="login.php?next=<?= rawurlencode('./member_profile.php') ?>">登入後重寄驗證信</a>
        <?php endif; ?>
        <p class="back"><a href="login.php">返回登入</a> · <a href="./">首頁</a></p>
    </div>
    <?php fate_mobile_member_bar_emit_footer(['urlPrefix' => './', 'loginNext' => './member_profile.php']); ?>
</body>
</html>
