<?php

namespace App\Filament\Resources\CreditNoteResource\Pages;

use App\Filament\Resources\CreditNoteResource;
use App\Filament\Support\StatusLookup;
use App\Models\Lookups\Status;
use App\Models\Returns\CreditNote;
use App\Models\Returns\ReturnItem;
use App\Services\CurrentContext;
use App\Support\Quantity;
use Filament\Infolists\Components\RepeatableEntry;
use Filament\Infolists\Components\TextEntry;
use Filament\Resources\Pages\ViewRecord;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;

/**
 * Ported from CreditNoteController::show(). The issue/claim/void transitions
 * are the same Action objects the table row uses (CreditNoteResource::
 * transitionActions()), minus the redundant "view" link.
 */
class ViewCreditNote extends ViewRecord
{
    protected static string $resource = CreditNoteResource::class;

    public function infolist(Schema $schema): Schema
    {
        return $schema->components([
            Section::make()->schema([
                TextEntry::make('cn_number')->label('CN #'),
                TextEntry::make('customer.business_name')->label('Customer')->placeholder('—'),
                TextEntry::make('status.name')
                    ->label('Status')
                    ->badge()
                    ->color(fn (CreditNote $record): string => StatusLookup::colorOf(Status::CTX_CREDIT_NOTE, $record->status?->code ?? '')),
                TextEntry::make('amount')->money(fn () => app(CurrentContext::class)->currencyCode()),
                TextEntry::make('applied_amount')->label('Applied')->money(fn () => app(CurrentContext::class)->currencyCode())->placeholder('—'),
                TextEntry::make('return.return_number')->label('From return')->placeholder('—'),
                TextEntry::make('claimedInvoice.invoice_number')->label('Claimed on invoice')->placeholder('—'),
                TextEntry::make('issued_at')->dateTime()->placeholder('—'),
                TextEntry::make('claimed_at')->dateTime()->placeholder('—'),
                TextEntry::make('voided_at')->dateTime()->placeholder('—'),
                TextEntry::make('void_reason')->placeholder('—'),
                TextEntry::make('creator.name')->label('Issued by')->placeholder('—'),
                TextEntry::make('note')->columnSpanFull()->placeholder('—'),
            ])->columns(3),

            // The originating return's lines, matching show.tsx's
            // 'return.items.product' eager load.
            Section::make('Originating return items')
                ->visible(fn (CreditNote $record): bool => $record->return_id !== null)
                ->schema([
                    RepeatableEntry::make('return.items')
                        ->schema([
                            TextEntry::make('product.name')->label('Product'),
                            TextEntry::make('quantity')
                                ->formatStateUsing(fn ($state, ReturnItem $record): string => Quantity::format($state, $record->unit)),
                            TextEntry::make('unit_price')->money(fn () => app(CurrentContext::class)->currencyCode()),
                            TextEntry::make('grand_total')->money(fn () => app(CurrentContext::class)->currencyCode()),
                        ])
                        ->columns(4)
                        ->columnSpanFull(),
                ]),
        ]);
    }

    protected function getHeaderActions(): array
    {
        return collect(CreditNoteResource::transitionActions())
            ->reject(fn ($action) => $action->getName() === 'view')
            ->all();
    }
}
