<?php
session_start();

// If already logged in, redirect
if (isset($_SESSION['admin_token'])) {
    header('Location: index.php');
    exit;
}

require_once 'includes/api_client.php';

$error = '';

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

    if ($email && $password) {
        // Authenticate against Node.js API
        $result = apiPost('/auth/admin/login', ['email' => $email, 'password' => $password]);

        if ($result['success'] ?? false) {
            $_SESSION['admin_token'] = $result['token'];
            $_SESSION['admin_name']  = $result['admin']['name'] ?? 'Admin';
            $_SESSION['admin_email'] = $result['admin']['email'] ?? '';
            $_SESSION['admin_role']  = $result['admin']['role'] ?? 'admin';
            header('Location: index.php');
            exit;
        } else {
            $error = $result['message'] ?? 'Login failed. Check your credentials.';
        }
    } else {
        $error = 'Please enter your email and password.';
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Admin Login — Mastreams</title>
<style>
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; background: #0f0f0f; color: #fff; min-height: 100vh; display: flex; align-items: center; justify-content: center; }
.box { background: #1a1a1a; border: 1px solid #2a2a2a; border-radius: 14px; padding: 40px 36px; width: 100%; max-width: 420px; }
.logo { display: flex; align-items: center; gap: 10px; margin-bottom: 32px; }
.logo-m { background: #e87722; color: #fff; font-weight: 900; font-size: 18px; width: 36px; height: 36px; border-radius: 8px; display: flex; align-items: center; justify-content: center; }
.logo-text { font-size: 18px; font-weight: 900; letter-spacing: 0.5px; }
.logo-text span { color: #e87722; }
h2 { font-size: 22px; font-weight: 800; margin-bottom: 6px; }
p { color: #aaa; font-size: 14px; margin-bottom: 28px; }
label { display: block; font-size: 13px; font-weight: 600; color: #aaa; margin-bottom: 6px; }
input { width: 100%; background: #222; border: 1px solid #2a2a2a; border-radius: 8px; padding: 11px 14px; color: #fff; font-size: 14px; outline: none; margin-bottom: 16px; transition: border-color 0.15s; }
input:focus { border-color: #e87722; }
.error { background: rgba(231,76,60,0.12); border: 1px solid rgba(231,76,60,0.3); color: #e74c3c; border-radius: 8px; padding: 10px 14px; font-size: 13px; margin-bottom: 16px; }
button { width: 100%; background: #e87722; color: #fff; border: none; border-radius: 8px; padding: 13px; font-size: 15px; font-weight: 700; cursor: pointer; margin-top: 6px; transition: opacity 0.15s; }
button:hover { opacity: 0.88; }
.hint { text-align: center; color: #555; font-size: 11px; margin-top: 20px; }
.api-note { background: rgba(232,119,34,0.08); border: 1px solid rgba(232,119,34,0.2); border-radius: 8px; padding: 10px 14px; font-size: 12px; color: #aaa; margin-bottom: 20px; }
.api-note code { color: #e87722; font-family: monospace; }
</style>
</head>
<body>
<div class="box">
    <div class="logo">
        <div class="logo-m">M</div>
        <div class="logo-text">MAS<span>TREAMS</span> Admin</div>
    </div>
    <h2>Sign In</h2>
    <p>Manage your streaming platform</p>

    <div class="api-note">
        Authenticates via Node.js API at<br>
        <code><?= htmlspecialchars(API_BASE) ?></code>
    </div>

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

    <form method="POST">
        <label>Email Address</label>
        <input type="email" name="email" value="<?= htmlspecialchars($_POST['email'] ?? '') ?>" placeholder="admin@mastreams.mw" required autofocus>
        <label>Password</label>
        <input type="password" name="password" placeholder="••••••••" required>
        <button type="submit">Sign In</button>
    </form>
    <div class="hint">Mastreams Admin v1.0 · Powered by Node.js + PostgreSQL + R2</div>
</div>
</body>
</html>
