<?php

namespace App\Filament\Resources\Coupons\Schemas;

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

class CouponForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                Grid::make(3)
                    ->schema([

                        // ── Left / Main Column (span 2) ──────────────────────
                        Grid::make(1)
                            ->schema([
                                Section::make('Coupon Details')
                                    ->description('Set up coupon codes, value type, and discount amount.')
                                    ->schema([
                                        TextInput::make('code')
                                            ->required()
                                            ->unique(ignoreRecord: true)
                                            ->dehydrateStateUsing(fn ($state) => strtoupper($state))
                                            ->placeholder('e.g. DISCOUNT20'),
                                        Select::make('type')
                                            ->options([
                                                'fixed'      => 'Fixed Amount (BDT)',
                                                'percentage' => 'Percentage (%)',
                                            ])
                                            ->required()
                                            ->default('fixed'),
                                        TextInput::make('value')
                                            ->label('Discount Value')
                                            ->required()
                                            ->numeric()
                                            ->prefix(fn (callable $get) => $get('type') === 'percentage' ? '%' : '৳'),
                                        TextInput::make('min_amount')
                                            ->label('Minimum Spend Required (BDT)')
                                            ->required()
                                            ->numeric()
                                            ->default(0.0)
                                            ->prefix('৳'),
                                    ])
                                    ->columns(2),

                                Section::make('Validity Period')
                                    ->description('Define when this coupon is valid.')
                                    ->schema([
                                        DateTimePicker::make('starts_at')
                                            ->label('Valid From'),
                                        DateTimePicker::make('expires_at')
                                            ->label('Expires At'),
                                    ])
                                    ->columns(2),
                            ])
                            ->columnSpan(2),

                        // ── Right Sidebar Column (span 1) ────────────────────
                        Grid::make(1)
                            ->schema([
                                Section::make('Status')
                                    ->schema([
                                        Toggle::make('is_active')
                                            ->label('Active')
                                            ->default(true)
                                            ->required()
                                            ->columnSpanFull(),
                                    ]),

                                Section::make('Record Info')
                                    ->schema([
                                        Placeholder::make('created_at')
                                            ->label('Created at')
                                            ->content(fn ($record): string => $record?->created_at
                                                ? $record->created_at->diffForHumans()
                                                : '-'),
                                        Placeholder::make('updated_at')
                                            ->label('Last modified at')
                                            ->content(fn ($record): string => $record?->updated_at
                                                ? $record->updated_at->diffForHumans()
                                                : '-'),
                                    ])
                                    ->hidden(fn ($record) => $record === null),
                            ])
                            ->columnSpan(1),

                    ])
                    ->columnSpanFull(),
            ]);
    }
}
