<?php
// forgot_password.php
session_start();
require_once 'config/database.php';

$database = new Database();
$db = $database->getConnection();

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

if($_SERVER['REQUEST_METHOD'] == 'POST') {
    $email = trim($_POST['email']);
    
    if(empty($email)) {
        $error = "Please enter your email address.";
    } else {
        $query = "SELECT id FROM customers WHERE email = :email";
        $stmt = $db->prepare($query);
        $stmt->execute([':email' => $email]);
        
        if($stmt->fetch()) {
            // Generate reset token
            $token = bin2hex(random_bytes(32));
            $expires = date('Y-m-d H:i:s', strtotime('+1 hour'));
            
            $update_query = "UPDATE customers SET reset_token = :token, reset_expires = :expires WHERE email = :email";
            $update_stmt = $db->prepare($update_query);
            $update_stmt->execute([
                ':token' => $token,
                ':expires' => $expires,
                ':email' => $email
            ]);
            
            // Send email (implement with mail server)
            // sendResetEmail($email, $token);
            
            $success = "Password reset instructions have been sent to your email.";
        } else {
            // Don't reveal if email exists or not for security
            $success = "If your email is registered, you will receive reset instructions.";
        }
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Forgot Password</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }
        .card {
            border-radius: 20px;
            box-shadow: 0 20px 40px rgba(0,0,0,0.2);
            max-width: 400px;
            width: 100%;
        }
    </style>
<link rel="stylesheet" href="assets/theme.css">
<link rel="stylesheet" href="assets/portal-dark.css">
    <link rel="stylesheet" href="assets/ushs-global-theme.css">
</head>
<body>
    <div class="card">
        <div class="card-body p-4">
            <h4 class="text-center mb-4">Reset Password</h4>
            
            <?php if($error): ?>
                <div class="alert alert-danger"><?php echo $error; ?></div>
            <?php endif; ?>
            
            <?php if($success): ?>
                <div class="alert alert-success"><?php echo $success; ?></div>
                <div class="text-center">
                    <a href="login.php" class="btn btn-primary">Return to Login</a>
                </div>
            <?php else: ?>
                <form method="POST">
                    <div class="mb-3">
                        <label class="form-label">Email Address</label>
                        <input type="email" name="email" class="form-control" required>
                    </div>
                    <button type="submit" class="btn btn-primary w-100">Send Reset Link</button>
                </form>
                <div class="text-center mt-3">
                    <a href="login.php">Back to Login</a>
                </div>
            <?php endif; ?>
        </div>
    </div>
</body>
</html>