<?php

namespace Modules\Payment\Filament\Resources\Payments\Tables;

use Filament\Actions\ViewAction;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Modules\Payment\Enums\PaymentMethod;
use Modules\Payment\Enums\PaymentStatus;

class PaymentsTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->modifyQueryUsing(fn (Builder $query) => $query->with('booking'))
            ->defaultSort('created_at', 'desc')
            ->columns([
                TextColumn::make('booking.booking_ref')
                    ->label('Booking')
                    ->searchable()
                    ->sortable(),
                TextColumn::make('gateway')
                    ->badge(),
                TextColumn::make('status')
                    ->badge()
                    ->color(fn (PaymentStatus $state) => match ($state) {
                        PaymentStatus::Pending => 'warning',
                        PaymentStatus::Completed => 'success',
                        PaymentStatus::Failed => 'danger',
                    }),
                TextColumn::make('amount')
                    ->numeric(2)
                    ->sortable(),
                TextColumn::make('currency'),
                TextColumn::make('gateway_transaction_id')
                    ->label('Gateway Txn ID')
                    ->searchable()
                    ->toggleable(isToggledHiddenByDefault: true),
                TextColumn::make('initiated_at')
                    ->dateTime()
                    ->sortable(),
                TextColumn::make('completed_at')
                    ->dateTime()
                    ->placeholder('—')
                    ->sortable(),
            ])
            ->filters([
                SelectFilter::make('status')
                    ->options(array_combine(
                        array_map(fn (PaymentStatus $status) => $status->value, PaymentStatus::cases()),
                        array_map(fn (PaymentStatus $status) => str($status->value)->headline()->toString(), PaymentStatus::cases()),
                    )),
                SelectFilter::make('gateway')
                    ->options(array_combine(
                        array_map(fn (PaymentMethod $method) => $method->value, PaymentMethod::cases()),
                        array_map(fn (PaymentMethod $method) => str($method->value)->headline()->toString(), PaymentMethod::cases()),
                    )),
            ])
            ->recordActions([
                ViewAction::make(),
            ]);
    }
}
