<?php

namespace App\Filament\Resources;

use App\Filament\Resources\BetResource\Pages;
use App\Models\Bet;
use Filament\Actions;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Tables;
use Filament\Tables\Table;

class BetResource extends Resource
{
    protected static ?string $model = Bet::class;

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

    public static function form(Schema $schema): Schema
    {
        return $schema->components([]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('id')->sortable(),
                Tables\Columns\TextColumn::make('reference')->searchable()->copyable(),
                Tables\Columns\TextColumn::make('user.username')->label('Player')->searchable()->sortable(),
                Tables\Columns\TextColumn::make('stake')->money('USD')->sortable(),
                Tables\Columns\TextColumn::make('total_odds')->label('Odds')->numeric(decimalPlaces: 2)->sortable(),
                Tables\Columns\TextColumn::make('potential_payout')->money('USD')->label('Potential')->sortable(),
                Tables\Columns\TextColumn::make('actual_payout')->money('USD')->label('Payout')->sortable(),
                Tables\Columns\TextColumn::make('type')->badge()
                    ->color(fn($state) => $state === 'parlay' ? 'info' : 'gray'),
                Tables\Columns\TextColumn::make('status')->badge()
                    ->color(fn($state) => match($state) {
                        'won' => 'success', 'lost' => 'danger',
                        'pending' => 'warning', 'cancelled' => 'gray', default => 'gray',
                    }),
                Tables\Columns\TextColumn::make('settled_at')->dateTime()->sortable(),
                Tables\Columns\TextColumn::make('created_at')->dateTime()->sortable(),
            ])
            ->filters([
                Tables\Filters\SelectFilter::make('status')
                    ->options(['pending' => 'Pending', 'won' => 'Won', 'lost' => 'Lost', 'cancelled' => 'Cancelled']),
                Tables\Filters\SelectFilter::make('type')
                    ->options(['single' => 'Single', 'parlay' => 'Parlay']),
            ])
            ->recordActions([Actions\ViewAction::make()])
            ->defaultSort('created_at', 'desc');
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListBets::route('/'),
        ];
    }

    public static function canCreate(): bool { return false; }
}
