<?php

namespace App\Filament\Exports;

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

/**
 * Columns mirror CashMovementResource::table() exactly.
 */
class CashMovementExporter extends Exporter
{
    protected static ?string $model = CashMovement::class;

    public static function getColumns(): array
    {
        return [
            ExportColumn::make('created_at'),
            ExportColumn::make('type'),
            ExportColumn::make('source'),
            ExportColumn::make('amount'),
            ExportColumn::make('reason.name')->label('Reason'),
            ExportColumn::make('note'),
            ExportColumn::make('session.till.name')->label('Till'),
            ExportColumn::make('session.branch.name')->label('Branch'),
            ExportColumn::make('creator.name')->label('By'),
        ];
    }

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