<?php

namespace App\Filament\Resources;

use App\Filament\Resources\PromotionResource\Pages;
use App\Models\Promotion;
use Filament\Actions;
use Filament\Forms\Components\DateTimePicker;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Resources\Resource;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Filament\Tables;
use Filament\Tables\Table;

class PromotionResource extends Resource
{
    protected static ?string $model = Promotion::class;

    public static function getNavigationIcon(): string { return 'heroicon-o-megaphone'; }
    public static function getNavigationGroup(): string { return 'Marketing'; }
    public static function getNavigationSort(): int { return 1; }

    public static function form(Schema $schema): Schema
    {
        return $schema->components([
            Section::make('Promotion Details')->schema([
                TextInput::make('title')->required()->maxLength(255),
                TextInput::make('slug')->required()->unique(ignoreRecord: true),
                Select::make('type')
                    ->options(['welcome' => 'Welcome', 'cashback' => 'Cashback', 'reload' => 'Reload', 'vip' => 'VIP', 'sports' => 'Sports', 'free_spin' => 'Free Spin'])
                    ->required(),
                TextInput::make('badge_text')->maxLength(50)->label('Badge Text'),
                TextInput::make('cta_text')->maxLength(50)->label('CTA Button Text'),
                TextInput::make('gradient')->maxLength(100)->label('Gradient CSS')->placeholder('from-red-600 to-red-900'),
                TextInput::make('image')->url()->label('Banner Image URL'),
                Textarea::make('description')->rows(3),
            ])->columns(2),

            Section::make('Bonus Config')->schema([
                TextInput::make('value')->numeric()->label('Value'),
                Select::make('value_type')
                    ->options(['percent' => 'Percentage (%)', 'fixed' => 'Fixed Amount ($)'])
                    ->default('percent'),
                TextInput::make('min_deposit')->numeric()->prefix('$')->label('Min Deposit'),
                TextInput::make('max_bonus')->numeric()->prefix('$')->label('Max Bonus'),
                TextInput::make('wagering_requirement')->numeric()->default(30)->label('Wagering Req (x)'),
            ])->columns(3),

            Section::make('Schedule')->schema([
                DateTimePicker::make('starts_at')->label('Starts At'),
                DateTimePicker::make('expires_at')->label('Expires At'),
                TextInput::make('sort_order')->numeric()->default(0),
                Toggle::make('is_active')->default(true),
            ])->columns(4),
        ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('sort_order')->sortable()->label('#'),
                Tables\Columns\TextColumn::make('title')->searchable()->sortable(),
                Tables\Columns\TextColumn::make('type')->badge(),
                Tables\Columns\TextColumn::make('value')
                    ->formatStateUsing(fn($state, $record) => $record->value_type === 'percent' ? "{$state}%" : "\${$state}"),
                Tables\Columns\TextColumn::make('expires_at')->dateTime()->sortable()->label('Expires'),
                Tables\Columns\IconColumn::make('is_active')->boolean()->label('Active'),
            ])
            ->reorderable('sort_order')
            ->recordActions([
                Actions\EditAction::make(),
                Actions\DeleteAction::make(),
            ])
            ->toolbarActions([
                Actions\BulkActionGroup::make([Actions\DeleteBulkAction::make()]),
            ]);
    }

    public static function getPages(): array
    {
        return [
            'index'  => Pages\ListPromotions::route('/'),
            'create' => Pages\CreatePromotion::route('/create'),
            'edit'   => Pages\EditPromotion::route('/{record}/edit'),
        ];
    }
}
