<?php

namespace App\Filament\Resources\Users\Schemas;

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

class UserForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                Section::make('Profile')
                    ->columns(2)
                    ->schema([
                        TextInput::make('name')
                            ->label('Full Name')
                            ->required()
                            ->columnSpan(1),

                        TextInput::make('phone')
                            ->label('Phone Number')
                            ->tel()
                            ->required()
                            ->helperText('International format e.g. +255712345678')
                            ->columnSpan(1),

                        TextInput::make('email')
                            ->label('Email Address')
                            ->email()
                            ->columnSpan(1),

                        Select::make('role')
                            ->label('Account Role')
                            ->options([
                                'driver' => 'Driver',
                                'rider'  => 'Rider',
                                'both'   => 'Driver & Rider',
                            ])
                            ->default('rider')
                            ->required()
                            ->columnSpan(1),

                        Select::make('lang')
                            ->label('Preferred Language')
                            ->options([
                                'sw' => 'Swahili',
                                'en' => 'English',
                            ])
                            ->default('sw')
                            ->required()
                            ->columnSpan(1),

                        TextInput::make('emergency_contact_phone')
                            ->label('Emergency Contact Phone')
                            ->tel()
                            ->columnSpan(1),
                    ]),

                Section::make('Account Status')
                    ->columns(2)
                    ->schema([
                        Toggle::make('is_verified')
                            ->label('Phone Verified')
                            ->columnSpan(1),

                        Toggle::make('is_suspended')
                            ->label('Account Suspended')
                            ->helperText('Suspended users cannot log in or post trips')
                            ->columnSpan(1),

                        Toggle::make('is_admin')
                            ->label('Admin Access (can access this panel)')
                            ->columnSpan(1),
                    ]),

                Section::make('Credits & Rating')
                    ->columns(3)
                    ->schema([
                        TextInput::make('trip_credits')
                            ->label('Trip Credits')
                            ->helperText('1 credit = 1 posted trip. Add credits manually here.')
                            ->required()
                            ->numeric()
                            ->minValue(0)
                            ->default(0)
                            ->columnSpan(1),

                        TextInput::make('rating_avg')
                            ->label('Average Rating')
                            ->helperText('Calculated automatically from reviews')
                            ->numeric()
                            ->readOnly()
                            ->default(0.0)
                            ->columnSpan(1),

                        TextInput::make('strike_count')
                            ->label('Strike Count')
                            ->helperText('3 strikes = account review')
                            ->required()
                            ->numeric()
                            ->minValue(0)
                            ->default(0)
                            ->columnSpan(1),
                    ]),

                Section::make('Password (Admin Reset)')
                    ->schema([
                        TextInput::make('password')
                            ->label('New Password')
                            ->password()
                            ->helperText('Leave blank to keep existing password')
                            ->dehydrated(fn($state) => filled($state))
                            ->dehydrateStateUsing(fn($state) => bcrypt($state)),
                    ]),
            ]);
    }
}
