<?php

namespace App\Filament\Exports;

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

/**
 * Columns mirror LoyaltyProgramResource::table() exactly.
 */
class LoyaltyProgramExporter extends Exporter
{
    protected static ?string $model = LoyaltyProgram::class;

    public static function getColumns(): array
    {
        return [
            ExportColumn::make('name'),
            ExportColumn::make('base_earn_rate')->label('Earn rate'),
            ExportColumn::make('redemption_rate')->label('Redemption rate'),
            ExportColumn::make('minimum_purchase_to_earn'),
            ExportColumn::make('minimum_points_to_redeem'),
            ExportColumn::make('points_validity_days')->label('Validity (days)'),
            ExportColumn::make('tiers_count')->label('Tiers')->counts('tiers'),
            ExportColumn::make('is_active'),
        ];
    }

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