<?php

namespace App\Filament\Resources\Users\Schemas;

use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Schemas\Schema;

class UserForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                Select::make('plan_id')
                    ->relationship('plan', 'name'),
                TextInput::make('name')
                    ->required(),
                TextInput::make('email')
                    ->label('Email address')
                    ->email()
                    ->required(),
                Select::make('email_verified_at')
                    ->label('Email verification')
                    ->options([
                        'verified' => 'Verified',
                        'unverified' => 'Unverified',
                    ])
                    ->formatStateUsing(fn (mixed $state): string => filled($state) ? 'verified' : 'unverified')
                    ->dehydrateStateUsing(fn (string $state): mixed => $state === 'verified' ? now() : null)
                    ->native(false)
                    ->required()
                    ->helperText('Admin can manually verify or unverify this user without requiring an email code.'),
                TextInput::make('password')
                    ->password()
                    ->required(fn (string $operation): bool => $operation === 'create')
                    ->dehydrated(fn (?string $state): bool => filled($state)),
                Select::make('role')
                    ->options([
                        'user' => 'User',
                        'admin' => 'Admin',
                    ])
                    ->required()
                    ->default('user'),
                Select::make('status')
                    ->options([
                        'active' => 'Active',
                        'suspended' => 'Suspended',
                        'banned' => 'Banned',
                    ])
                    ->required()
                    ->default('active'),
                TextInput::make('avatar'),
                TextInput::make('timezone')
                    ->required()
                    ->default('UTC'),
            ]);
    }
}
