<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\Request;

abstract class Controller
{
    use AuthorizesRequests;

    protected function authorizeWrite(string $module): void
    {
        $user = auth()->user();

        if ($user->isSuperAdmin()) {
            return;
        }

        if (!$user->canManage($module)) {
            abort(403, 'Non hai i permessi per modificare ' . $module);
        }
    }

    protected function authorizeRead(string $module): void
    {
        $user = auth()->user();

        if ($user->isSuperAdmin()) {
            return;
        }

        if (!$user->canAccess($module)) {
            abort(403, 'Non hai i permessi per visualizzare ' . $module);
        }
    }

    protected function authorizeDelete(string $module): void
    {
        $user = auth()->user();

        if ($user->isSuperAdmin()) {
            return;
        }

        if (!$user->canDelete($module)) {
            abort(403, 'Non hai i permessi per eliminare ' . $module);
        }
    }
}