<?php

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

use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Modules\Payment\Enums\RefundStatus;

class RefundsTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->modifyQueryUsing(fn (Builder $query) => $query->with(['payment.booking', 'requestedBy']))
            ->defaultSort('created_at', 'desc')
            ->columns([
                TextColumn::make('payment.booking.booking_ref')
                    ->label('Booking')
                    ->searchable()
                    ->sortable(),
                TextColumn::make('payment.gateway')
                    ->label('Gateway')
                    ->badge(),
                TextColumn::make('status')
                    ->badge()
                    ->color(fn (RefundStatus $state) => match ($state) {
                        RefundStatus::Pending => 'warning',
                        RefundStatus::Completed => 'success',
                        RefundStatus::Failed => 'danger',
                    }),
                TextColumn::make('amount')
                    ->numeric(2)
                    ->sortable(),
                TextColumn::make('reason')
                    ->limit(40)
                    ->tooltip(fn (?string $state) => $state),
                TextColumn::make('gateway_refund_id')
                    ->label('Gateway Refund ID')
                    ->placeholder('—')
                    ->toggleable(isToggledHiddenByDefault: true),
                TextColumn::make('requestedBy.name')
                    ->label('Requested By')
                    ->placeholder('—'),
                TextColumn::make('requested_at')
                    ->dateTime()
                    ->sortable(),
                TextColumn::make('completed_at')
                    ->dateTime()
                    ->placeholder('—')
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                SelectFilter::make('status')
                    ->options(array_combine(
                        array_map(fn (RefundStatus $status) => $status->value, RefundStatus::cases()),
                        array_map(fn (RefundStatus $status) => str($status->value)->headline()->toString(), RefundStatus::cases()),
                    )),
            ]);
    }
}
