<?php

namespace App\Filament\Resources\CreditResource\RelationManagers;

use App\Enums\PaymentAuditEvent;
use App\Filament\Resources\PaymentAuditLog\PaymentAuditLogResource;
use App\Models\PaymentAuditLog;
use App\Support\Format;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Table;

class PaymentAuditLogsRelationManager extends RelationManager
{
    protected static string $relationship = 'paymentAuditLogs';

    protected static ?string $title = 'Auditoría de Pagos';

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

                Tables\Columns\TextColumn::make('payment.id')
                    ->label('Pago')
                    ->sortable(),

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

                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')
                    ->sortable(),

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

                Tables\Columns\TextColumn::make('created_at')
                    ->label('Fecha')
                    ->dateTime('d/m/Y H:i')
                    ->sortable(),
            ])
            ->defaultSort('created_at', 'desc')
            ->paginated([10, 25, 50])
            ->recordUrl(fn (PaymentAuditLog $record) => PaymentAuditLogResource::getUrl('view', ['record' => $record])
            )
            ->headerActions([])  // 🔒 solo lectura
            ->actions([])        // sin editar/eliminar
            ->bulkActions([]);   // sin bulk
    }
}
