<?php
session_start();
require 'config/db.php';
include 'includes/header.php';

$msg = "";
$msgClass = "";

if (isset($_POST['reset_request'])) {
    $email = trim($_POST['email']);
    
    // Check if user exists
    $stmt = $pdo->prepare("SELECT id FROM users WHERE email = ?");
    $stmt->execute([$email]);
    if ($stmt->rowCount() > 0) {
        
        // Generate Token
        $token = bin2hex(random_bytes(50));
        
        // Remove old tokens for this email to prevent duplicates
        $pdo->prepare("DELETE FROM password_resets WHERE email = ?")->execute([$email]);
        
        // Insert new token
        $stmt = $pdo->prepare("INSERT INTO password_resets (email, token) VALUES (?, ?)");
        $stmt->execute([$email, $token]);
        
        // SEND EMAIL (Live Server Logic)
        $resetLink = "https://prosubscriptionoffers.com/reset_password.php?token=" . $token;
        $subject = "Reset Your Password - Pro Subscription Offers";
        
        // HTML Email Content
        $message = "
        <html>
        <head><title>Password Reset</title></head>
        <body style='font-family: Arial, sans-serif; color: #333;'>
            <div style='background-color: #f4f4f4; padding: 20px; text-align: center;'>
                <h2 style='color: #2c3e50;'>Password Reset Request</h2>
                <p>You requested to reset your password for Pro Subscription Offers.</p>
                <p>Click the button below to set a new password:</p>
                <br>
                <a href='$resetLink' style='background-color: #3498db; color: white; padding: 12px 25px; text-decoration: none; border-radius: 5px; font-weight: bold;'>Reset Password</a>
                <br><br>
                <p>Or copy this link into your browser:</p>
                <p style='word-break: break-all; color: #555;'>$resetLink</p>
                <br>
                <p><small>If you did not request this, please ignore this email.</small></p>
            </div>
        </body>
        </html>
        ";
        
        // Headers for HTML Email
        $headers = "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
        $headers .= "From: Pro Subscription Offers Support <support@prosubscriptionoffers.com>" . "\r\n";

        // Attempt to send mail
        if(mail($email, $subject, $message, $headers)) {
            $msg = "Success! A reset link has been sent to your email. Please check your Inbox and Spam folder.";
            $msgClass = "success";
        } else {
            $msg = "Error: The system could not send the email. Please try again later or contact support.";
            $msgClass = "error";
        }
        
    } else {
        $msg = "We couldn't find an account registered with that email.";
        $msgClass = "error";
    }
}
?>

<style>
    .forgot-wrapper {
        background: linear-gradient(135deg, #FF512F 0%, #DD2476 100%); /* Vibrant Orange/Pink Gradient */
        min-height: 85vh;
        display: flex;
        justify-content: center;
        align-items: center;
        padding: 20px;
    }

    .forgot-card {
        background: rgba(255, 255, 255, 0.95);
        width: 100%;
        max-width: 420px;
        padding: 40px;
        border-radius: 20px;
        text-align: center;
        
        /* Deep 3D Shadow */
        box-shadow: 
            0 20px 50px rgba(0,0,0,0.3), 
            0 0 0 1px rgba(255,255,255,0.5) inset;
            
        transform: translateY(0);
        transition: transform 0.3s ease;
    }

    .forgot-card:hover {
        transform: translateY(-5px);
    }

    .forgot-card h2 {
        color: #333;
        margin-bottom: 15px;
        font-weight: 800;
        text-transform: uppercase;
        font-size: 24px;
    }

    .forgot-card p {
        color: #666;
        font-size: 15px;
        margin-bottom: 30px;
        line-height: 1.5;
    }

    .styled-input {
        width: 100%;
        padding: 14px;
        border: 2px solid #eee;
        border-radius: 10px;
        font-size: 16px;
        box-sizing: border-box;
        margin-bottom: 25px;
        transition: border-color 0.3s;
        background: #f9f9f9;
    }
    .styled-input:focus {
        border-color: #DD2476;
        background: #fff;
        outline: none;
    }

    .btn-reset {
        width: 100%;
        padding: 15px;
        background: linear-gradient(to right, #FF512F, #DD2476);
        color: white;
        border: none;
        border-radius: 10px;
        font-size: 16px;
        font-weight: bold;
        cursor: pointer;
        box-shadow: 0 5px 15px rgba(221, 36, 118, 0.3);
        transition: transform 0.2s;
    }
    .btn-reset:hover {
        transform: scale(1.02);
    }

    .msg-box { padding: 15px; border-radius: 8px; margin-bottom: 25px; font-size: 14px; font-weight: 500; }
    .msg-box.success { background: #d4edda; color: #155724; border: 1px solid #c3e6cb; }
    .msg-box.error { background: #ffdddd; color: #c0392b; border: 1px solid #f5c6cb; }
    
    .back-link {
        display: inline-block;
        margin-top: 25px;
        color: #DD2476;
        font-weight: bold;
        text-decoration: none;
        font-size: 14px;
    }
    .back-link:hover { text-decoration: underline; }
</style>

<div class="forgot-wrapper">
    <div class="forgot-card">
        <h2>Reset Password</h2>
        <p>Enter your registered email address below. We'll send you a secure link to create a new password.</p>

        <?php if($msg): ?>
            <div class="msg-box <?php echo $msgClass; ?>">
                <?php echo $msg; ?>
            </div>
        <?php endif; ?>

        <form method="POST">
            <input type="email" name="email" class="styled-input" placeholder="name@example.com" required>
            <button type="submit" name="reset_request" class="btn-reset">SEND RESET LINK</button>
        </form>

        <a href="login.php" class="back-link">
            <i class="fas fa-arrow-left"></i> Back to Login
        </a>
    </div>
</div>

<?php include 'includes/footer.php'; ?>