<?php

namespace App\Modules\Dashboard\Services;

use App\Models\User;

final class PersonaAccessEvaluator
{
    private const PERSONAS = ['warehouse', 'procurement', 'sales', 'finance', 'admin'];

    public function isAllowedPersona(string $persona): bool
    {
        return in_array($persona, self::PERSONAS, true);
    }

    public function canAccess(?User $user, string $persona): bool
    {
        if ($user === null || ! $this->isAllowedPersona($persona)) {
            return false;
        }

        return match ($persona) {
            'warehouse' => $user->can('inventory.warehouses.view')
                || $user->can('inventory.catalog.view')
                || $user->can('inventory.stock.view'),
            'procurement' => $user->can('procurement.purchase_orders.view')
                || $user->can('procurement.goods_receipts.view'),
            'sales' => $user->can('sales.orders.view'),
            'finance' => $user->can('exports.access') || $user->can('operations.control.view'),
            'admin' => $user->can('settings.users.view')
                || $user->can('settings.roles.view')
                || $user->can('admin.access'),
            default => false,
        };
    }
}
