<?php

namespace App\Filament\Exports;

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

/**
 * Columns mirror StockTransferResource::table() exactly.
 */
class StockTransferExporter extends Exporter
{
    protected static ?string $model = StockTransfer::class;

    public static function getColumns(): array
    {
        return [
            ExportColumn::make('fromBranch.name')->label('From'),
            ExportColumn::make('toBranch.name')->label('To'),
            ExportColumn::make('product.name'),
            ExportColumn::make('quantity'),
            ExportColumn::make('unit.code')->label('Unit'),
            ExportColumn::make('reason.name'),
            ExportColumn::make('created_at'),
        ];
    }

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