<?php

namespace App\Filament\Resources\Master\Users\Schemas;

use App\Enums\RoleEnum;
use App\Filament\Support\CheerfulNotification;
use Filament\Forms\Components\Hidden;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Schemas\Components\Grid;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\HtmlString;

class UserForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                Section::make()
                    ->description(function (string $operation) {
                        return $operation === 'create'
                            ? new HtmlString(CheerfulNotification::getByKey('user.create_form_desc'))
                            : null;
                    })
                    ->schema([
                        TextInput::make('name')
                            ->label('Nama Lengkap')
                            ->placeholder('John Doe')
                            ->autocomplete(false)
                            ->autofocus()
                            ->required()
                            ->maxLength(100),

                        Grid::make(1)
                            ->schema([
                                TextInput::make('email')
                                    ->label('Alamat Surel')
                                    ->placeholder('johndoe@example.com')
                                    ->autocomplete(false)
                                    ->required()
                                    ->maxLength(254)
                                    ->email()
                                    ->unique(ignoreRecord: true)
                                    ->prefixIcon('heroicon-o-envelope'),

                                TextInput::make('username')
                                    ->label('Nama Pengguna')
                                    ->placeholder('johndoe')
                                    ->autocomplete(false)
                                    ->required()
                                    ->minLength(5)
                                    ->maxLength(20)
                                    ->regex('/^[a-zA-Z0-9_.-]+$/')
                                    ->unique(ignoreRecord: true)
                                    ->prefixIcon('heroicon-o-at-symbol'),

                                Select::make('roles')
                                    ->label('Peran')
                                    ->required()
                                    ->relationship('roles', 'name', function (Builder $query): Builder {
                                        return $query->whereNotIn('name', [RoleEnum::DEVELOPER, RoleEnum::PERUSAHAAN]);
                                    })
                                    ->multiple()
                                    ->preload()
                                    ->searchable(),

                                Hidden::make('password')
                                    ->default(fn (): string => config('auth.password_default'))
                                    ->visibleOn('create'),
                            ]),
                    ]),
            ])
            ->columns(1);
    }
}
