<?php
require_once 'functions.php';

if (isLoggedIn()) {
    header('Location: dashboard.php');
    exit();
}

$error = '';
$success = '';

$token = $_GET['token'] ?? '';

if (empty($token)) {
    header('Location: o/forgot-password.php');
    exit();
}

$db = getDB();

// Validate token from password_resets table
$stmt = $db->prepare("SELECT * FROM password_resets WHERE token = ? AND used = 0 AND expires_at > NOW()");
$stmt->bind_param('s', $token);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows !== 1) {
    $error = 'Invalid or expired reset link. Please request a new one.';
} else {
    $reset = $result->fetch_assoc();
    $resetLink = BASE_URL . 'o/reset-password.php?token=' . $token;

    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        $password = $_POST['password'] ?? '';
        $confirm_password = $_POST['confirm_password'] ?? '';
        
        if (strlen($password) < 8) {
            $error = 'Password must be at least 8 characters long.';
        } elseif ($password !== $confirm_password) {
            $error = 'Passwords do not match.';
        } else {
            $hashed = password_hash($password, PASSWORD_BCRYPT);
            
            $stmt = $db->prepare("UPDATE users SET password = ? WHERE email = ?");
            $stmt->bind_param('ss', $hashed, $reset['email']);
            $stmt->execute();
            
            $stmt = $db->prepare("UPDATE password_resets SET used = 1 WHERE id = ?");
            $stmt->bind_param('i', $reset['id']);
            $stmt->execute();
            
            $success = 'Password reset successful! You can now log in with your new password.';
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>Reset Password - <?php echo SITE_NAME; ?></title>
    <link rel="stylesheet" href="assets/css/style.css">
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        html, body { height: 100%; width: 100%; overflow-x: hidden; }
        
        body.login-page {
            min-height: 100vh;
            min-height: 100dvh;
            width: 100vw;
            display: flex;
            align-items: center;
            justify-content: center;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            padding: 16px;
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
        }
        
        .login-container {
            width: 100%;
            max-width: 420px;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        
        .login-card {
            width: 100%;
            background: #ffffff;
            border-radius: 16px;
            box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
            padding: 32px 28px;
            animation: slideUp 0.4s ease-out;
        }
        
        @keyframes slideUp {
            from { opacity: 0; transform: translateY(30px); }
            to { opacity: 1; transform: translateY(0); }
        }
        
        .login-logo { text-align: center; margin-bottom: 28px; }
        .login-logo img { max-width: 80px; height: auto; margin-bottom: 12px; }
        .login-logo h1 { font-size: 24px; font-weight: 700; color: #1a202c; margin: 0; }
        
        .alert {
            padding: 14px 16px;
            border-radius: 10px;
            margin-bottom: 20px;
            font-size: 14px;
            line-height: 1.5;
            display: flex;
            align-items: flex-start;
            gap: 10px;
        }
        .alert-danger { background: #fef2f2; border: 1px solid #fecaca; color: #dc2626; }
        .alert-success { background: #f0fdf4; border: 1px solid #bbf7d0; color: #16a34a; }
        .alert span { font-size: 18px; flex-shrink: 0; line-height: 1; }
        
        .form-group { margin-bottom: 20px; }
        .form-label { display: block; font-size: 14px; font-weight: 600; color: #374151; margin-bottom: 8px; }
        
        .form-control {
            width: 100%;
            padding: 14px 16px;
            border: 2px solid #e5e7eb;
            border-radius: 10px;
            font-size: 16px;
            color: #1f2937;
            background: #fafafa;
            transition: all 0.2s ease;
            -webkit-appearance: none;
            appearance: none;
        }
        .form-control:focus {
            outline: none;
            border-color: #667eea;
            background: #ffffff;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }
        .form-control::placeholder { color: #9ca3af; }
        
        .hint-text { font-size: 13px; color: #6b7280; margin-bottom: 20px; line-height: 1.5; }
        
        .btn {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            padding: 14px 24px;
            border: none;
            border-radius: 10px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.2s ease;
            text-decoration: none;
            -webkit-tap-highlight-color: transparent;
        }
        .btn-primary { background: #dc2626; color: #ffffff; width: 100%; }
        .btn-primary:hover { background: #b91c1c; transform: translateY(-1px); box-shadow: 0 4px 12px rgba(220, 38, 38, 0.3); }
        .btn-primary:active { transform: translateY(0); }
        .btn-block { width: 100%; }
        
        .back-link { text-align: center; margin-top: 24px; }
        .back-link a { font-size: 14px; color: #667eea; font-weight: 600; text-decoration: none; display: inline-flex; align-items: center; gap: 6px; }
        .back-link a:hover { color: #764ba2; }
        
        .password-requirements {
            font-size: 12px;
            color: #6b7280;
            margin-top: 6px;
        }
        
        @media (max-width: 480px) {
            body.login-page { padding: 12px; background: #ffffff; }
            .login-card { box-shadow: none; padding: 24px 16px; border-radius: 0; }
            .login-logo img { max-width: 64px; }
            .login-logo h1 { font-size: 20px; }
            .form-control { padding: 16px; font-size: 16px; }
            .btn { padding: 16px 24px; min-height: 48px; }
        }
        
        @media (max-width: 360px) {
            .login-card { padding: 20px 12px; }
            .login-logo h1 { font-size: 18px; }
        }
        
        @media (max-height: 500px) and (orientation: landscape) {
            body.login-page { align-items: flex-start; padding-top: 20px; padding-bottom: 20px; overflow-y: auto; }
            .login-logo { margin-bottom: 16px; }
            .login-logo img { max-width: 48px; margin-bottom: 8px; }
        }
        
        @media (min-width: 769px) {
            .login-card { padding: 40px 36px; }
            .login-logo h1 { font-size: 28px; }
            .form-control { padding: 16px 18px; }
        }
        
        @media (prefers-color-scheme: dark) {
            body.login-page { background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%); }
            .login-card { background: #1f2937; }
            .login-logo h1 { color: #f9fafb; }
            .form-label { color: #d1d5db; }
            .form-control { background: #374151; border-color: #4b5563; color: #f9fafb; }
            .form-control:focus { background: #374151; border-color: #667eea; }
            .form-control::placeholder { color: #9ca3af; }
            .hint-text { color: #9ca3af; }
            .alert-danger { background: rgba(220, 38, 38, 0.1); border-color: rgba(220, 38, 38, 0.3); }
            .alert-success { background: rgba(22, 163, 74, 0.1); border-color: rgba(22, 163, 74, 0.3); }
        }
        
        @supports (padding: max(0px)) {
            body.login-page {
                padding-left: max(16px, env(safe-area-inset-left));
                padding-right: max(16px, env(safe-area-inset-right));
                padding-bottom: max(16px, env(safe-area-inset-bottom));
            }
        }
    </style>
</head>
<body class="login-page">
    <div class="login-container">
        <div class="login-card">
            <div class="login-logo">
                <img src="assets/images/logo.png" alt="<?php echo SITE_NAME; ?>">
                <h1>Reset Password</h1>
            </div>
            
            <?php if ($error): ?>
                <div class="alert alert-danger"><span>⚠</span> <?php echo e($error); ?></div>
            <?php endif; ?>
            
            <?php if ($success): ?>
                <div class="alert alert-success"><span>✓</span> <?php echo e($success); ?></div>
                <a href="index.php" class="btn btn-primary btn-block">Go to Login</a>
            <?php else: ?>
                <form method="POST" action="" novalidate>
                    <div class="form-group">
                        <label class="form-label" for="password">New Password</label>
                        <input 
                            type="password" 
                            id="password"
                            name="password" 
                            class="form-control" 
                            required 
                            placeholder="Enter new password"
                            minlength="8"
                            autocomplete="new-password"
                        >
                        <p class="password-requirements">Minimum 8 characters</p>
                    </div>
                    
                    <div class="form-group">
                        <label class="form-label" for="confirm_password">Confirm Password</label>
                        <input 
                            type="password" 
                            id="confirm_password"
                            name="confirm_password" 
                            class="form-control" 
                            required 
                            placeholder="Confirm new password"
                            minlength="8"
                            autocomplete="new-password"
                        >
                    </div>
                    
                    <button type="submit" class="btn btn-primary btn-block">Reset Password</button>
                </form>
            <?php endif; ?>
            
            <div class="back-link">
                <a href="login.php">← Back to Login</a>
            </div>
        </div>
    </div>
</body>
</html>
