<?php

namespace App\Filament\Resources\Users\Schemas;

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

class UserForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                Section::make('User Account Details')
                    ->schema([
                        TextInput::make('name')
                            ->required(),
                        TextInput::make('email')
                            ->label('Email Address')
                            ->email()
                            ->required()
                            ->unique(ignoreRecord: true),
                        Select::make('role')
                            ->options([
                                'admin' => 'Admin',
                                'staff' => 'Staff / Manager',
                                'customer' => 'Customer',
                                'reseller' => 'Reseller (রিসেলার)',
                            ])
                            ->required()
                            ->live() // dynamic update
                            ->default('customer'),
                        TextInput::make('password')
                            ->password()
                            ->dehydrateStateUsing(fn ($state) => Hash::make($state))
                            ->dehydrated(fn ($state) => filled($state))
                            ->required(fn (string $operation): bool => $operation === 'create')
                            ->helperText(fn (string $operation): ?string => $operation === 'edit' ? 'Leave blank to keep current password.' : null),
                    ])->columns(2),

                Section::make('Reseller System Details')
                    ->description('Set status, validity, and package details for this reseller.')
                    ->visible(fn (callable $get) => $get('role') === 'reseller')
                    ->schema([
                        Select::make('reseller_status')
                            ->label('Reseller Status')
                            ->options([
                                'pending' => 'Pending',
                                'active' => 'Active',
                                'expired' => 'Expired',
                                'suspended' => 'Suspended',
                            ])
                            ->default('pending')
                            ->live()
                            ->afterStateUpdated(function ($state, $set, $get) {
                                if ($state === 'active' && !$get('reseller_expires_at')) {
                                    $days = 365;
                                    if ($packageId = $get('reseller_package_id')) {
                                        $package = \App\Models\ResellerPackage::find($packageId);
                                        if ($package) {
                                            $days = $package->validity_days;
                                        }
                                    }
                                    $set('reseller_expires_at', now()->addDays($days)->format('Y-m-d'));
                                }
                            }),

                        Select::make('reseller_package_id')
                            ->label('Membership Package')
                            ->options(\App\Models\ResellerPackage::pluck('name', 'id'))
                            ->nullable()
                            ->live()
                            ->afterStateUpdated(function ($state, $set) {
                                if ($state) {
                                    $package = \App\Models\ResellerPackage::find($state);
                                    if ($package) {
                                        $set('reseller_expires_at', now()->addDays($package->validity_days)->format('Y-m-d'));
                                    }
                                }
                            }),

                        DatePicker::make('reseller_expires_at')
                            ->label('Expiration Date')
                            ->nullable()
                            ->default(now()->addYear()),
                    ])
                    ->columns(3),
            ]);
    }
}
