<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

session_start();
include ('../config/db.php');
include ('mailer.php'); 

$message = '';
$success = false;

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $input = trim($_POST['identifier']); // Can be username or email

    // Try to find the user
    $stmt = $pdo->prepare("SELECT * FROM users WHERE username = ? OR email = ?");
    $stmt->execute([$input, $input]);
    $user = $stmt->fetch(PDO::FETCH_ASSOC);

    if ($user) {
        // Create a reset token and expiration
        $token = bin2hex(random_bytes(32));
        $expires = date('Y-m-d H:i:s', strtotime('+1 hour'));

        // Save token to the database
        $stmt = $pdo->prepare("UPDATE users SET reset_token = ?, reset_token_expires = ? WHERE id = ?");
        $stmt->execute([$token, $expires, $user['id']]);

        // Build reset link
        $reset_link = "https://custora.digivorttechnologies.org/auth/reset_password.php?token=$token";

        // Send reset email
        sendPasswordResetEmail($user['email'], $user['full_name'], $reset_link);

        $success = true;
        $message = "If your account exists, a password reset link has been sent.";
    } else {
        // Prevent account enumeration
        $success = true;
        $message = "If your account exists, a password reset link has been sent.";
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Forgot Password</title>
    <link rel="icon" href="../public/assets/img/custora_logo.png" type="image/png">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <style>
        body {
            font-family: Arial, sans-serif;
            background: #f4f7fa;
            margin: 0;
            display: flex;
            align-items: center;
            justify-content: center;
            height: 100vh;
        }

        .container {
            background: #fff;
            padding: 35px;
            width: 100%;
            max-width: 380px;
            border-radius: 10px;
            box-shadow: 0 6px 18px rgba(0,0,0,0.1);
            text-align: center;
            box-sizing: border-box;
        }

        img {
            width: 160px;
            margin-bottom: 10px;
        }

        h2 {
            margin-bottom: 20px;
            color: #005f8d;
            font-size: 1.4rem;
        }

        form {
            text-align: left;
        }

        label {
            display: block;
            margin-bottom: 6px;
            font-weight: bold;
            color: #444;
            font-size: 0.95rem;
        }

        input[type="text"] {
            width: 100%;
            padding: 12px;
            margin-bottom: 18px;
            border: 1px solid #ccc;
            border-radius: 6px;
            outline: none;
            font-size: 1rem;
            box-sizing: border-box;
        }

        input[type="text"]:focus {
            border-color: #005f8d;
            box-shadow: 0 0 5px rgba(0,95,141,0.3);
        }

        button {
            width: 100%;
            padding: 12px;
            background: #005f8d;
            color: #fff;
            font-size: 1rem;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            transition: background 0.3s;
        }

        button:hover {
            background: #004466;
        }

        .message {
            padding: 10px;
            border-radius: 6px;
            margin-bottom: 15px;
            font-size: 0.95rem;
        }

        .success {
            background: #d1e7dd;
            color: #0f5132;
        }

        .error {
            background: #f8d7da;
            color: #842029;
        }

        a {
            display: block;
            margin-top: 15px;
            color: #005f8d;
            text-decoration: none;
            font-size: 0.95rem;
        }

        a:hover {
            text-decoration: underline;
        }

        /* Responsive design adjustments */
        @media (max-width: 480px) {
            body {
                height: auto;
                padding: 20px;
                display: block;
            }
            .container {
                width: 100%;
                padding: 25px 20px;
                box-shadow: none;
                border-radius: 8px;
            }
            img {
                width: 140px;
            }
            h2 {
                font-size: 1.2rem;
            }
            button {
                font-size: 0.95rem;
                padding: 10px;
            }
        }
    </style>
</head>

<body>
    <div class="container">
        <h1><img src="../public/assets/img/custora_logo.png" alt="Custora Logo"></h1>
        <h2>Forgot Password</h2>

        <?php if ($message): ?>
            <p class="message <?= $success ? 'success' : 'error' ?>">
                <?= htmlspecialchars($message) ?>
            </p>
        <?php endif; ?>

        <form method="POST">
            <label for="identifier">Username or Email:</label>
            <input type="text" id="identifier" name="identifier" placeholder="Enter your username or email" required>

            <button type="submit">Send Reset Link</button>
        </form>

        <a href="login.php">← Back to Login</a>
    </div>
</body>
</html>