<?php

namespace App\Filament\Resources\Users\Schemas;

use Filament\Forms\Components\DateTimePicker;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Schemas\Schema;
use Illuminate\Support\Facades\Hash;

class UserForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                TextInput::make('name')
                    ->label('Nama Lengkap') 
                    ->required(),

                TextInput::make('email')
                    ->label('Alamat Email') 
                    ->email()
                    ->required()
                    // Mencegah email User Utama diubah sembarangan
                    ->disabled(fn ($record) => $record && $record->id === 1),

                DateTimePicker::make('email_verified_at')
                    ->label('Email Diverifikasi Pada')
                    ->hidden(),

                TextInput::make('password')
                    ->label('Kata Sandi') 
                    ->password()
                    ->required(fn (string $context): bool => $context === 'create')
                    ->dehydrated(fn ($state) => filled($state))
                    ->dehydrateStateUsing(fn ($state) => Hash::make($state))
                    ->minLength(8)
                    ->maxLength(255),
                
                Select::make('roles')
                    ->label('Role / Jabatan') 
                    ->relationship('roles', 'name')
                    ->preload()
                    ->searchable()               
                    ->disabled(fn ($record) => $record && $record->id === 2 || $record && $record->id === 7)
                    ->dehydrated(fn ($record) => !($record && $record->hasRole('super_admin'))),
        ]);
    }
}