<?php

declare(strict_types=1);

require_once __DIR__ . '/../core/web_bootstrap.php';

/* Already logged in */
if (!empty($_SESSION['user']) && !empty($_SESSION['api_token'])) {
    header('Location: dashboard.php');
    exit;
}

$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

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

    if (!$email || !$password) {
        $error = 'Email and password required.';
    } else {

        $apiUrl = 'https://app2.jb26.co.za/api/auth/login.php';

        $ch = curl_init($apiUrl);

        curl_setopt_array($ch, [
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POST => true,
            CURLOPT_TIMEOUT => 10,
            CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
            CURLOPT_POSTFIELDS => json_encode([
                'email' => $email,
                'password' => $password
            ])
        ]);

        $response = curl_exec($ch);

        if (curl_errno($ch)) {
            $error = 'Connection error. Please try again.';
        }

        curl_close($ch);

        if (!$error) {

            $result = json_decode($response, true);

            if (!($result['success'] ?? false)) {
                $error = $result['message'] ?? 'Login failed.';
            } else {

                $user  = $result['data']['user'];
                $token = $result['data']['token'];

                if ($user['role'] !== 'super_admin') {
                    $error = 'Access denied. Super Admin only.';
                } else {

                    session_regenerate_id(true);

                    $_SESSION['user'] = $user;
                    $_SESSION['api_token'] = $token;

                    header('Location: dashboard.php');
                    exit;
                }
            }
        }
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Platform Admin Login</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="bg-dark d-flex align-items-center" style="height:100vh;">

<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-4">
            <div class="card shadow">
                <div class="card-body">
                    <h4 class="mb-4 text-center">Platform Admin</h4>

                    <?php if ($error): ?>
                        <div class="alert alert-danger">
                            <?= htmlspecialchars($error) ?>
                        </div>
                    <?php endif; ?>

                    <form method="POST" autocomplete="off">
                        <div class="mb-3">
                            <input type="email"
                                   name="email"
                                   class="form-control"
                                   placeholder="Email"
                                   required>
                        </div>

                        <div class="mb-3">
                            <input type="password"
                                   name="password"
                                   class="form-control"
                                   placeholder="Password"
                                   required>
                        </div>

                        <button class="btn btn-primary w-100">
                            Login
                        </button>
                    </form>

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

</body>
</html>