<?php

namespace App\Support;

class ModuloPortal
{
    public static function todos(): array
    {
        return collect(config('modules.items', []))
            ->map(fn (array $modulo) => self::normalizar($modulo))
            ->all();
    }

    public static function encontrar(string $slug): ?array
    {
        return collect(self::todos())->firstWhere('slug', $slug);
    }

    public static function permissao(string $slug): string
    {
        return $slug === 'wiki'
            ? 'wiki.visualizar'
            : "modulo.{$slug}.acessar";
    }

    public static function permissoes(): array
    {
        return collect(self::todos())
            ->pluck('permissao')
            ->filter()
            ->values()
            ->all();
    }

    private static function normalizar(array $modulo): array
    {
        $modulo['permissao'] ??= self::permissao($modulo['slug']);

        return $modulo;
    }
}
