<?php

namespace App\Filament\Exports;

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

/**
 * Columns mirror BinCardResource::table(), plus SKU and Direction — carried
 * over from the page-level CSV action this exporter replaces, so switching
 * to the standard export actions loses no data.
 */
class StockMovementExporter extends Exporter
{
    protected static ?string $model = StockMovement::class;

    public static function getColumns(): array
    {
        return [
            ExportColumn::make('created_at')->label('Date'),
            ExportColumn::make('product.name')->label('Product'),
            ExportColumn::make('product.sku')->label('SKU'),
            ExportColumn::make('branch.name')->label('Branch'),
            ExportColumn::make('movementType.name')->label('Type'),
            ExportColumn::make('movementType.direction')->label('Direction'),
            ExportColumn::make('quantity_change')->label('Qty change'),
            ExportColumn::make('quantity_after')->label('Qty after'),
            ExportColumn::make('unit.code')->label('Unit'),
            ExportColumn::make('reference_number')->label('Reference'),
            ExportColumn::make('creator.name')->label('Recorded by'),
        ];
    }

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