<?php

namespace App\Filament\Widgets;

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

class LatestPayments extends BaseWidget
{
    protected static ?int $sort = 4;
    protected int | string | array $columnSpan = 'full';
    protected static ?string $heading = 'Recent Payment Activity';

    public function table(Table $table): Table
    {
        return $table
            ->query(
                Transaction::query()->latest()->limit(8)
            )
            ->columns([
                Tables\Columns\TextColumn::make('payment_id')
                    ->label('Payment ID')
                    ->fontFamily('mono')
                    ->weight('bold')
                    ->copyable()
                    ->copyMessage('Payment ID copied'),

                Tables\Columns\TextColumn::make('gateway')
                    ->label('Gateway')
                    ->badge()
                    ->color(fn (string $state): string => match (strtolower(trim($state))) {
                        'bkash', 'bkash personal' => 'pink',
                        'nagad', 'nagad personal' => 'warning',
                        'rocket', 'rocket personal' => 'purple',
                        default => 'primary',
                    })
                    ->formatStateUsing(fn ($state) => strtoupper($state ?: 'DIRECT')),

                Tables\Columns\TextColumn::make('amount')
                    ->label('Amount')
                    ->weight('black')
                    ->formatStateUsing(fn ($record, $state) => setting('currency_symbol', '৳') . ' ' . number_format((float)$state, 2)),

                Tables\Columns\TextColumn::make('trx_id')
                    ->label('TrxID / Sender')
                    ->fontFamily('mono')
                    ->copyable()
                    ->copyMessage('TrxID copied')
                    ->formatStateUsing(fn ($state) => $state ?: '—')
                    ->description(fn ($record) => $record->sender_number ? 'From: ' . $record->sender_number : null),

                Tables\Columns\TextColumn::make('created_at')
                    ->label('Date & Time')
                    ->dateTime('M d, Y h:i A')
                    ->description(fn ($record) => $record->created_at?->diffForHumans()),

                Tables\Columns\TextColumn::make('status')
                    ->label('Status')
                    ->badge()
                    ->color(fn (string $state): string => match (strtoupper(trim($state))) {
                        'SUCCESS', 'COMPLETE', 'COMPLETED' => 'success',
                        'PENDING' => 'warning',
                        'CANCELLED', 'FAILED', 'REFUNDED' => 'danger',
                        default => 'gray',
                    })
                    ->icon(fn (string $state): ?string => match (strtoupper(trim($state))) {
                        'SUCCESS', 'COMPLETE', 'COMPLETED' => 'heroicon-m-check-circle',
                        'PENDING' => 'heroicon-m-clock',
                        'CANCELLED', 'FAILED', 'REFUNDED' => 'heroicon-m-x-circle',
                        default => null,
                    }),
            ])
            ->paginated(false);
    }
}
