<?php

namespace App\Enums;

use Illuminate\Support\Arr;

enum RoleSlug: string
{
    case Admin = 'admin';
    case Manager = 'manager';

    /**
     * @return list<string>
     */
    public static function values(): array
    {
        return array_column(self::cases(), 'value');
    }

    /**
     * @param  self|string|array<int, self|string>  $roles
     * @return list<self>
     */
    public static function wrap(mixed $roles): array
    {
        return array_values(array_map(
            fn (mixed $role): self => $role instanceof self
                ? $role
                : self::from(strtolower((string) $role)),
            Arr::wrap($roles),
        ));
    }

    /**
     * @return list<string>
     */
    public static function codes(): array
    {
        return array_map(
            fn (self $role): string => $role->code(),
            self::cases(),
        );
    }

    public function label(): string
    {
        return match ($this) {
            self::Admin => 'Admin',
            self::Manager => 'Manager',
        };
    }

    public function code(): string
    {
        return strtoupper($this->value);
    }

    public static function middleware(self ...$roles): string
    {
        return 'role:'.implode(',', array_map(
            fn (self $role): string => $role->code(),
            $roles,
        ));
    }

    public function description(): string
    {
        return match ($this) {
            self::Admin => 'Full access to all platform permissions.',
            self::Manager => 'Limited operational access to customers, bookings, and reports.',
        };
    }

    /**
     * @return list<PermissionSlug>
     */
    public function permissions(): array
    {
        return match ($this) {
            self::Admin => PermissionSlug::cases(),
            self::Manager => [
                PermissionSlug::UsersView,
                PermissionSlug::CustomersView,
                PermissionSlug::CustomersCreate,
                PermissionSlug::CustomersUpdate,
                PermissionSlug::AgentsView,
                PermissionSlug::AgentsCreate,
                PermissionSlug::AgentsUpdate,
                PermissionSlug::UnitsView,
                PermissionSlug::BookingsView,
                PermissionSlug::BookingsCreate,
                PermissionSlug::BookingsUpdate,
                PermissionSlug::ReportsView,
            ],
        };
    }
}
