<?php

namespace App\Filament\Exports;

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

class SerialValidationExporter extends Exporter
{
    protected static ?string $model = SerialValidation::class;

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

        return [
            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('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_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('batch_number')
                ->label('BATCH NUMBER'),
            ExportColumn::make('quantity')
                ->label('QUANTITY'),
            ExportColumn::make('operator_id')
                ->label('OPERATOR ID'),
            ExportColumn::make('created_at')
                ->label('CREATED AT'),
            // ->dateTimeFormat('d-m-Y H:i:s'),
            ExportColumn::make('updated_at')
                ->label('UPDATED AT'),
            // ->dateTimeFormat('d-m-Y H:i:s'),
            ExportColumn::make('deleted_at')
                ->enabledByDefault(false)
                ->label('DELETED AT'),
        ];
    }

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