<?php

namespace App\Filament\Widgets;

use App\Models\Transaction;
use Filament\Tables;
use Filament\Tables\Table;
use Filament\Widgets\TableWidget as BaseWidget;

class RecentTransactionsWidget extends BaseWidget
{
    protected static ?int $sort = 6;
    protected static ?string $heading = 'Recent Transactions';
    protected int | string | array $columnSpan = 'full';

    public function table(Table $table): Table
    {
        return $table
            ->query(Transaction::with(['user', 'paymentMethod'])->latest()->limit(15))
            ->columns([
                Tables\Columns\TextColumn::make('reference')
                    ->copyable()
                    ->label('Reference')
                    ->searchable(),
                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',
                        default      => 'gray',
                    }),
                Tables\Columns\TextColumn::make('amount')
                    ->money('USD')
                    ->sortable(),
                Tables\Columns\TextColumn::make('balance_before')
                    ->money('USD')
                    ->label('Before'),
                Tables\Columns\TextColumn::make('balance_after')
                    ->money('USD')
                    ->label('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',
                        default     => 'gray',
                    }),
                Tables\Columns\TextColumn::make('created_at')
                    ->dateTime()
                    ->label('Time')
                    ->sortable(),
            ])
            ->paginated(false)
            ->defaultSort('created_at', 'desc');
    }
}
