<?php

namespace App\Filament\Resources\RoleResource\Pages;

use App\Filament\Resources\RoleResource;
use App\Models\Role;
use Filament\Infolists\Components\IconEntry;
use Filament\Infolists\Components\TextEntry;
use Filament\Resources\Pages\ViewRecord;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;

/**
 * Ported from RoleController::show().
 */
class ViewRole extends ViewRecord
{
    protected static string $resource = RoleResource::class;

    public function infolist(Schema $schema): Schema
    {
        return $schema->components([
            Section::make()->schema([
                TextEntry::make('name'),
                TextEntry::make('description')->placeholder('—'),
                IconEntry::make('is_system')->boolean()->label('System role'),
                IconEntry::make('is_full_admin')->boolean()->label('Full admin'),
                IconEntry::make('is_active')->boolean(),
                TextEntry::make('assigned_user_count')
                    ->label('Assigned users')
                    ->state(fn (Role $record): int => $record->assignedUserCount()),
            ])->columns(3),

            Section::make('Permissions')->schema([
                TextEntry::make('permissions')
                    ->label('')
                    ->state(fn (Role $record): array => $record->permissions()->pluck('permissions.name')->all())
                    ->listWithLineBreaks()
                    ->placeholder('No permissions granted.'),
            ])->visible(fn (Role $record): bool => ! $record->is_full_admin),
        ]);
    }
}
