<?php
session_start();
require_once __DIR__ . '/../config/conexao.php';
require_once __DIR__ . '/../config/app.php';

// Se já estiver logado
if (isset($_SESSION['usuario_id'])) {
    header("Location: " . base_url('pages/dashboard.php'));
    exit;
}

$erro = '';

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

    if (!empty($email) && !empty($senha)) {

        $sql = $pdo->prepare("SELECT * FROM usuarios WHERE email = :email AND status = 'Ativo' LIMIT 1");
        $sql->execute(['email' => $email]);
        $usuario = $sql->fetch(PDO::FETCH_ASSOC);

        // ✅ Validação correta de senha
        if ($usuario && password_verify($senha, $usuario['senha'])) {

            // 🔐 Segurança extra (previne session fixation)
            session_regenerate_id(true);

            $_SESSION['usuario_id'] = $usuario['id'];
            $_SESSION['usuario_nome'] = $usuario['nome'] ?? 'Usuário';
            $_SESSION['usuario_nivel'] = $usuario['nivel'] ?? 'usuario';

            header("Location: " . base_url('pages/dashboard.php'));
            exit;

        } else {
            $erro = 'Email ou senha incorretos.';
        }

    } else {
        $erro = 'Preencha todos os campos.';
    }
}
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login - Lico Lico</title>

<style>
:root {
    --bg: #020617;
    --card: #0f172a;
    --border: #1e293b;
    --primary: #6366f1;
    --primary-dark: #4f46e5;
    --text: #e2e8f0;
    --muted: #64748b;
    --danger: #ef4444;
}
* { margin: 0; padding: 0; box-sizing: border-box; }

body {
    background: var(--bg);
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    font-family: 'Segoe UI', -apple-system, sans-serif;
    color: var(--text);
}

.login-container {
    width: 100%;
    max-width: 420px;
    padding: 20px;
}

.login-box {
    background: linear-gradient(145deg, var(--card), #0a0f1e);
    padding: 40px 36px;
    border-radius: 20px;
    border: 1px solid var(--border);
    box-shadow: 0 20px 60px rgba(0,0,0,0.5);
}

.logo {
    text-align: center;
    margin-bottom: 32px;
}

.logo h1 {
    font-size: 28px;
    font-weight: 700;
    color: #fff;
}

.logo p {
    font-size: 13px;
    color: var(--muted);
    margin-top: 4px;
}

.form-group {
    margin-bottom: 20px;
}

.form-group label {
    display: block;
    font-size: 12px;
    font-weight: 600;
    color: var(--muted);
    margin-bottom: 8px;
    text-transform: uppercase;
}

.form-group input {
    width: 100%;
    padding: 13px 16px;
    border-radius: 12px;
    border: 1px solid var(--border);
    background: var(--bg);
    color: #fff;
    font-size: 15px;
}

.form-group input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(99,102,241,0.15);
}

.btn-login {
    width: 100%;
    padding: 14px;
    background: linear-gradient(135deg, var(--primary), var(--primary-dark));
    color: #fff;
    border: none;
    border-radius: 12px;
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
    margin-top: 8px;
}

.btn-login:hover {
    transform: translateY(-2px);
    box-shadow: 0 10px 30px rgba(99,102,241,0.4);
}

.erro {
    background: rgba(239,68,68,0.1);
    color: var(--danger);
    border: 1px solid rgba(239,68,68,0.2);
    padding: 12px;
    border-radius: 10px;
    font-size: 13px;
    margin-bottom: 20px;
    text-align: center;
}
</style>
</head>

<body>

<div class="login-container">
    <div class="login-box">

        <div class="logo">
            <h1>Lico Lico</h1>
            <p>Sistema de Gestão Escolar</p>
        </div>

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

        <form method="POST" autocomplete="off">
            <div class="form-group">
                <label>Email</label>
                <input type="email" name="email" placeholder="seu@email.com" required autofocus>
            </div>

            <div class="form-group">
                <label>Senha</label>
                <input type="password" name="senha" placeholder="Digite sua senha" required>
            </div>

            <button type="submit" class="btn-login">Entrar</button>
        </form>

    </div>
</div>

</body>
</html>