<?php

namespace App\Filament\Resources\ReturnResource\Pages;

use App\Filament\Resources\ReturnResource;
use App\Filament\Support\StatusLookup;
use App\Models\Lookups\Status;
use App\Models\Returns\ReturnDoc;
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 ReturnController::show() — read-only drill-down, same eager
 * loads, same fields the source's returns/show.tsx renders.
 */
class ViewReturn extends ViewRecord
{
    protected static string $resource = ReturnResource::class;

    public function infolist(Schema $schema): Schema
    {
        return $schema->components([
            Section::make()->schema([
                TextEntry::make('return_number')->label('Return #'),
                TextEntry::make('customer.business_name')->label('Customer')->placeholder('Walk-in'),
                TextEntry::make('branch.name')->label('Branch')->placeholder('—'),
                TextEntry::make('status.name')
                    ->label('Status')
                    ->badge()
                    ->color(fn (ReturnDoc $record): string => StatusLookup::colorOf(Status::CTX_RETURN, $record->status?->code ?? '')),
                TextEntry::make('refundMethod.name')->label('Refund method')->placeholder('—'),
                TextEntry::make('total')->money(fn () => app(CurrentContext::class)->currencyCode()),
                TextEntry::make('refund_amount')->label('Refunded')->money(fn () => app(CurrentContext::class)->currencyCode()),
                TextEntry::make('creator.name')->label('Processed by')->placeholder('—'),
                TextEntry::make('created_at')->dateTime(),
                TextEntry::make('note')->columnSpanFull()->placeholder('—'),
            ])->columns(3),

            Section::make('Items')->schema([
                RepeatableEntry::make('items')
                    ->schema([
                        TextEntry::make('product.name')->label('Product'),
                        TextEntry::make('invoice.invoice_number')->label('Invoice')->placeholder('—'),
                        TextEntry::make('reason.name')->label('Reason')->placeholder('—'),
                        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('discount_amount')->money(fn () => app(CurrentContext::class)->currencyCode()),
                        TextEntry::make('grand_total')->money(fn () => app(CurrentContext::class)->currencyCode()),
                        TextEntry::make('is_add_back_to_stock')
                            ->label('Restocked')
                            ->formatStateUsing(fn ($state): string => $state ? 'Yes' : 'No'),
                    ])
                    ->columns(4)
                    ->columnSpanFull(),
            ]),
        ]);
    }
}
