<?php

namespace App\Filament\Exports;

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

/**
 * Columns mirror StockBatchResource::table() exactly.
 */
class StockBatchExporter extends Exporter
{
    protected static ?string $model = StockBatch::class;

    public static function getColumns(): array
    {
        return [
            ExportColumn::make('internal_batch_number'),
            ExportColumn::make('product.name'),
            ExportColumn::make('branch.name'),
            ExportColumn::make('available_quantity'),
            ExportColumn::make('product.baseUnit.code')->label('Unit'),
            ExportColumn::make('sale_price')->label('Sale price'),
            ExportColumn::make('batch_cost')->label('Cost'),
            ExportColumn::make('grn.grn_number')->label('GRN'),
            ExportColumn::make('expiry_date'),
            ExportColumn::make('status.name'),
        ];
    }

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