<?php

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

use Filament\Infolists\Components\TextEntry;
use Filament\Schemas\Components\Grid;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Modules\Payment\Enums\PaymentStatus;

class PaymentInfolist
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                Section::make('Payment')
                    ->schema([
                        Grid::make(4)
                            ->schema([
                                TextEntry::make('booking.booking_ref')->label('Booking'),
                                TextEntry::make('gateway')->badge(),
                                TextEntry::make('status')
                                    ->badge()
                                    ->color(fn (PaymentStatus $state) => match ($state) {
                                        PaymentStatus::Pending => 'warning',
                                        PaymentStatus::Completed => 'success',
                                        PaymentStatus::Failed => 'danger',
                                    }),
                                TextEntry::make('gateway_transaction_id')->label('Gateway Txn ID'),
                                TextEntry::make('amount')->numeric(2),
                                TextEntry::make('currency'),
                                TextEntry::make('initiated_at')->dateTime(),
                                TextEntry::make('completed_at')->dateTime()->placeholder('—'),
                            ]),
                    ]),
                // Raw gateway response — may include data not meant for the
                // support role, so it's gated the same as refund initiation
                // (process_refunds: admin/super_admin only, domain.md §6).
                Section::make('Gateway Response')
                    ->visible(fn () => auth()->user()?->can('process_refunds') ?? false)
                    ->schema([
                        TextEntry::make('gateway_payload')
                            ->label('')
                            ->formatStateUsing(fn (mixed $state) => match (true) {
                                is_array($state) => json_encode($state, JSON_PRETTY_PRINT),
                                is_string($state) && $state !== '' => $state,
                                default => null,
                            })
                            ->placeholder('—')
                            ->columnSpanFull(),
                    ]),
            ]);
    }
}
