<?php

declare(strict_types=1);

namespace Core;

use App\Models\AdminUser;

abstract class Controller
{
    protected function view(string $view, array $data = [], string $layout = 'app'): void
    {
        $this->disableCaching();
        View::render($view, $data, $layout);
    }

    protected function disableCaching(): void
    {
        if (headers_sent()) {
            return;
        }

        header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
        header('Pragma: no-cache');
        header('Expires: 0');
    }

    protected function redirectTo(string $path): never
    {
        redirect(baseUrl($path));
    }

    protected function requireAdmin(): void
    {
        if (!AdminUser::current()) {
            Session::flash('error', 'Faça login para acessar o painel.');
            redirect(baseUrl('admin/login'));
        }
    }

    protected function requireAdminRole(): void
    {
        $this->requireAdmin();

        if (!AdminUser::currentIsAdmin()) {
            Session::flash('error', 'Apenas administradores podem acessar esta área.');
            redirect(baseUrl('admin/admissoes'));
        }
    }
}
