<?php

namespace App\Filament\Exports;

use App\Models\InvoiceValidation;
use Filament\Actions\Exports\ExportColumn;
use Filament\Actions\Exports\Exporter;
use Filament\Actions\Exports\Models\Export;

class InvoiceValidationExporter extends Exporter
{
    protected static ?string $model = InvoiceValidation::class;

    public static function getColumns(): array
    {
        static $rowNumber = 0;

        return [
            // ExportColumn::make('id')
            //     ->label('ID'),
            ExportColumn::make('no')
                ->label('NO')
                ->state(function ($record) use (&$rowNumber) {
                    // Increment and return the row number
                    return ++$rowNumber;
                }),
            ExportColumn::make('plant.code')
                ->label('PLANT CODE'),
            ExportColumn::make('invoice_number')
                ->label('INVOICE NUMBER'),
            ExportColumn::make('serial_number')
                ->label('SERIAL NUMBER'),
            ExportColumn::make('stickerMaster.item.code')
                ->label('ITEM CODE'),
            ExportColumn::make('stickerMaster.item.description')
                ->label('ITEM DESCRIPTION'),
            ExportColumn::make('stickerMaster.item.uom')
                ->label('UNIT OF MEASURE'),
            ExportColumn::make('motor_scanned_status')
                ->label('MOTOR SCANNED STATUS'),
            ExportColumn::make('pump_scanned_status')
                ->label('PUMP SCANNED STATUS'),
            ExportColumn::make('scanned_status_set')
                ->label('PUMPSET SCANNED STATUS'),
            ExportColumn::make('capacitor_scanned_status')
                ->label('CAPACITOR SCANNED STATUS'),
            ExportColumn::make('scanned_status')
                ->label('SCANNED STATUS'),
            ExportColumn::make('panel_box_code')
                ->label('PANEL BOX CODE'),
            ExportColumn::make('panel_box_supplier')
                ->label('PANEL BOX SUPPLIER'),
            ExportColumn::make('panel_box_serial_number')
                ->label('PANEL BOX SERIAL NUMBER'),
            ExportColumn::make('load_rate')
                ->label('LOAD RATE'),
            ExportColumn::make('upload_status')
                ->label('UPLOAD STATUS'),
            ExportColumn::make('operator_id')
                ->label('OPERATOR ID'),
            ExportColumn::make('created_at')
                ->label('CREATED AT'),
            // ->dateTimeFormat('d-m-Y H:i:s'),
            ExportColumn::make('created_by')
                ->label('CREATED BY'),
            ExportColumn::make('updated_at')
                ->label('UPDATED AT'),
            // ->dateTimeFormat('d-m-Y H:i:s'),
            ExportColumn::make('updated_by')
                ->label('UPDATED BY'),
            ExportColumn::make('deleted_at')
                ->enabledByDefault(false)
                ->label('DELETED AT'),
            // ->dateTimeFormat('d-m-Y H:i:s'),
        ];
    }

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