<?php

namespace App\Filament\Imports;

use App\Models\TestingPanelReading;
use Filament\Actions\Imports\ImportColumn;
use Filament\Actions\Imports\Importer;
use Filament\Actions\Imports\Models\Import;

class TestingPanelReadingImporter extends Importer
{
    protected static ?string $model = TestingPanelReading::class;

    public static function getColumns(): array
    {
        return [
            ImportColumn::make('machine')
                ->requiredMapping()
                ->relationship()
                ->rules(['required']),
            ImportColumn::make('motorTestingMaster.item.code')
                ->requiredMapping()
                ->relationship()
                ->rules(['required']),
            ImportColumn::make('output')
                ->requiredMapping()
                ->rules(['required']),
            ImportColumn::make('serial_number')
                ->requiredMapping()
                ->rules(['required']),
            ImportColumn::make('winded_serial_number')
                ->requiredMapping()
                ->rules(['required']),
            ImportColumn::make('before_fr_volt'),
            ImportColumn::make('before_fr_cur'),
            ImportColumn::make('before_fr_pow'),
            ImportColumn::make('before_fr_res_ry'),
            ImportColumn::make('before_fr_res_yb'),
            ImportColumn::make('before_fr_res_br'),
            ImportColumn::make('before_fr_ir'),
            ImportColumn::make('before_fr_ir_r'),
            ImportColumn::make('before_fr_ir_y'),
            ImportColumn::make('before_fr_ir_b'),
            ImportColumn::make('before_fr_freq'),
            ImportColumn::make('before_fr_speed'),
            ImportColumn::make('after_fr_vol'),
            ImportColumn::make('after_fr_cur'),
            ImportColumn::make('after_fr_pow'),
            ImportColumn::make('after_fr_ir_hot'),
            ImportColumn::make('after_fr_ir_hot_r'),
            ImportColumn::make('after_fr_ir_hot_y'),
            ImportColumn::make('after_fr_ir_hot_b'),
            ImportColumn::make('after_fr_ir_cool'),
            ImportColumn::make('after_fr_ir_cool_r'),
            ImportColumn::make('after_fr_ir_cool_y'),
            ImportColumn::make('after_fr_ir_cool_b'),
            ImportColumn::make('after_fr_freq'),
            ImportColumn::make('after_fr_speed'),
            ImportColumn::make('after_fr_leak_cur'),
            ImportColumn::make('locked_rt_volt'),
            ImportColumn::make('locked_rt_cur'),
            ImportColumn::make('locked_rt_pow'),
            ImportColumn::make('no_load_pickup_volt'),
            ImportColumn::make('room_temperature'),
            ImportColumn::make('hv_test'),
            ImportColumn::make('batch_number'),
            ImportColumn::make('batch_count')
                ->requiredMapping()
                ->rules(['required']),
            ImportColumn::make('result'),
            ImportColumn::make('remark'),
            ImportColumn::make('rework_count')
                ->requiredMapping()
                ->rules(['required']),
            ImportColumn::make('update_count')
                ->requiredMapping()
                ->rules(['required']),
            ImportColumn::make('output_flag')
                ->requiredMapping()
                ->rules(['required']),
            ImportColumn::make('line')
                ->requiredMapping()
                ->relationship(resolveUsing: 'name')
                ->exampleHeader('Line Name')
                ->example(['4 inch pump line'])
                ->label('Line Name')
                ->rules(['required']),
            ImportColumn::make('plant')
                ->requiredMapping()
                ->relationship(resolveUsing: 'code')
                ->exampleHeader('Plant Code')
                ->example(['1000'])
                ->label('Plant Code')
                ->rules(['required']),
            ImportColumn::make('tested_by'),
            ImportColumn::make('updated_by'),
            ImportColumn::make('scanned_at')
                ->requiredMapping()
                ->rules(['required']),
        ];
    }

    public function resolveRecord(): ?TestingPanelReading
    {
        // return TestingPanelReading::firstOrNew([
        //     // Update existing records, matching them by `$this->data['column_name']`
        //     'email' => $this->data['email'],
        // ]);

        return new TestingPanelReading;
    }

    public static function getCompletedNotificationBody(Import $import): string
    {
        $body = 'Your testing panel reading import has completed and '.number_format($import->successful_rows).' '.str('row')->plural($import->successful_rows).' imported.';

        if ($failedRowsCount = $import->getFailedRowsCount()) {
            $body .= ' '.number_format($failedRowsCount).' '.str('row')->plural($failedRowsCount).' failed to import.';
        }

        return $body;
    }
}
