<?php
namespace App\Controllers;

use App\Services\AuthService;
use App\Helpers\Response;

class AuthController
{
    private $authService;
    
    public function __construct()
    {
        $this->authService = new AuthService();
    }
    
    public function login()
    {
        // Si ya está logueado, redirigir al index
        if ($this->authService->isLoggedIn()) {
            redirect(siteUrl('index.php'));
        }
        
        $error = null;
        
        if (isPost()) {
            $username = post('username');
            $password = post('password');
            
            if ($this->authService->login($username, $password)) {
                redirect(siteUrl('index.php'));
            } else {
                $error = "Usuario o contraseña incorrectos, o cuenta inactiva.";
            }
        }
        
        return ['error' => $error];
    }
    
    public function logout()
    {
        $this->authService->logout();
        redirect(siteUrl('login.php'));
    }
}
