<?php

namespace App\Filament\Pages\Reports\Concerns;

use App\Filament\Support\StatusLookup;
use App\Models\Lookups\Status;
use App\Models\Sales\Invoice;
use App\Services\CurrentContext;
use App\Services\VatSettings;
use Filament\Actions\Action;
use Filament\Infolists\Components\TextEntry;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Components\View;

/**
 * The "View" record action used by every report that lists invoices (Sales
 * Report and Invoice Report — same record, same modal, one definition).
 *
 * Reuses the app's standard infolist pattern (see AuditLogsRelationManager):
 * a columned Section of TextEntries for the header/totals, and a shared Blade
 * partial for the line items.
 */
trait ProvidesInvoiceViewModal
{
    public function invoiceViewAction(): Action
    {
        return Action::make('view')
            ->label('View')
            ->icon('heroicon-m-eye')
            ->color('gray')
            ->schema([
                Section::make()->columns(4)->schema([
                    TextEntry::make('invoice_number')->label('Invoice #'),
                    TextEntry::make('created_at')->label('Date')->dateTime(),
                    TextEntry::make('branch.name')->label('Branch')->placeholder('—'),
                    TextEntry::make('customer.business_name')->label('Customer')->placeholder('Walk-in'),
                    TextEntry::make('paymentStatus.name')->label('Payment')->badge()
                        ->color(fn (Invoice $record): ?string => StatusLookup::colorOf(Status::CTX_INVOICE_PAYMENT, (string) ($record->paymentStatus?->code ?? ''))),
                    TextEntry::make('orderStatus.name')->label('Order')->badge()
                        ->color(fn (Invoice $record): ?string => StatusLookup::colorOf(Status::CTX_INVOICE_ORDER, (string) ($record->orderStatus?->code ?? ''))),
                    TextEntry::make('channel')->label('Channel')->placeholder('—'),
                    TextEntry::make('subtotal')->label('Subtotal')
                        ->money(fn (): string => app(CurrentContext::class)->currencyCode()),
                    TextEntry::make('discount')->label('Discount')
                        ->money(fn (): string => app(CurrentContext::class)->currencyCode()),
                    TextEntry::make('vat_total')->label('VAT')
                        ->money(fn (): string => app(CurrentContext::class)->currencyCode())
                        ->visible(fn (Invoice $record): bool => app(VatSettings::class)->enabled()),
                    TextEntry::make('net_total')->label('Total')
                        ->money(fn (): string => app(CurrentContext::class)->currencyCode()),
                    TextEntry::make('paid_amount')->label('Paid')
                        ->money(fn (): string => app(CurrentContext::class)->currencyCode()),
                    TextEntry::make('balance')->label('Balance')
                        ->money(fn (): string => app(CurrentContext::class)->currencyCode()),
                ]),

                Section::make('Items')->schema([
                    View::make('filament.pages.reports.partials.invoice-items')
                        ->viewData(fn (Invoice $record): array => ['items' => $record->items()->get()]),
                ]),
            ]);
    }
}
