<?php

namespace App\Filament\Resources\ResellerWithdrawRequests\Schemas;

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

class ResellerWithdrawRequestForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                Section::make('Payout Request Details')
                    ->schema([
                        Select::make('user_id')
                            ->label('Select Reseller')
                            ->options(\App\Models\User::where('role', 'reseller')->pluck('name', 'id'))
                            ->required()
                            ->disabled(fn ($record) => $record !== null) // Disable in Edit mode
                            ->reactive()
                            ->afterStateUpdated(function ($state, callable $set, callable $get) {
                                if ($state) {
                                    $reseller = \App\Models\User::find($state);
                                    if ($reseller) {
                                        // Auto-fill wallet balance
                                        $balance = $reseller->getWalletBalance();
                                        $set('amount', $balance);
                                        
                                        // Auto-fill account number if payment method is already selected
                                        $method = $get('payment_method');
                                        if ($method) {
                                            if ($method === 'bkash') {
                                                $set('account_number', $reseller->reseller_bkash_number);
                                            } elseif ($method === 'nagad') {
                                                $set('account_number', $reseller->reseller_nagad_number);
                                            } elseif ($method === 'rocket') {
                                                $set('account_number', $reseller->reseller_rocket_number);
                                            } elseif ($method === 'bank') {
                                                $set('account_number', $reseller->reseller_bank_details);
                                            }
                                        }
                                    }
                                }
                            }),

                        TextInput::make('amount')
                            ->label('Withdraw Amount (৳)')
                            ->prefix('৳')
                            ->numeric()
                            ->required(),

                        Select::make('payment_method')
                            ->label('Payment Method')
                            ->options([
                                'bkash' => 'bKash (বিকাশ)',
                                'nagad' => 'Nagad (নগদ)',
                                'rocket' => 'Rocket (রকেট)',
                                'bank' => 'Bank Transfer (ব্যাংক)',
                            ])
                            ->required()
                            ->reactive()
                            ->afterStateUpdated(function ($state, callable $set, callable $get) {
                                $userId = $get('user_id');
                                if ($userId) {
                                    $reseller = \App\Models\User::find($userId);
                                    if ($reseller) {
                                        if ($state === 'bkash') {
                                            $set('account_number', $reseller->reseller_bkash_number);
                                        } elseif ($state === 'nagad') {
                                            $set('account_number', $reseller->reseller_nagad_number);
                                        } elseif ($state === 'rocket') {
                                            $set('account_number', $reseller->reseller_rocket_number);
                                        } elseif ($state === 'bank') {
                                            $set('account_number', $reseller->reseller_bank_details);
                                        }
                                    }
                                }
                            }),

                        TextInput::make('account_number')
                            ->label('Account Number / Details')
                            ->required(),

                        Select::make('status')
                            ->label('Update Status')
                            ->options([
                                'pending' => 'Pending',
                                'approved' => 'Approved',
                                'rejected' => 'Rejected',
                            ])
                            ->required()
                            ->disabled(fn ($record) => $record?->status === 'approved'), // Prevent editing approved requests to avoid double-debiting!

                        Textarea::make('admin_notes')
                            ->label('Admin Notes / Transaction ID')
                            ->placeholder('Enter transaction ID or rejection reason')
                            ->rows(3)
                            ->columnSpanFull(),
                    ])
                    ->columns(2)
                    ->columnSpanFull(),
            ]);
    }
}
