<?php

namespace App\Filament\Exports;

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

class TestingPanelReadingExporter extends Exporter
{
    protected static ?string $model = TestingPanelReading::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('line.name')
                ->label('LINE NAME'),
            ExportColumn::make('machine.name')
                ->label('MACHINE NAME'),
            ExportColumn::make('motorTestingMaster.item.code')
                ->label('ITEM CODE'),
            ExportColumn::make('motorTestingMaster.item.description')
                ->label('MODEL DESCRIPTION'),
            ExportColumn::make('output')
                ->label('OUTPUT NAME'),
            ExportColumn::make('serial_number')
                ->label('SERIAL NUMBER'),
            ExportColumn::make('winded_serial_number')
                ->label('WINDED SERIAL NUMBER'),
            ExportColumn::make('motorTestingMaster.kw')
                ->label('KW'),
            ExportColumn::make('motorTestingMaster.hp')
                ->label('HP'),
            ExportColumn::make('motorTestingMaster.phase')
                ->label('PHASE'),
            ExportColumn::make('motorTestingMaster.connection')
                ->label('CONNECTION'),
            ExportColumn::make('motorTestingMaster.isi_model')
                ->label('ISI MODEL'),
            ExportColumn::make('before_fr_volt')
                ->label('BEFORE FR VOLT'),
            ExportColumn::make('before_fr_cur')
                ->label('BEFORE FR CURRENT'),
            ExportColumn::make('before_fr_pow')
                ->label('BEFORE FR POWER'),
            ExportColumn::make('before_fr_res_ry')
                ->label('BEFORE FR RESISTANCE RY'),
            ExportColumn::make('before_fr_res_yb')
                ->label('BEFORE FR RESISTANCE YB'),
            ExportColumn::make('before_fr_res_br')
                ->label('BEFORE FR RESISTANCE BR'),
            ExportColumn::make('before_fr_ir')
                ->label('BEFORE FR IR'),
            ExportColumn::make('before_fr_ir_r')
                ->label('BEFORE FR IR R'),
            ExportColumn::make('before_fr_ir_y')
                ->label('BEFORE FR IR Y'),
            ExportColumn::make('before_fr_ir_b')
                ->label('BEFORE FR IR B'),
            ExportColumn::make('before_fr_freq')
                ->label('BEFORE FR FREQUENCY'),
            ExportColumn::make('before_fr_speed')
                ->label('BEFORE FR SPEED'),
            ExportColumn::make('after_fr_vol')
                ->label('AFTER FR VOLT'),
            ExportColumn::make('after_fr_cur')
                ->label('AFTER FR CURRENT'),
            ExportColumn::make('after_fr_pow')
                ->label('AFTER FR POWER'),
            ExportColumn::make('after_fr_ir_hot')
                ->label('AFTER FR IR HOT'),
            ExportColumn::make('after_fr_ir_hot_r')
                ->label('AFTER FR IR HOT R'),
            ExportColumn::make('after_fr_ir_hot_y')
                ->label('AFTER FR IR HOT Y'),
            ExportColumn::make('after_fr_ir_hot_b')
                ->label('AFTER FR IR HOT B'),
            ExportColumn::make('after_fr_ir_cool')
                ->label('AFTER FR IR COOL'),
            ExportColumn::make('after_fr_ir_cool_r')
                ->label('AFTER FR IR COOL R'),
            ExportColumn::make('after_fr_ir_cool_y')
                ->label('AFTER FR IR COOL Y'),
            ExportColumn::make('after_fr_ir_cool_b')
                ->label('AFTER FR IR COOL B'),
            ExportColumn::make('after_fr_freq')
                ->label('AFTER FR FREQUENCY'),
            ExportColumn::make('after_fr_speed')
                ->label('AFTER FR SPEED'),
            ExportColumn::make('after_fr_leak_cur')
                ->label('AFTER FR LEAK CURRENT'),
            ExportColumn::make('locked_rt_volt')
                ->label('LOCKED RT VOLT'),
            ExportColumn::make('locked_rt_cur')
                ->label('LOCKED RT CURRENT'),
            ExportColumn::make('locked_rt_pow')
                ->label('LOCKED RT POWER'),
            ExportColumn::make('no_load_pickup_volt')
                ->label('NO LOAD PICKUP VOLT'),
            ExportColumn::make('room_temperature')
                ->label('ROOM TEMPERATURE'),
            ExportColumn::make('hv_test')
                ->label('HIGH VOLTAGE TEST'),
            ExportColumn::make('batch_number')
                ->label('BATCH NUMBER'),
            ExportColumn::make('batch_count')
                ->label('BATCH COUNT'),
            ExportColumn::make('result')
                ->label('RESULT'),
            ExportColumn::make('remark')
                ->label('REMARK'),
            ExportColumn::make('rework_count')
                ->label('REWORK COUNT'),
            ExportColumn::make('update_count')
                ->label('UPDATED COUNT'),
            ExportColumn::make('output_flag')
                ->label('OUTPUT FLAG'),
            ExportColumn::make('tested_by')
                ->label('TESTED BY'),
            ExportColumn::make('updated_by')
                ->label('UPDATED BY'),
            ExportColumn::make('created_at')
                ->label('CREATED AT'),
            ExportColumn::make('updated_at')
                ->label('UPDATED AT'),
            ExportColumn::make('scanned_at')
                ->label('SCANNED AT'),
            ExportColumn::make('deleted_at')
                ->enabledByDefault(false)
                ->label('DELETED AT'),
        ];
    }

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