<?php

namespace App\Filament\Exports;

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

/**
 * Columns mirror LoyaltyBalanceResource::table() exactly.
 */
class CustomerLoyaltyBalanceExporter extends Exporter
{
    protected static ?string $model = CustomerLoyaltyBalance::class;

    public static function getColumns(): array
    {
        return [
            ExportColumn::make('customer.business_name')->label('Customer'),
            ExportColumn::make('customer.phone')->label('Phone'),
            ExportColumn::make('points_balance')->label('Points'),
            ExportColumn::make('lifetime_points')->label('Lifetime'),
            ExportColumn::make('tier.name')->label('Tier'),
        ];
    }

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