<?php

namespace App\Filament\Resources\Coupons\Schemas;

use Filament\Forms\Components\DateTimePicker;
use Filament\Forms\Components\KeyValue;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Schemas\Components\Grid;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Illuminate\Support\Str;

class CouponForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                Section::make('Basic Information')
                    ->schema([
                        Grid::make(2)
                            ->schema([
                                TextInput::make('code')
                                    ->label('Coupon Code')
                                    ->required()
                                    ->unique(ignoreRecord: true)
                                    ->formatStateUsing(fn ($state) => Str::upper($state))
                                    ->helperText('This code will be entered by customers to apply the discount'),

                                TextInput::make('name')
                                    ->label('Display Name')
                                    ->required()
                                    ->helperText('Internal name for this coupon'),
                            ]),

                        Textarea::make('description')
                            ->label('Description')
                            ->rows(2)
                            ->helperText('Optional description for internal reference'),

                        Grid::make(2)
                            ->schema([
                                Select::make('type')
                                    ->label('Discount Type')
                                    ->options([
                                        'percentage' => 'Percentage',
                                        'fixed' => 'Fixed Amount',
                                    ])
                                    ->required()
                                    ->reactive()
                                    ->afterStateUpdated(fn ($state, callable $set) => $set('value', $state === 'percentage' ? 10 : 10.00)
                                    ),

                                TextInput::make('value')
                                    ->label('Discount Value')
                                    ->required()
                                    ->numeric()
                                    ->step(fn ($get) => $get('type') === 'percentage' ? 1 : 0.01)
                                    ->suffix(fn ($get) => $get('type') === 'percentage' ? '%' : '$')
                                    ->helperText(fn ($get) => $get('type') === 'percentage'
                                            ? 'Percentage discount (e.g., 10 for 10%)'
                                            : 'Fixed amount discount in USD'
                                    ),
                            ]),
                    ]),

                Section::make('Usage Limits')
                    ->schema([
                        Grid::make(2)
                            ->schema([
                                TextInput::make('max_uses')
                                    ->label('Maximum Uses')
                                    ->numeric()
                                    ->helperText('Leave blank for unlimited uses'),

                                TextInput::make('max_uses_per_user')
                                    ->label('Max Uses Per User')
                                    ->numeric()
                                    ->helperText('Limit how many times one user can use this coupon'),
                            ]),

                        Grid::make(2)
                            ->schema([
                                TextInput::make('minimum_amount')
                                    ->label('Minimum Order Amount')
                                    ->numeric()
                                    ->step(0.01)
                                    ->prefix('$')
                                    ->helperText('Minimum order amount required to use this coupon'),

                                TextInput::make('uses_count')
                                    ->label('Current Uses')
                                    ->numeric()
                                    ->default(0)
                                    ->disabled()
                                    ->helperText('Number of times this coupon has been used'),
                            ]),
                    ])
                    ->collapsible(),

                Section::make('Schedule')
                    ->schema([
                        Grid::make(2)
                            ->schema([
                                DateTimePicker::make('starts_at')
                                    ->label('Start Date')
                                    ->helperText('When this coupon becomes active'),

                                DateTimePicker::make('expires_at')
                                    ->label('Expiration Date')
                                    ->helperText('When this coupon expires'),
                            ]),
                    ])
                    ->collapsible(),

                Section::make('Settings')
                    ->schema([
                        Toggle::make('is_active')
                            ->label('Active')
                            ->default(true)
                            ->helperText('Only active coupons can be used'),
                    ])
                    ->collapsible(),

                Section::make('Metadata')
                    ->schema([
                        KeyValue::make('metadata')
                            ->label('Custom Metadata')
                            ->addActionLabel('Add metadata')
                            ->keyLabel('Key')
                            ->valueLabel('Value')
                            ->helperText('Additional key-value data for this coupon'),
                    ])
                    ->collapsible(),
            ]);
    }
}
