<?php

namespace App\Filament\Exports;

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

/**
 * Columns mirror BarcodeLabelResource::table() exactly.
 */
class BarcodeLabelExporter extends Exporter
{
    protected static ?string $model = BarcodeLabel::class;

    public static function getColumns(): array
    {
        return [
            ExportColumn::make('code')->label('Label code'),
            ExportColumn::make('product.name')->label('Product'),
            ExportColumn::make('label_name')->label('Printed name'),
            ExportColumn::make('batch.internal_batch_number')->label('Lot'),
            ExportColumn::make('serial.serial_number')->label('Serial'),
            ExportColumn::make('quantity'),
            ExportColumn::make('unit.code')->label('Unit'),
            ExportColumn::make('base_quantity')->label('Base quantity'),
            ExportColumn::make('branch.name')->label('Branch'),
            ExportColumn::make('printed_count'),
            ExportColumn::make('last_printed_at'),
            ExportColumn::make('created_at'),
        ];
    }

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