<?php

declare(strict_types=1);

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

/*
|--------------------------------------------------------------------------
| Prevent output before redirects
|--------------------------------------------------------------------------
*/
ob_start();

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

$error = '';

/*
|--------------------------------------------------------------------------
| Already logged in
|--------------------------------------------------------------------------
*/
if (!empty($_SESSION['admin_id'])) {

    header('Location: control_panel.php');
    exit;
}

/*
|--------------------------------------------------------------------------
| Handle Login
|--------------------------------------------------------------------------
*/
if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $email = trim($_POST['email'] ?? '');
    $password = $_POST['password'] ?? '';

    if ($email === '' || $password === '') {

        $error = 'Please enter your email and password.';

    } else {

        try {

            $stmt = db()->prepare("
                SELECT *
                FROM admins
                WHERE email = ?
                AND is_active = 1
                LIMIT 1
            ");

            $stmt->execute([$email]);

            $admin = $stmt->fetch(PDO::FETCH_ASSOC);

            if (
                $admin &&
                password_verify($password, $admin['password'])
            ) {

                /*
                |--------------------------------------------------------------------------
                | Regenerate session for security
                |--------------------------------------------------------------------------
                */
                session_regenerate_id(true);

                $_SESSION['admin_id'] = (int)$admin['id'];
                $_SESSION['admin_name'] = $admin['name'];
                $_SESSION['admin_email'] = $admin['email'];
                $_SESSION['is_admin'] = true;

                /*
                |--------------------------------------------------------------------------
                | Absolute redirect
                |--------------------------------------------------------------------------
                */
                header('Location: control_panel.php');
                exit;
            }

            $error = 'Invalid email or password.';

        } catch (Throwable $e) {

            $error = 'Login failed. ' . $e->getMessage();
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">

    <meta
        name="viewport"
        content="width=device-width, initial-scale=1.0"
    >

    <title>Admin Login</title>

    <script src="https://cdn.tailwindcss.com"></script>
</head>

<body class="min-h-screen bg-slate-950 flex items-center justify-center p-4">

    <div class="w-full max-w-md">

        <div class="bg-white rounded-3xl shadow-2xl overflow-hidden">

            <div class="bg-gradient-to-r from-slate-900 to-blue-900 px-8 py-8 text-white">

                <p class="uppercase tracking-[0.25em] text-xs text-blue-200">
                    Institutional Trading Assistant
                </p>

                <h1 class="text-3xl font-black mt-2">
                    Admin Login
                </h1>

                <p class="text-sm text-slate-300 mt-2">
                    Access the institutional control panel.
                </p>
            </div>

            <div class="p-8">

                <?php if ($error): ?>

                    <div class="mb-5 rounded-xl border border-red-200 bg-red-50 px-4 py-3 text-red-700 text-sm font-semibold">
                        <?= htmlspecialchars($error, ENT_QUOTES, 'UTF-8') ?>
                    </div>

                <?php endif; ?>

                <form method="post" class="space-y-5">

                    <div>

                        <label class="block text-sm font-semibold text-slate-700">
                            Email Address
                        </label>

                        <input
                            type="email"
                            name="email"
                            required
                            autocomplete="email"
                            class="mt-2 w-full rounded-xl border border-slate-300 px-4 py-3 focus:border-blue-500 focus:ring-2 focus:ring-blue-200 outline-none"
                            placeholder="admin@example.com"
                        >
                    </div>

                    <div>

                        <label class="block text-sm font-semibold text-slate-700">
                            Password
                        </label>

                        <input
                            type="password"
                            name="password"
                            required
                            autocomplete="current-password"
                            class="mt-2 w-full rounded-xl border border-slate-300 px-4 py-3 focus:border-blue-500 focus:ring-2 focus:ring-blue-200 outline-none"
                            placeholder="••••••••"
                        >
                    </div>

                    <button
                        type="submit"
                        class="w-full rounded-xl bg-blue-600 hover:bg-blue-700 text-white font-bold py-3 transition"
                    >
                        Login to Control Panel
                    </button>
                </form>

                <div class="mt-6 text-center text-sm text-slate-500">
                    Protected Institutional Access
                </div>

            </div>
        </div>
    </div>

</body>
</html>