<?php

namespace App\Filament\Resources\ReturnGrnResource\Pages;

use App\Filament\Resources\ReturnGrnResource;
use App\Models\Inventory\ReturnGrn;
use App\Models\Inventory\ReturnGrnItem;
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;

/**
 * The confirm/delete transitions here are the same Action objects the table
 * row uses (ReturnGrnResource::transitionActions()), minus the redundant
 * "view" link, so a return GRN can be confirmed or deleted from its own
 * detail page instead of only from the list.
 */
class ViewReturnGrn extends ViewRecord
{
    protected static string $resource = ReturnGrnResource::class;

    public function infolist(Schema $schema): Schema
    {
        return $schema->components([
            Section::make()->schema([
                TextEntry::make('return_grn_number')->label('Return GRN #'),
                TextEntry::make('branch.name'),
                TextEntry::make('supplier.name')->placeholder('—'),
                TextEntry::make('returnGrnStatus.name')
                    ->label('Status')
                    ->badge()
                    ->color(fn (ReturnGrn $record): string => $record->returnGrnStatus?->color ?? 'gray'),
                TextEntry::make('reference')->placeholder('—'),
                TextEntry::make('reason')->placeholder('—')->columnSpanFull(),
                TextEntry::make('total_cost')
                    ->label('Total')
                    ->money(fn () => app(CurrentContext::class)->currencyCode()),
                TextEntry::make('confirmedBy.name')->label('Confirmed by')->placeholder('—'),
                TextEntry::make('note')->columnSpanFull(),
            ])->columns(3),
            Section::make('Items')->schema([
                RepeatableEntry::make('items')
                    ->schema([
                        TextEntry::make('product.name')->label('Product'),
                        TextEntry::make('quantity')
                            ->formatStateUsing(fn ($state, ReturnGrnItem $record): string => Quantity::format($state, $record->unit)),
                        TextEntry::make('unit_cost')->label('Cost at return')->money(fn () => app(CurrentContext::class)->currencyCode())->placeholder('—'),
                        TextEntry::make('sourceGrnItem.grn.grn_number')->label('From GRN')->placeholder('—'),
                        TextEntry::make('serial_numbers')
                            ->label('Serials')
                            ->formatStateUsing(fn ($state): ?string => is_array($state) && $state !== [] ? implode(', ', $state) : null)
                            ->placeholder('—'),
                    ])
                    ->columns(5)
                    ->columnSpanFull(),
            ]),
        ]);
    }

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