<?php

declare(strict_types=1);

namespace Ekdos\Users;

/**
 * Die drei Rollen bilden ab, was vorher fest auf die Namen "sascha" und "svenja"
 * verdrahtet war. "inhaber" entspricht Saschas Rechten, "buero" denen von Svenja.
 */
enum Role: string
{
    case Admin = 'admin';
    case Inhaber = 'inhaber';
    case Buero = 'buero';

    public function label(): string
    {
        return match ($this) {
            self::Admin => 'Administration',
            self::Inhaber => 'Inhaber',
            self::Buero => 'Büro',
        };
    }

    /** @return list<string> */
    public function permissions(): array
    {
        return match ($this) {
            self::Admin => Permission::ALL,
            self::Inhaber => [
                Permission::OffersDelete,
                Permission::CustomersDelete,
                Permission::TasksDelete,
                Permission::PurchasesCreate,
                Permission::PurchasesDelete,
            ],
            self::Buero => [
                Permission::PurchasesComplete,
                Permission::InvoicesProcess,
            ],
        };
    }

    public function allows(string $permission): bool
    {
        return in_array($permission, $this->permissions(), true);
    }
}
