<?php
session_start();

require_once __DIR__ . '/config/config.php';
require_once __DIR__ . '/core/Database.php';
require_once __DIR__ . '/core/Auth.php';

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

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

    $user = User::findByUsername($username);

    if ($user && password_verify($password, $user['password'])) {
        Auth::login($user);
        header('Location: /dashboard.php');
        exit;
    } else {
        $error = 'Credenciales incorrectas';
    }
}

require_once __DIR__ . '/views/auth/login.php';
