<?php

namespace App\Filament\Resources\TransactionResource\Pages;

use App\Filament\Resources\TransactionResource;
use App\Filament\Support\StatusLookup;
use App\Models\Lookups\PaymentMethod;
use App\Models\Lookups\Status;
use App\Models\Sales\Transaction;
use App\Services\CurrentContext;
use Filament\Actions\EditAction;
use Filament\Infolists\Components\RepeatableEntry;
use Filament\Infolists\Components\TextEntry;
use Filament\Resources\Pages\ViewRecord;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;

class ViewTransaction extends ViewRecord
{
    protected static string $resource = TransactionResource::class;

    public function infolist(Schema $schema): Schema
    {
        return $schema->components([
            Section::make()->schema([
                TextEntry::make('transaction_number')->label('Transaction #'),
                TextEntry::make('customer.business_name')->label('Customer')->placeholder('—'),
                TextEntry::make('branch.name')->label('Branch')->placeholder('—'),
                TextEntry::make('method.name')->label('Method')->placeholder('—'),
                TextEntry::make('status.name')
                    ->label('Status')
                    ->badge()
                    ->color(fn (Transaction $record): string => StatusLookup::colorOf(Status::CTX_TRANSACTION, $record->status?->code ?? '')),
                TextEntry::make('amount')->money(fn () => app(CurrentContext::class)->currencyCode()),
                TextEntry::make('usable_amount')->label('Owed')->money(fn () => app(CurrentContext::class)->currencyCode()),
                TextEntry::make('used_amount')->label('Used')->money(fn () => app(CurrentContext::class)->currencyCode()),
                TextEntry::make('usable_pending')->label('Pending')->money(fn () => app(CurrentContext::class)->currencyCode()),
                TextEntry::make('taken_on')->label('Taken on')->dateTime()->placeholder('—'),
                TextEntry::make('reference')->placeholder('—'),
                TextEntry::make('creator.name')->label('Recorded by')->placeholder('—'),
                TextEntry::make('created_at')->dateTime(),
            ])->columns(3),

            Section::make('Cheque details')
                ->visible(fn (Transaction $record): bool => $record->method?->code === PaymentMethod::CHEQUE)
                ->schema([
                    TextEntry::make('cheque_number')->label('Cheque #')->placeholder('—'),
                    TextEntry::make('cheque_bank')->label('Bank')->placeholder('—'),
                    TextEntry::make('cheque_branch_code')->label('Branch')->placeholder('—'),
                    TextEntry::make('cheque_date')->label('Cheque date')->date()->placeholder('—'),
                ])->columns(4),

            Section::make('Settlements')
                ->schema([
                    RepeatableEntry::make('claims')
                        ->schema([
                            TextEntry::make('claim_number')->label('Claim #'),
                            TextEntry::make('invoice.invoice_number')->label('Invoice')->placeholder('—'),
                            TextEntry::make('invoice.net_total')->label('Invoiced total')->money(fn () => app(CurrentContext::class)->currencyCode())->placeholder('—'),
                            TextEntry::make('amount')->label('Net paid')->money(fn () => app(CurrentContext::class)->currencyCode()),
                            TextEntry::make('created_at')->label('Date')->dateTime(),
                        ])
                        ->columns(5)
                        ->columnSpanFull(),
                ]),

            Section::make('Note')
                ->visible(fn (Transaction $record): bool => filled($record->note))
                ->schema([
                    TextEntry::make('note')->columnSpanFull(),
                ]),
        ]);
    }

    protected function getHeaderActions(): array
    {
        return collect(TransactionResource::lifecycleActions())
            ->reject(fn ($action) => in_array($action->getName(), ['view', 'edit'], true))
            ->prepend(
                EditAction::make()
                    ->visible(fn (Transaction $record): bool => TransactionResource::canEdit($record) && ! $record->trashed()),
            )
            ->all();
    }
}
