<?php

namespace App\Filament\Exports;

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

/**
 * Columns mirror LoyaltyTransactionResource::table() exactly.
 */
class LoyaltyPointTransactionExporter extends Exporter
{
    protected static ?string $model = LoyaltyPointTransaction::class;

    public static function getColumns(): array
    {
        return [
            ExportColumn::make('created_at')->label('Date'),
            ExportColumn::make('customer.business_name')->label('Customer'),
            ExportColumn::make('type.name')->label('Type'),
            ExportColumn::make('points')->label('Points'),
            ExportColumn::make('balance_after')->label('Balance after'),
            ExportColumn::make('expires_at')->label('Expires'),
            ExportColumn::make('invoice.invoice_number')->label('Invoice'),
            ExportColumn::make('description')->label('Description'),
            ExportColumn::make('is_reversed')->label('Reversed'),
        ];
    }

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