<?php

namespace App\Filament\Resources\PaymentAuditLog\Tables;

use App\Enums\PaymentAuditEvent;
use App\Filament\Resources\Credits\CreditResource;
use App\Filament\Resources\PaymentAuditLog\PaymentAuditLogResource;
use App\Filament\Resources\Payments\PaymentResource;
use App\Models\Company;
use App\Models\PaymentAuditLog;
use App\Support\Format;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Support\Facades\Auth;

class PaymentAuditLogTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('id')
                    ->label('#')
                    ->sortable(),

                Tables\Columns\TextColumn::make('payment.id')
                    ->label('Pago')
                    ->sortable()
                    ->searchable()
                    ->url(fn ($record) => $record->payment_id
                        ? PaymentResource::getUrl(
                            'view',
                            ['record' => $record->payment_id]
                        )
                        : null
                    ),

                Tables\Columns\TextColumn::make('credit.id')
                    ->label('Crédito')
                    ->sortable()
                    ->searchable()
                    ->url(fn ($record) => $record->credit_id
                        ? CreditResource::getUrl(
                            'view',
                            ['record' => $record->credit_id]
                        )
                        : null
                    ),

                Tables\Columns\TextColumn::make('user.name')
                    ->label('Usuario')
                    ->sortable()
                    ->searchable(),

                Tables\Columns\TextColumn::make('action')
                    ->label('Acción')
                    ->badge()
                    ->formatStateUsing(fn (string $state): string => PaymentAuditEvent::tryFrom($state)?->label() ?? ucfirst($state))
                    ->color(fn (string $state): string => PaymentAuditEvent::tryFrom($state)?->color() ?? 'gray')
                    ->icon(fn (string $state): ?string => PaymentAuditEvent::tryFrom($state)?->icon())
                    ->sortable(),

                Tables\Columns\TextColumn::make('amount')
                    ->label('Monto')
                    ->formatStateUsing(fn ($state) => Format::money($state)),

                Tables\Columns\TextColumn::make('created_at')
                    ->label('Fecha')
                    ->dateTime('d/m/Y H:i')
                    ->sortable(),
            ])

            ->filters([
                Tables\Filters\SelectFilter::make('action')
                    ->label('Acción')
                    ->options(
                        collect(PaymentAuditEvent::cases())
                            ->mapWithKeys(fn (PaymentAuditEvent $event) => [
                                $event->value => $event->label(),
                            ])
                            ->toArray()
                    ),

                Tables\Filters\SelectFilter::make('company_id')
                    ->label('Compañía')
                    ->options(
                        fn () => Auth::user()->isSuperAdmin()
                            ? Company::pluck('name', 'id')->toArray()
                            : []
                    )
                    ->visible(fn () => Auth::user()->isSuperAdmin()),
            ])

            ->defaultSort('created_at', 'desc')
            ->paginated([25, 50, 100])
            ->recordUrl(fn (PaymentAuditLog $record) => PaymentAuditLogResource::getUrl(
                'view',
                ['record' => $record]
            ));
    }
}
