<?php

namespace App\Filament\Imports;

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

class EbReadingImporter extends Importer
{
    protected static ?string $model = EbReading::class;

    public static function getColumns(): array
    {
        return [
            ImportColumn::make('plant')
                ->requiredMapping()
                ->exampleHeader('Plant Code')
                ->example('1000')
                ->label('Plant Code')
                ->relationship(resolveUsing: 'code')
                ->rules(['required']),
            ImportColumn::make('lcd_segment_check')
                ->label('LCD Segment Check')
                ->example('Ok')
                ->exampleHeader('LCD Segment Check'),
            ImportColumn::make('meter_serial_no')
                ->label('Meter Serial No')
                ->example('572880')
                ->exampleHeader('Meter Serial No'),
            ImportColumn::make('eb_date_time')
                ->label('EB Date Time')
                ->example('2025-08-05 08:32:58')
                ->exampleHeader('EB Date Time')
                ->requiredMapping()
                ->rules(['required', 'datetime']),
            ImportColumn::make('ph_seq_of_volt')
                ->label('Phase Sequence of Volt')
                ->example('RYB')
                ->exampleHeader('Phase Sequence of Volt'),
            ImportColumn::make('ph_assoc_conn_check')
                ->example('GOOD')
                ->exampleHeader('Phase Associated Connection Check')
                ->label('Phase Associated Connection Check'),
            ImportColumn::make('instantaneous_ph_volt')
                ->exampleHeader('Instantaneous Phase Volt')
                ->example('64.35,64.91,64.93')
                ->label('Instantaneous Phase Volt'),
            ImportColumn::make('instantaneous_curr')
                ->exampleHeader('Instantaneous Current')
                ->example('1.02,1.00,0.94')
                ->label('Instantaneous Current'),
            ImportColumn::make('instantaneous_freq')
                ->exampleHeader('Instantaneous Frequency')
                ->example('50.07')
                ->label('Instantaneous Frequency'),
            ImportColumn::make('instantaneous_kw_with_sign')
                ->exampleHeader('Instantaneous KW with Sign')
                ->example('0.176')
                ->label('Instantaneous KW with Sign'),
            ImportColumn::make('instantaneous_kva')
                ->exampleHeader('Instantaneous KVA')
                ->example('0.176')
                ->label('Instantaneous KVA'),
            ImportColumn::make('instantaneous_kv_ar')
                ->exampleHeader('Instantaneous KV AR')
                ->example('0.02')
                ->label('Instantaneous KV AR'),
            ImportColumn::make('instantaneous_pf_with_sign')
                ->exampleHeader('Instantaneous PF with Sign')
                ->example('0.99')
                ->label('Instantaneous PF with Sign'),
            ImportColumn::make('rd_with_elapsed_time_kva')
                ->exampleHeader('RD with Elapsed Time KVA')
                ->example('0.047')
                ->label('RD with Elapsed Time KVA'),
            ImportColumn::make('cum_active_import_energy')
                ->exampleHeader('Cumulative Active Import Energy')
                ->example('13246.46')
                ->label('Cumulative Active Import Energy'),
            ImportColumn::make('tod1_active_energy_6_9')
                ->exampleHeader('TOD1 Active Energy 6-9')
                ->example('1367.75')
                ->label('TOD1 Active Energy 6-9'),
            ImportColumn::make('tod2_active_energy_18_21')
                ->exampleHeader('TOD2 Active Energy 18-21')
                ->example('1759.08')
                ->label('TOD2 Active Energy 18-21'),
            ImportColumn::make('tod3_active_energy_21_22')
                ->exampleHeader('TOD3 Active Energy 21-22')
                ->example('457.67')
                ->label('TOD3 Active Energy 21-22'),
            ImportColumn::make('tod4_active_energy_5_6_9_18')
                ->exampleHeader('TOD4 Active Energy 5-6-9-18')
                ->example('6253.85')
                ->label('TOD4 Active Energy 5-6-9-18'),
            ImportColumn::make('tod5_active_energy_22_5')
                ->exampleHeader('TOD5 Active Energy 22-5')
                ->example('3408.11')
                ->label('TOD5 Active Energy 22-5'),
            ImportColumn::make('cum_reac_lag_energy')
                ->exampleHeader('Cumulative Reactive Lag Energy')
                ->example('685.11')
                ->label('Cumulative Reactive Lag Energy'),
            ImportColumn::make('cum_reac_lead_energy')
                ->exampleHeader('Cumulative Reactive Lead Energy')
                ->example('426.1')
                ->label('Cumulative Reactive Lead Energy'),
            ImportColumn::make('cum_appar_energy')
                ->exampleHeader('Cumulative Apparent Energy')
                ->example('13306.57')
                ->label('Cumulative Apparent Energy'),
            ImportColumn::make('tod1_appar_energy_6_9')
                ->exampleHeader('TOD1 Apparent Energy 6-9')
                ->example('1374.63')
                ->label('TOD1 Apparent Energy 6-9'),
            ImportColumn::make('tod2_appar_energy_18_21')
                ->exampleHeader('TOD2 Apparent Energy 18-21')
                ->example('1766.61')
                ->label('TOD2 Apparent Energy 18-21'),
            ImportColumn::make('tod3_appar_energy_21_22')
                ->exampleHeader('TOD3 Apparent Energy 21-22')
                ->example('459.47')
                ->label('TOD3 Apparent Energy 21-22'),
            ImportColumn::make('tod4_appar_energy_5_6_9_18')
                ->exampleHeader('TOD4 Apparent Energy 5-6-9-18')
                ->example('6283.28')
                ->label('TOD4 Apparent Energy 5-6-9-18'),
            ImportColumn::make('tod5_appar_energy_22_5')
                ->exampleHeader('TOD5 Apparent Energy 22-5')
                ->example('3422.56')
                ->label('TOD5 Apparent Energy 22-5'),
            ImportColumn::make('avg_pow_factor')
                ->exampleHeader('Average Power Factor')
                ->example('0.98')
                ->label('Average Power Factor'),
            ImportColumn::make('avg_freq_15min_last_ip')
                ->exampleHeader('Average Frequency 15min Last IP')
                ->example('50')
                ->label('Average Frequency 15min Last IP'),
            ImportColumn::make('net_kv_arh_high')
                ->exampleHeader('Net KV ARH High')
                ->example('2.99')
                ->label('Net KV ARH High'),
            ImportColumn::make('net_kv_arh_low')
                ->exampleHeader('Net KV ARH Low')
                ->example('143.14')
                ->label('Net KV ARH Low'),
            ImportColumn::make('cum_md_kva')
                ->exampleHeader('Cumulative MD KVA')
                ->example('43.816')
                ->label('Cumulative MD KVA'),
            ImportColumn::make('present_md_kva')
                ->exampleHeader('Present MD KVA')
                ->example('0.379')
                ->label('Present MD KVA'),
            ImportColumn::make('present_md_kva_date_time')
                ->label('Present MD KVA Date Time')
                ->exampleHeader('Present MD KVA Date Time')
                ->example('2025-08-05 08:32:58')
                ->requiredMapping()
                ->rules(['required', 'datetime']),
            ImportColumn::make('tod1_md_kva_6_9')
                ->exampleHeader('TOD1 MD KVA 6-9')
                ->example('0.282')
                ->label('TOD1 MD KVA 6-9'),
            ImportColumn::make('tod2_md_kva_18_21')
                ->exampleHeader('TOD2 MD KVA 18-21')
                ->example('0.268')
                ->label('TOD2 MD KVA 18-21'),
            ImportColumn::make('tod3_md_kva_21_22')
                ->exampleHeader('TOD3 MD KVA 21-22')
                ->example('0')
                ->label('TOD3 MD KVA 21-22'),
            ImportColumn::make('tod4_md_kva_5_6_9_18')
                ->exampleHeader('TOD4 MD KVA 5-6-9-18')
                ->example('0.379')
                ->label('TOD4 MD KVA 5-6-9-18'),
            ImportColumn::make('tod5_md_kva_22_5')
                ->exampleHeader('TOD5 MD KVA 22-5')
                ->example('0.379')
                ->label('TOD5 MD KVA 22-5'),
            ImportColumn::make('total_pow_off_hours')
                ->exampleHeader('Total Power Off Hours')
                ->example('6480.56')
                ->label('Total Power Off Hours'),
            ImportColumn::make('programming_count')
                ->exampleHeader('Programming Count')
                ->example('3')
                ->label('Programming Count'),
            ImportColumn::make('last_occ_res_event_type')
                ->exampleHeader('Last Occurrence/Reset Event Type')
                ->example('-')
                ->label('Last Occurrence/Reset Event Type'),
            ImportColumn::make('last_occ_res_event_date_time')
                ->label('Last Occurrence/Reset Event Date Time')
                ->example('2025-08-05 08:32:58')
                ->exampleHeader('Last Occurrence/Reset Event Date Time')
                ->requiredMapping()
                ->rules(['required', 'datetime']),
            ImportColumn::make('tamper_count')
                ->exampleHeader('Tamper Count')
                ->example('24')
                ->label('Tamper Count'),
            ImportColumn::make('reset_count')
                ->exampleHeader('Reset Count')
                ->example('108')
                ->label('Reset Count'),
            ImportColumn::make('last_md_reset_date_time')
                ->exampleHeader('Last MD Reset Date Time')
                ->example('2025-08-05 08:32:58')
                ->label('Last MD Reset Date Time')
                ->requiredMapping()
                ->rules(['required', 'datetime']),
            ImportColumn::make('electrician_sign')
                ->exampleHeader('Electrician Sign')
                ->example('Admin')
                ->label('Electrician Sign'),
        ];
    }

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

        return new EbReading;
    }

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