<?php

namespace App\Filament\Exports;

use App\Models\Sales\Invoice;
use Filament\Actions\Exports\ExportColumn;
use Filament\Actions\Exports\Exporter;
use Filament\Actions\Exports\Models\Export;
use Illuminate\Support\Number;

/**
 * Columns mirror InvoiceResource::table() exactly.
 */
class InvoiceExporter extends Exporter
{
    protected static ?string $model = Invoice::class;

    public static function getColumns(): array
    {
        return [
            ExportColumn::make('invoice_number')->label('Invoice #'),
            ExportColumn::make('customer.business_name')->label('Customer'),
            ExportColumn::make('branch.name')->label('Branch'),
            ExportColumn::make('channel'),
            ExportColumn::make('orderStatus.name')->label('Order status'),
            ExportColumn::make('paymentStatus.name')->label('Payment status'),
            ExportColumn::make('net_total')->label('Net total'),
            ExportColumn::make('paid_amount')->label('Paid amount'),
            ExportColumn::make('balance'),
            ExportColumn::make('creator.name')->label('Created by'),
            ExportColumn::make('created_at')->label('Created at'),
        ];
    }

    public static function getCompletedNotificationBody(Export $export): string
    {
        $body = 'Your invoice export has completed and '.Number::format($export->successful_rows).' '.str('row')->plural($export->successful_rows).' exported.';

        if ($failedRowsCount = $export->getFailedRowsCount()) {
            $body .= ' '.Number::format($failedRowsCount).' '.str('row')->plural($failedRowsCount).' failed to export.';
        }

        return $body;
    }
}
