<?php

namespace App\Filament\Exports;

use App\Models\Returns\CreditNote;
use Filament\Actions\Exports\ExportColumn;
use Filament\Actions\Exports\Exporter;
use Filament\Actions\Exports\Models\Export;
use Illuminate\Support\Number;

/**
 * Columns mirror CreditNoteResource::table() exactly.
 */
class CreditNoteExporter extends Exporter
{
    protected static ?string $model = CreditNote::class;

    public static function getColumns(): array
    {
        return [
            ExportColumn::make('cn_number')->label('CN #'),
            ExportColumn::make('customer.business_name')->label('Customer'),
            ExportColumn::make('status.name')->label('Status'),
            ExportColumn::make('amount'),
            ExportColumn::make('applied_amount')->label('Applied'),
            ExportColumn::make('return.return_number')->label('From return'),
            ExportColumn::make('claimedInvoice.invoice_number')->label('Claimed on'),
            ExportColumn::make('creator.name')->label('Issued by'),
            ExportColumn::make('created_at'),
        ];
    }

    public static function getCompletedNotificationBody(Export $export): string
    {
        $body = 'Your credit note 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;
    }
}
