<?php

namespace App\Filament\Resources;

use App\Filament\Resources\TransactionResource\Pages;
use App\Models\Transaction;
use Filament\Actions;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Tables;
use Filament\Tables\Table;

class TransactionResource extends Resource
{
    protected static ?string $model = Transaction::class;

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

    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('type')->badge()
                    ->color(fn($state) => match($state) {
                        'deposit' => 'success', 'withdrawal' => 'warning',
                        'bet' => 'danger', 'win' => 'success',
                        'bonus' => 'info', 'refund' => 'gray', default => 'gray',
                    }),
                Tables\Columns\TextColumn::make('amount')->money('USD')->sortable(),
                Tables\Columns\TextColumn::make('balance_before')->money('USD')->label('Balance Before'),
                Tables\Columns\TextColumn::make('balance_after')->money('USD')->label('Balance After'),
                Tables\Columns\TextColumn::make('paymentMethod.name')->label('Method')->badge(),
                Tables\Columns\TextColumn::make('status')->badge()
                    ->color(fn($state) => match($state) {
                        'completed' => 'success', 'pending' => 'warning',
                        'failed' => 'danger', 'cancelled' => 'gray', default => 'gray',
                    }),
                Tables\Columns\TextColumn::make('processed_at')->dateTime()->sortable(),
            ])
            ->filters([
                Tables\Filters\SelectFilter::make('type')
                    ->options(['deposit' => 'Deposit', 'withdrawal' => 'Withdrawal', 'bet' => 'Bet', 'win' => 'Win']),
                Tables\Filters\SelectFilter::make('status')
                    ->options(['pending' => 'Pending', 'completed' => 'Completed', 'failed' => 'Failed']),
            ])
            ->recordActions([Actions\ViewAction::make()])
            ->defaultSort('created_at', 'desc');
    }

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

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