<?php
require_once __DIR__ . '/config/bootstrap.php';

$token = Validator::clean((string)($_GET['token'] ?? ''), 128);

// Quick token sanity (full validation on submit)
$tokenValid = ($token !== '' && strlen($token) === 64 && ctype_xdigit($token));
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Reset Password — DroidTester</title>
    <meta name="robots" content="noindex,nofollow">
    <meta name="theme-color" content="#1B7A43">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css" rel="stylesheet">
    <link rel="stylesheet" href="/assets/css/style.css">
    <link rel="stylesheet" href="/assets/css/landing.css">
    <link rel="stylesheet" href="/assets/css/auth.css">
    <link rel="stylesheet" href="/assets/css/responsive.css">
</head>
<body class="auth-page">

    <nav class="navbar navbar-expand-lg public-navbar">
        <div class="container">
            <a class="navbar-brand-custom" href="/">
                <div class="navbar-logo-icon"><i class="bi bi-gear-fill"></i></div>
                DroidTester
            </a>
        </div>
    </nav>

    <div class="auth-content">
        <div class="auth-container" style="max-width:520px;margin:0 auto">
            <?php if (!$tokenValid): ?>
                <div class="text-center" style="padding:40px 20px">
                    <div style="font-size:64px;margin-bottom:12px">⚠️</div>
                    <h2 style="color:var(--danger);font-weight:700">Invalid Reset Link</h2>
                    <p class="text-muted">This reset link is malformed or has expired.</p>
                    <a href="/forgot-password" class="auth-btn-main d-inline-flex" style="margin-top:20px;width:auto;padding:12px 28px">
                        Request a new link <i class="bi bi-arrow-right"></i>
                    </a>
                </div>
            <?php else: ?>
                <div class="auth-badge text-center">
                    <span class="badge-green"><i class="bi bi-key-fill" style="color:#FFD700"></i> Set New Password</span>
                </div>
                <h1 class="auth-title text-center">Reset Your Password</h1>
                <p class="auth-subtitle text-center mx-auto">Choose a strong password you haven't used before.</p>

                <div class="auth-form-col mt-4">
                    <form id="resetForm" autocomplete="off">
                        <input type="hidden" name="token" value="<?= e($token) ?>">
                        <div class="auth-input-pill">
                            <div class="input-icon-circle"><i class="bi bi-lock-fill"></i></div>
                            <input type="password" name="password" id="resetPwd" placeholder="New Password" required minlength="8" autocomplete="new-password">
                        </div>
                        <div class="auth-input-pill">
                            <div class="input-icon-circle"><i class="bi bi-shield-lock-fill"></i></div>
                            <input type="password" name="confirm_password" id="resetConf" placeholder="Confirm New Password" required autocomplete="new-password">
                        </div>
                        <button type="submit" class="auth-btn-main">Update Password <i class="bi bi-check-circle-fill ms-2"></i></button>
                    </form>
                    <div class="forgot-password-link mt-4 text-center">
                        <a href="/login"><i class="bi bi-arrow-left"></i> Back to Login</a>
                    </div>
                </div>
            <?php endif; ?>
        </div>
    </div>

    <div class="auth-mini-footer">
        <div>
            <a href="/privacy-policy">Privacy Policy</a>
            <span class="separator">|</span>
            <a href="/terms-condition">Terms &amp; Condition</a>
        </div>
        <span class="copyright">© <?= date('Y') ?> DroidTester | Zodroid Media</span>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
    <?php if ($tokenValid): ?>
    <script>
    const Toast = Swal.mixin({
        toast: true, position: 'top-end', showConfirmButton: false,
        timer: 3500, timerProgressBar: true
    });

    document.getElementById('resetForm').addEventListener('submit', async function(e) {
        e.preventDefault();
        const pwd = document.getElementById('resetPwd').value;
        const conf = document.getElementById('resetConf').value;
        if (pwd !== conf) {
            Toast.fire({ icon: 'error', title: 'Passwords do not match.' });
            return;
        }

        const btn = this.querySelector('button[type="submit"]');
        const orig = btn.innerHTML;
        btn.disabled = true;
        btn.innerHTML = '<span class="spinner-border spinner-border-sm"></span> Updating...';

        try {
            const fd = new FormData(this);
            const res = await fetch('/api/auth/reset', { method: 'POST', body: fd });
            const data = await res.json();
            if (data.status === 'success') {
                Swal.fire({
                    title: 'Password Updated',
                    text: data.message,
                    icon: 'success',
                    confirmButtonColor: '#1B7A43',
                    confirmButtonText: 'Login Now'
                }).then(() => { window.location.href = (data.data && data.data.redirect) || '/login'; });
            } else {
                Toast.fire({ icon: 'error', title: data.message });
                btn.disabled = false;
                btn.innerHTML = orig;
            }
        } catch {
            Toast.fire({ icon: 'error', title: 'Network error.' });
            btn.disabled = false;
            btn.innerHTML = orig;
        }
    });
    </script>
    <?php endif; ?>
</body>
</html>
