<?php

namespace App\Filament\Exports;

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

class MotorTestingMasterExporter extends Exporter
{
    protected static ?string $model = MotorTestingMaster::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'),
            ExportColumn::make('item.category')
                ->label('CATEGORY'),
            ExportColumn::make('item.code')
                ->label('ITEM CODE'),
            ExportColumn::make('subassembly_code')
                ->label('SUBASSEMBLY CODE'),
            ExportColumn::make('item.description')
                ->label('DESCRIPTION'),
            ExportColumn::make('isi_model')
                ->label('ISI MODEL'),
            ExportColumn::make('phase')
                ->label('PHASE'),
            ExportColumn::make('hp')
                ->label('HP'),
            ExportColumn::make('kw')
                ->label('KW'),
            ExportColumn::make('volt')
                ->label('VOLT'),
            ExportColumn::make('current')
                ->label('CURRENT'),
            ExportColumn::make('rpm')
                ->label('RPM'),
            ExportColumn::make('torque')
                ->label('TORQUE'),
            ExportColumn::make('frequency')
                ->label('FREQUENCY'),
            ExportColumn::make('connection')
                ->label('CONNECTION'),
            ExportColumn::make('ins_res_limit')
                ->label('INSULATION RESISTANCE LIMIT'),
            ExportColumn::make('ins_res_type')
                ->label('INSULATION RESISTANCE TYPE'),
            ExportColumn::make('routine_test_time')
                ->label('ROUTINE TEST TIME'),
            ExportColumn::make('res_ry_ll')
                ->label('RESISTANCE RY LL'),
            ExportColumn::make('res_ry_ul')
                ->label('RESISTANCE RY UL'),
            ExportColumn::make('res_yb_ll')
                ->label('RESISTANCE YB LL'),
            ExportColumn::make('res_yb_ul')
                ->label('RESISTANCE YB UL'),
            ExportColumn::make('res_br_ll')
                ->label('RESISTANCE BR LL'),
            ExportColumn::make('res_br_ul')
                ->label('RESISTANCE BR UL'),
            ExportColumn::make('lock_volt_limit')
                ->label('LOCK VOLT LIMIT'),
            ExportColumn::make('leak_cur_limit')
                ->label('LEAK CURRENT LIMIT'),
            ExportColumn::make('lock_cur_ll')
                ->label('LOCK CURRENT LL'),
            ExportColumn::make('lock_cur_ul')
                ->label('LOCK CURRENT UL'),
            ExportColumn::make('noload_cur_ll')
                ->label('NO LOAD CURRENT LL'),
            ExportColumn::make('noload_cur_ul')
                ->label('NO LOAD CURRENT UL'),
            ExportColumn::make('noload_pow_ll')
                ->label('NO LOAD POWER LL'),
            ExportColumn::make('noload_pow_ul')
                ->label('NO LOAD POWER UL'),
            ExportColumn::make('noload_spd_ll')
                ->label('NO LOAD SPEED LL'),
            ExportColumn::make('noload_spd_ul')
                ->label('NO LOAD SPEED UL'),
            ExportColumn::make('created_at')
                ->label('CREATED AT'),
            ExportColumn::make('created_by')
                ->label('CREATED BY'),
            ExportColumn::make('updated_at')
                ->label('UPDATED AT'),
            ExportColumn::make('updated_by')
                ->label('UPDATED BY'),
            ExportColumn::make('deleted_at')
                ->enabledByDefault(false)
                ->label('DELETED AT'),
        ];
    }

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