<?php
session_start();

// Se o utilizador já estiver autenticado, redireciona para o painel de administração
if (isset($_SESSION['user']) && $_SESSION['user'] === 'admin') {
    header('Location: admin.php');
    exit;
}

// Credenciais fixas (podem ser alteradas aqui ou via .env/variáveis de ambiente)
$adminUser = getenv('ADMIN_USER') ?: 'admin';
$adminPass = getenv('ADMIN_PASS') ?: 'admin123';

$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = $_POST['username'] ?? '';
    $password = $_POST['password'] ?? '';
    if ($username === $adminUser && $password === $adminPass) {
        $_SESSION['user'] = 'admin';
        header('Location: admin.php');
        exit;
    } else {
        $error = 'Credenciais inválidas. Tente novamente.';
    }
}

// Carrega o accent color para estilização
$data = [];
if (file_exists(__DIR__ . '/data.json')) {
    $json = file_get_contents(__DIR__ . '/data.json');
    $data = json_decode($json, true);
}
$accent = $data['accentColor'] ?? '#E94298';
?>
<!DOCTYPE html>
<html lang="pt">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login - <?php echo htmlspecialchars($data['siteName'] ?? 'Site'); ?></title>
    <link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@400;600&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="css/styles.css">
    <style>:root{--accent: <?php echo htmlspecialchars($accent); ?>;}</style>
    <style>
        body {
            display:flex;
            justify-content:center;
            align-items:center;
            height:100vh;
            margin:0;
            background: linear-gradient(135deg, var(--accent), #ffb5d1);
            font-family:'Montserrat', sans-serif;
        }
        .login-box {
            background:#fff;
            padding:40px;
            border-radius:10px;
            width:350px;
            box-shadow:0 5px 15px rgba(0,0,0,0.3);
        }
        .login-box h2 {
            margin:0 0 20px;
            color:var(--accent);
            text-align:center;
        }
        .login-box label {
            display:block;
            margin-bottom:5px;
            font-weight:600;
        }
        .login-box input[type="text"], .login-box input[type="password"] {
            width:100%;
            padding:10px;
            border:1px solid #ddd;
            border-radius:5px;
            margin-bottom:15px;
        }
        .login-box button {
            width:100%;
            padding:10px;
            background:var(--accent);
            color:#fff;
            border:none;
            border-radius:5px;
            font-weight:600;
            cursor:pointer;
            transition:background 0.3s ease;
        }
        .login-box button:hover {
            background:#c92d75;
        }
        .error {
            color:red;
            margin-bottom:15px;
            text-align:center;
        }
    </style>
</head>
<body>
    <div class="login-box">
        <h2>Login</h2>
        <?php if ($error): ?>
            <p class="error"><?php echo htmlspecialchars($error); ?></p>
        <?php endif; ?>
        <form method="POST" action="login.php">
            <label>Utilizador</label>
            <input type="text" name="username" required>
            <label>Palavra‑passe</label>
            <input type="password" name="password" required>
            <button type="submit">Entrar</button>
        </form>
    </div>
</body>
</html>