<?php

namespace App\Filament\Exports;

use App\Models\Customers\Customer;
use Filament\Actions\Exports\ExportColumn;
use Filament\Actions\Exports\Exporter;
use Filament\Actions\Exports\Models\Export;
use Illuminate\Support\Number;

/**
 * Columns mirror CustomerResource::table() exactly.
 */
class CustomerExporter extends Exporter
{
    protected static ?string $model = Customer::class;

    public static function getColumns(): array
    {
        return [
            ExportColumn::make('business_name')->label('Customer'),
            ExportColumn::make('phone'),
            ExportColumn::make('nic')->label('NIC'),
            ExportColumn::make('email'),
            ExportColumn::make('city.name')->label('City'),
            ExportColumn::make('contacts_count')->label('Contacts'),
            ExportColumn::make('total_invoiced')->label('Total invoiced'),
            ExportColumn::make('total_paid')->label('Total paid'),
            ExportColumn::make('creditBalance.balance')->label('Credit'),
            ExportColumn::make('is_active')->label('Active'),
        ];
    }

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