<?php

namespace App\Filament\Resources\Resellers\Schemas;

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

class ResellerForm
{
    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([
                                'reseller' => 'Reseller (রিসেলার)',
                            ])
                            ->required()
                            ->default('reseller')
                            ->disabled()
                            ->dehydrated(),
                        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',
                                'under_review'  => 'Under Review',
                                'active'        => 'Active',
                                'rejected'      => 'Rejected',
                                '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()),

                        Textarea::make('reseller_rejection_reason')
                            ->label('Rejection Reason')
                            ->placeholder('e.g. Blurry photo, mismatch in NID details...')
                            ->visible(fn (callable $get) => $get('reseller_status') === 'rejected')
                            ->columnSpanFull(),
                    ])
                    ->columns(3),

                Section::make('Store & Social Media Details')
                    ->description('Reseller store information and social profiles (Facebook, Website, WhatsApp, Telegram).')
                    ->collapsible()
                    ->schema([
                        TextInput::make('reseller_store_name')
                            ->label('Store / Page Name')
                            ->placeholder('e.g. My E-commerce Store')
                            ->nullable(),

                        TextInput::make('reseller_website')
                            ->label('Website URL')
                            ->placeholder('e.g. https://mystore.com')
                            ->url()
                            ->nullable(),

                        TextInput::make('reseller_fb_profile')
                            ->label('Facebook Profile Link')
                            ->placeholder('e.g. https://facebook.com/username')
                            ->url()
                            ->nullable(),

                        TextInput::make('reseller_fb_page')
                            ->label('Facebook Page Link')
                            ->placeholder('e.g. https://facebook.com/pagename')
                            ->url()
                            ->nullable(),

                        TextInput::make('reseller_whatsapp')
                            ->label('WhatsApp Number')
                            ->placeholder('e.g. 017XXXXXXXX')
                            ->nullable(),

                        TextInput::make('reseller_telegram')
                            ->label('Telegram Number/Username')
                            ->placeholder('e.g. 017XXXXXXXX or @username')
                            ->nullable(),
                    ])
                    ->columns(2),

                Section::make('Payout Account Information')
                    ->description('Mobile financial service and bank account details provided by the reseller for receiving payouts.')
                    ->collapsible()
                    ->schema([
                        TextInput::make('reseller_bkash_number')
                            ->label('bKash Account Number')
                            ->placeholder('e.g. 017XXXXXXXX')
                            ->tel()
                            ->nullable(),

                        TextInput::make('reseller_nagad_number')
                            ->label('Nagad Account Number')
                            ->placeholder('e.g. 017XXXXXXXX')
                            ->tel()
                            ->nullable(),

                        TextInput::make('reseller_rocket_number')
                            ->label('Rocket Account Number')
                            ->placeholder('e.g. 017XXXXXXXXX')
                            ->tel()
                            ->nullable(),

                        Textarea::make('reseller_bank_details')
                            ->label('Bank Account Details')
                            ->placeholder('Bank Name, Branch, Routing Number, Account Name and Account Number...')
                            ->rows(3)
                            ->nullable()
                            ->columnSpanFull(),
                    ])
                    ->columns(3),

                Section::make('Verification Documents')
                    ->description('Verification documents submitted by the reseller (NID front, NID back, passport size photo, and profile picture).')
                    ->collapsible()
                    ->schema([
                        \Filament\Forms\Components\FileUpload::make('avatar')
                            ->label('Profile Picture / Avatar')
                            ->disk(config('filesystems.default', 'public'))
                            ->image()
                            ->directory('resellers')
                            ->downloadable()
                            ->nullable(),

                        \Filament\Forms\Components\FileUpload::make('reseller_nid_front')
                            ->label('NID Front Side')
                            ->disk(config('filesystems.default', 'public'))
                            ->image()
                            ->directory('resellers')
                            ->downloadable()
                            ->nullable(),

                        \Filament\Forms\Components\FileUpload::make('reseller_nid_back')
                            ->label('NID Back Side')
                            ->disk(config('filesystems.default', 'public'))
                            ->image()
                            ->directory('resellers')
                            ->downloadable()
                            ->nullable(),

                        \Filament\Forms\Components\FileUpload::make('reseller_passport_photo')
                            ->label('Passport Size Photo')
                            ->disk(config('filesystems.default', 'public'))
                            ->image()
                            ->directory('resellers')
                            ->downloadable()
                            ->nullable(),
                    ])
                    ->columns(2),
            ]);
    }
}
