<?php

namespace App\Filament\Exports;

use App\Models\Returns\ReturnDoc;
use Filament\Actions\Exports\ExportColumn;
use Filament\Actions\Exports\Exporter;
use Filament\Actions\Exports\Models\Export;
use Illuminate\Support\Number;

/**
 * Columns mirror ReturnResource::table() exactly.
 */
class ReturnDocExporter extends Exporter
{
    protected static ?string $model = ReturnDoc::class;

    public static function getColumns(): array
    {
        return [
            ExportColumn::make('return_number')->label('Return #'),
            ExportColumn::make('customer.business_name')->label('Customer'),
            ExportColumn::make('branch.name')->label('Branch'),
            ExportColumn::make('status.name')->label('Status'),
            ExportColumn::make('refundMethod.name')->label('Refund'),
            ExportColumn::make('total'),
            ExportColumn::make('refund_amount')->label('Refunded'),
            ExportColumn::make('total_items')->label('Items'),
            ExportColumn::make('stocked_items')->label('Restocked'),
            ExportColumn::make('creator.name')->label('Processed by'),
            ExportColumn::make('deleted_at')->label('Deleted'),
            ExportColumn::make('created_at'),
        ];
    }

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