<?php

declare(strict_types=1);

require_once __DIR__ . '/../models/Usuario.php';

class AuthController extends Controller
{
    public function loginForm(): void
    {
        $this->view('auth/login', [], 'public');
    }

    public function login(): void
    {
        $email = trim($_POST['email'] ?? '');
        $senha = $_POST['senha'] ?? '';

        $model = new Usuario();
        $usuario = $model->buscarPorEmail($email);

        if (!$usuario || !password_verify($senha, $usuario['senha'])) {
            Session::flash('erro', 'Usuário ou senha inválidos.');
            $this->redirect('/login');
        }

        Auth::login($usuario);
        $this->redirect('/admin');
    }

    public function logout(): void
    {
        Auth::logout();
        $this->redirect('/login');
    }
}
