<?php

namespace App\Filament\Exports;

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

/**
 * Columns mirror VoucherResource::table() exactly.
 */
class VoucherExporter extends Exporter
{
    protected static ?string $model = Voucher::class;

    public static function getColumns(): array
    {
        return [
            ExportColumn::make('code'),
            ExportColumn::make('name'),
            ExportColumn::make('type.name')->label('Type'),
            ExportColumn::make('discountType.name')->label('Discount'),
            ExportColumn::make('discount_value'),
            ExportColumn::make('remaining_balance'),
            ExportColumn::make('used_count')->label('Used'),
            ExportColumn::make('usage_limit')->label('Limit'),
            ExportColumn::make('status.name')->label('Status'),
            ExportColumn::make('customer.business_name')->label('Customer'),
            ExportColumn::make('is_stackable'),
            ExportColumn::make('valid_until'),
            ExportColumn::make('created_at'),
        ];
    }

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