<?php

namespace App\Middleware;

class AuthMiddleware {
    public static function requireAuth() {
        if (!is_logged_in()) {
            redirect(base_url('login'));
        }
    }
    
    public static function requireRole($roles) {
        $user = current_user();
        if (!$user) {
            redirect(base_url('login'));
        }
        
        if (!is_array($roles)) {
            $roles = [$roles];
        }
        
        if (!in_array($user['role_slug'], $roles)) {
            http_response_code(403);
            require_once __DIR__ . '/../../app/Views/errors/403.php';
            exit;
        }
    }
    
    public static function requirePermission($permission) {
        $user = current_user();
        if (!$user || !has_permission($permission)) {
            http_response_code(403);
            require_once __DIR__ . '/../../app/Views/errors/403.php';
            exit;
        }
    }
    
    public static function checkLogin() {
        if (is_logged_in()) {
            $user = current_user();
            if ($user) {
                redirect(base_url($user['role_slug'] . '/dashboard'));
            }
        }
    }
}
