<?php
/**
 * Forgot Password Page
 * Digital Marketplace
 */

require_once __DIR__ . '/config/database.php';

$page_title = 'Forgot Password';

$errors = [];
$success = false;

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email = sanitize_email($_POST['email'] ?? '');
    
    if (empty($email)) {
        $errors[] = 'Please enter your email address.';
    } elseif (!validate_email($email)) {
        $errors[] = 'Please enter a valid email address.';
    } else {
        // Check if user exists
        $stmt = $pdo->prepare("SELECT id FROM users WHERE email = ?");
        $stmt->execute([$email]);
        $user = $stmt->fetch();
        
        if ($user) {
            // Generate reset token
            $token = generate_random_string(32);
            $expires = date('Y-m-d H:i:s', strtotime('+1 hour'));
            
            // Save token
            $stmt = $pdo->prepare("INSERT INTO password_resets (user_id, token, expires_at, created_at) VALUES (?, ?, ?, NOW())");
            $stmt->execute([$user['id'], $token, $expires]);
            
            // Send email
            send_password_reset_email($user['id'], $token);
            
            $success = true;
        } else {
            // Don't reveal if email exists
            $success = true;
        }
    }
}

// Don't show header for this page
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $page_title; ?> | Digital Marketplace</title>
    
    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
    
    <!-- Font Awesome -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
    
    <!-- Google Fonts -->
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    
    <style>
    :root {
        --primary: #6366f1;
        --primary-dark: #4f46e5;
        --dark: #0f172a;
        --dark-lighter: #1e293b;
    }
    
    body {
        font-family: 'Inter', sans-serif;
        background: var(--dark);
        min-height: 100vh;
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 2rem;
    }
    
    .auth-card {
        background: var(--dark-lighter);
        border: 1px solid rgba(255,255,255,0.1);
        border-radius: 1rem;
        padding: 2.5rem;
        width: 100%;
        max-width: 420px;
    }
    
    .auth-logo {
        text-align: center;
        margin-bottom: 2rem;
    }
    
    .auth-logo i {
        font-size: 3rem;
        color: var(--primary);
    }
    
    .auth-logo h4 {
        color: white;
        margin-top: 1rem;
    }
    
    .form-control {
        background: var(--dark);
        border: 1px solid rgba(255,255,255,0.1);
        color: white;
        padding: 0.875rem 1rem;
    }
    
    .form-control:focus {
        background: var(--dark);
        border-color: var(--primary);
        color: white;
        box-shadow: 0 0 0 0.2rem rgba(99, 102, 241, 0.25);
    }
    
    .btn-primary {
        background: var(--primary);
        border-color: var(--primary);
        padding: 0.875rem 1.5rem;
        font-weight: 500;
    }
    
    .btn-primary:hover {
        background: var(--primary-dark);
        border-color: var(--primary-dark);
    }
    
    .text-muted {
        color: rgba(255,255,255,0.5) !important;
    }
    </style>
</head>
<body>
    <div class="auth-card">
        <div class="auth-logo">
            <i class="fas fa-store"></i>
            <h4>Digital Marketplace</h4>
        </div>
        
        <?php if ($success): ?>
            <div class="text-center">
                <i class="fas fa-envelope fa-3x text-success mb-3"></i>
                <h5 class="text-white mb-3">Check Your Email</h5>
                <p class="text-muted mb-4">
                    If an account exists with this email, you will receive password reset instructions.
                </p>
                <a href="<?php echo BASE_URL; ?>/login.php" class="btn btn-primary w-100">
                    <i class="fas fa-arrow-left me-2"></i>Back to Login
                </a>
            </div>
        <?php else: ?>
            <h5 class="text-white mb-3">Forgot Password?</h5>
            <p class="text-muted mb-4">Enter your email address and we'll send you instructions to reset your password.</p>
            
            <?php if (!empty($errors)): ?>
                <div class="alert alert-danger">
                    <?php foreach ($errors as $error): ?>
                        <p class="mb-1"><i class="fas fa-exclamation-circle me-2"></i><?php echo $error; ?></p>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>
            
            <form method="POST">
                <div class="mb-4">
                    <label class="form-label text-white">Email Address</label>
                    <input type="email" name="email" class="form-control" placeholder="Enter your email" required>
                </div>
                
                <button type="submit" class="btn btn-primary w-100 mb-3">
                    <i class="fas fa-paper-plane me-2"></i>Send Reset Link
                </button>
                
                <div class="text-center">
                    <a href="<?php echo BASE_URL; ?>/login.php" class="text-muted">
                        <i class="fas fa-arrow-left me-1"></i>Back to Login
                    </a>
                </div>
            </form>
        <?php endif; ?>
    </div>
</body>
</html>
