<?php

namespace App\Filament\Exports;

use App\Models\Inventory\ReturnGrn;
use Filament\Actions\Exports\ExportColumn;
use Filament\Actions\Exports\Exporter;
use Filament\Actions\Exports\Models\Export;
use Illuminate\Support\Number;

/**
 * Columns mirror ReturnGrnResource::table() exactly.
 */
class ReturnGrnExporter extends Exporter
{
    protected static ?string $model = ReturnGrn::class;

    public static function getColumns(): array
    {
        return [
            ExportColumn::make('return_grn_number')->label('Return GRN #'),
            ExportColumn::make('branch.name')->label('Branch'),
            ExportColumn::make('supplier.name')->label('Supplier'),
            ExportColumn::make('reference'),
            ExportColumn::make('items_count')->label('Items')->counts('items'),
            ExportColumn::make('total_cost')->label('Total'),
            ExportColumn::make('returnGrnStatus.name')->label('Status'),
            ExportColumn::make('created_at'),
        ];
    }

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