<?php
session_start();
require_once 'db/config.php';
require_once 'mail/MailService.php';

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

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['email'])) {
    $email = trim($_POST['email']);
    $password = isset($_POST['password']) ? $_POST['password'] : '';

    // Admin Login
    if ($email === 'admin@gmail.com' && $password === 'admin') {
        $_SESSION['user'] = 'admin';
        header('Location: admin.php');
        exit;
    }
    
    // Participant Login
    try {
        $stmt = db()->prepare("SELECT * FROM attendees WHERE email = ?");
        $stmt->execute([$email]);
        $attendee = $stmt->fetch();

        if ($attendee && password_verify($password, $attendee['password'])) {
            $_SESSION['user_id'] = $attendee['id'];
            header('Location: dashboard.php');
            exit;
        } else {
            $error = 'Invalid email or password. Would you like to <a href="register.php" style="color: #ffdd57;">register for the webinar</a>?';
        }
    } catch (PDOException $e) {
        error_log($e->getMessage());
        $error = 'A database error occurred. Please try again later.';
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login - Webinar Platform</title>
    <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap" rel="stylesheet">
    <style>
        :root {
            --primary-color: #4a90e2;
            --background-color: #f4f7f6;
            --card-background: #ffffff;
            --text-color: #333;
            --input-border: #e0e0e0;
            --input-focus-border: #4a90e2;
            --error-color: #d9534f;
            --success-color: #5cb85c;
        }
        body {
            font-family: 'Roboto', sans-serif;
            background-color: var(--background-color);
            color: var(--text-color);
            margin: 0;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
        }
        .logo {
            font-weight: 700;
            font-size: 24px;
            margin-bottom: 2rem;
            color: var(--primary-color);
        }
        .login-container {
            width: 100%;
            max-width: 400px;
            padding: 2rem;
        }
        .login-card {
            background-color: var(--card-background);
            border-radius: 8px;
            padding: 2.5rem;
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.05);
            border: 1px solid var(--input-border);
        }
        h1 {
            font-size: 24px;
            font-weight: 500;
            margin-top: 0;
            margin-bottom: 0.5rem;
            text-align: center;
        }
        .subtitle {
            text-align: center;
            margin-bottom: 2rem;
            color: #777;
        }
        .form-group {
            margin-bottom: 1.5rem;
        }
        label {
            display: block;
            margin-bottom: 0.5rem;
            font-weight: 500;
            font-size: 14px;
        }
        input[type="email"], input[type="password"] {
            width: 100%;
            padding: 12px;
            border: 1px solid var(--input-border);
            border-radius: 4px;
            box-sizing: border-box;
            font-size: 16px;
            transition: border-color 0.2s;
        }
        input[type="email"]:focus, input[type="password"]:focus {
            outline: none;
            border-color: var(--input-focus-border);
            box-shadow: 0 0 0 2px rgba(74, 144, 226, 0.2);
        }
        .submit-button {
            width: 100%;
            padding: 14px;
            border: none;
            background-color: var(--primary-color);
            color: white;
            font-weight: 700;
            font-size: 16px;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.2s;
        }
        .submit-button:hover {
            background-color: #357abd;
        }
        .message {
            padding: 1rem;
            margin-bottom: 1.5rem;
            border-radius: 4px;
            text-align: center;
            font-size: 14px;
        }
        .error {
            background-color: #fbe9e7;
            color: var(--error-color);
            border: 1px solid var(--error-color);
        }
        .success {
            background-color: #e8f5e9;
            color: var(--success-color);
            border: 1px solid var(--success-color);
        }
        .footer-link {
            text-align: center;
            margin-top: 1.5rem;
            font-size: 14px;
        }
        .footer-link a {
            color: var(--primary-color);
            text-decoration: none;
            font-weight: 500;
        }
        .footer-link a:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="logo">WebinarPlatform</div>
        <div class="login-card">
            <h1>Welcome Back</h1>
            <p class="subtitle">Login to access your dashboard</p>

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

            <form method="POST" action="login.php">
                <div class="form-group">
                    <label for="email">Email Address</label>
                    <input type="email" id="email" name="email" required value="<?= isset($_POST['email']) ? htmlspecialchars($_POST['email']) : '' ?>">
                </div>
                <div class="form-group">
                    <label for="password">Password</label>
                    <input type="password" id="password" name="password" required>
                </div>
                <button type="submit" class="submit-button">Login</button>
            </form>
            
            <div class="footer-link">
                Don't have an account? <a href="register.php">Register for the Webinar</a>
            </div>
        </div>
        <div class="footer-link" style="margin-top: 2rem;">
            <a href="index.php">← Back to Home</a>
        </div>
    </div>
</body>
</html>