<?php

namespace App\Filament\Resources\RoleTemplates\Schemas;

use App\Models\PermissionModule;
use Filament\Forms\Components\CheckboxList;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Components\Grid;
use Filament\Schemas\Schema;

class RoleTemplateForm
{
    public static function configure(Schema $schema): Schema
    {
        $modules = PermissionModule::orderBy('label')->get();

        $permissionComponents = [];
        foreach ($modules as $module) {
            $options = [];
            foreach ($module->actions as $action) {
                $label = match ($action) {
                    'view' => 'Xem',
                    'create' => 'Thêm',
                    'update' => 'Sửa',
                    'delete' => 'Xóa',
                    'restore' => 'Khôi phục',
                    'forceDelete' => 'Xóa vĩnh viễn',
                    'export' => 'Xuất Excel',
                    default => $action,
                };
                $options[$action] = $label;
            }

            $permissionComponents[] = CheckboxList::make("permissions.{$module->module}")
                ->label($module->label)
                ->options($options)
                ->columns(6)
                ->columnSpanFull();
        }

        return $schema
            ->components([
                Section::make('Thông tin nhóm')
                    ->columnSpanFull()
                    ->columns(3)
                    ->schema([
                        TextInput::make('name')
                            ->label('Tên nhóm')
                            ->required(),
                        TextInput::make('description')
                            ->label('Mô tả'),
                        Toggle::make('is_active')
                            ->label('Kích hoạt')
                            ->default(true),
                    ]),

                Section::make('Phân quyền theo module')
                    ->columnSpanFull()
                    ->schema($permissionComponents),
            ]);
    }
}
