<?php

namespace App\Filament\Resources;

use App\Filament\Resources\SportMatchResource\Pages;
use App\Models\SportMatch;
use Filament\Actions;
use Filament\Forms\Components\DateTimePicker;
use Filament\Forms\Components\Select;
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 SportMatchResource extends Resource
{
    protected static ?string $model = SportMatch::class;
    protected static ?string $label = 'Sport Match';

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

    public static function form(Schema $schema): Schema
    {
        return $schema->components([
            Section::make('Match Info')->schema([
                Select::make('sport')
                    ->options(['football' => 'Football', 'basketball' => 'Basketball', 'tennis' => 'Tennis', 'volleyball' => 'Volleyball', 'hockey' => 'Ice Hockey', 'esport' => 'ESports'])
                    ->required(),
                TextInput::make('country'),
                TextInput::make('league')->required(),
                TextInput::make('home_team')->required()->label('Home Team'),
                TextInput::make('away_team')->required()->label('Away Team'),
                DateTimePicker::make('start_time')->label('Match Time'),
                Select::make('status')
                    ->options(['upcoming' => 'Upcoming', 'live' => 'Live', 'finished' => 'Finished'])
                    ->required()->default('upcoming'),
            ])->columns(2),

            Section::make('Odds')->schema([
                TextInput::make('home_odds')->numeric()->required()->label('Home Odds')->step(0.01),
                TextInput::make('draw_odds')->numeric()->label('Draw Odds')->step(0.01),
                TextInput::make('away_odds')->numeric()->required()->label('Away Odds')->step(0.01),
                TextInput::make('markets_count')->numeric()->default(1)->label('Markets'),
            ])->columns(4),

            Section::make('Live Score')->schema([
                TextInput::make('live_minute')->numeric()->label('Minute'),
                TextInput::make('home_score')->label('Home Score'),
                TextInput::make('away_score')->label('Away Score'),
                TextInput::make('set_score')->label('Set Score (Tennis)'),
            ])->columns(4),

            Toggle::make('is_active')->default(true),
        ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('sport')->badge()
                    ->color(fn($state) => match($state) {
                        'football' => 'success', 'basketball' => 'warning',
                        'tennis' => 'info', 'esport' => 'danger', default => 'gray',
                    }),
                Tables\Columns\TextColumn::make('league')->searchable(),
                Tables\Columns\TextColumn::make('home_team')->searchable()->label('Home'),
                Tables\Columns\TextColumn::make('away_team')->searchable()->label('Away'),
                Tables\Columns\TextColumn::make('home_odds')->numeric(2)->label('1'),
                Tables\Columns\TextColumn::make('draw_odds')->numeric(2)->label('X'),
                Tables\Columns\TextColumn::make('away_odds')->numeric(2)->label('2'),
                Tables\Columns\TextColumn::make('status')->badge()
                    ->color(fn($state) => match($state) {
                        'live' => 'danger', 'upcoming' => 'warning', 'finished' => 'gray', default => 'gray',
                    }),
                Tables\Columns\TextColumn::make('start_time')->dateTime()->sortable(),
                Tables\Columns\IconColumn::make('is_active')->boolean(),
            ])
            ->filters([
                Tables\Filters\SelectFilter::make('sport')
                    ->options(['football' => 'Football', 'basketball' => 'Basketball', 'tennis' => 'Tennis', 'esport' => 'ESports']),
                Tables\Filters\SelectFilter::make('status')
                    ->options(['upcoming' => 'Upcoming', 'live' => 'Live', 'finished' => 'Finished']),
            ])
            ->recordActions([
                Actions\EditAction::make(),
                Actions\DeleteAction::make(),
            ])
            ->toolbarActions([
                Actions\BulkActionGroup::make([Actions\DeleteBulkAction::make()]),
            ])
            ->defaultSort('start_time', 'asc');
    }

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