<?php

namespace App\Filament\Exports;

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

class EbReadingExporter extends Exporter
{
    protected static ?string $model = EbReading::class;

    public static $rowNumber = 0;

    public static function getColumns(): array
    {
        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('lcd_segment_check')
                ->label('LCD SEGMENT CHECK'),
            ExportColumn::make('meter_serial_no')
                ->label('METER SERIAL NO'),
            ExportColumn::make('eb_date_time')
                ->label('EB DATE TIME'),
            ExportColumn::make('ph_seq_of_volt')
                ->label('PH SEQ OF VOLT'),
            ExportColumn::make('ph_assoc_conn_check')
                ->label('PH ASSOC CONN CHECK'),
            ExportColumn::make('instantaneous_ph_volt')
                ->label('INSTANTANEOUS PH VOLT'),
            ExportColumn::make('instantaneous_curr')
                ->label('INSTANTANEOUS CURR'),
            ExportColumn::make('instantaneous_freq')
                ->label('INSTANTANEOUS FREQ'),
            ExportColumn::make('instantaneous_kw_with_sign')
                ->label('INSTANTANEOUS KW WITH SIGN'),
            ExportColumn::make('instantaneous_kva')
                ->label('INSTANTANEOUS KVA'),
            ExportColumn::make('instantaneous_kv_ar')
                ->label('INSTANTANEOUS KV AR'),
            ExportColumn::make('instantaneous_pf_with_sign')
                ->label('INSTANTANEOUS PF WITH SIGN'),
            ExportColumn::make('rd_with_elapsed_time_kva')
                ->label('RD WITH ELAPSED TIME KVA'),
            ExportColumn::make('cum_active_import_energy')
                ->label('CUM ACTIVE IMPORT ENERGY'),
            ExportColumn::make('tod1_active_energy_6_9')
                ->label('TOD1 ACTIVE ENERGY 6-9'),
            ExportColumn::make('tod2_active_energy_18_21')
                ->label('TOD2 ACTIVE ENERGY 18-21'),
            ExportColumn::make('tod3_active_energy_21_22')
                ->label('TOD3 ACTIVE ENERGY 21-22'),
            ExportColumn::make('tod4_active_energy_5_6_9_18')
                ->label('TOD4 ACTIVE ENERGY 5-6-9-18'),
            ExportColumn::make('tod5_active_energy_22_5')
                ->label('TOD5 ACTIVE ENERGY 22-5'),
            ExportColumn::make('cum_reac_lag_energy')
                ->label('CUM REAC LAG ENERGY'),
            ExportColumn::make('cum_reac_lead_energy')
                ->label('CUM REAC LEAD ENERGY'),
            ExportColumn::make('cum_appar_energy')
                ->label('CUM APPAR ENERGY'),
            ExportColumn::make('tod1_appar_energy_6_9')
                ->label('TOD1 APPAR ENERGY 6-9'),
            ExportColumn::make('tod2_appar_energy_18_21')
                ->label('TOD2 APPAR ENERGY 18-21'),
            ExportColumn::make('tod3_appar_energy_21_22')
                ->label('TOD3 APPAR ENERGY 21-22'),
            ExportColumn::make('tod4_appar_energy_5_6_9_18')
                ->label('TOD4 APPAR ENERGY 5-6-9-18'),
            ExportColumn::make('tod5_appar_energy_22_5')
                ->label('TOD5 APPAR ENERGY 22-5'),
            ExportColumn::make('avg_pow_factor')
                ->label('AVG POW FACTOR'),
            ExportColumn::make('avg_freq_15min_last_ip')
                ->label('AVG FREQ 15MIN LAST IP'),
            ExportColumn::make('net_kv_arh_high')
                ->label('NET KV ARH HIGH'),
            ExportColumn::make('net_kv_arh_low')
                ->label('NET KV ARH LOW'),
            ExportColumn::make('cum_md_kva')
                ->label('CUM MD KVA'),
            ExportColumn::make('present_md_kva')
                ->label('PRESENT MD KVA'),
            ExportColumn::make('present_md_kva_date_time')
                ->label('PRESENT MD KVA DATE TIME'),
            ExportColumn::make('tod1_md_kva_6_9')
                ->label('TOD1 MD KVA 6-9'),
            ExportColumn::make('tod2_md_kva_18_21')
                ->label('TOD2 MD KVA 18-21'),
            ExportColumn::make('tod3_md_kva_21_22')
                ->label('TOD3 MD KVA 21-22'),
            ExportColumn::make('tod4_md_kva_5_6_9_18')
                ->label('TOD4 MD KVA 5-6-9-18'),
            ExportColumn::make('tod5_md_kva_22_5')
                ->label('TOD5 MD KVA 22-5'),
            ExportColumn::make('total_pow_off_hours')
                ->label('TOTAL POW OFF HOURS'),
            ExportColumn::make('programming_count')
                ->label('PROGRAMMING COUNT'),
            ExportColumn::make('last_occ_res_event_type')
                ->label('LAST OCC RES EVENT TYPE'),
            ExportColumn::make('last_occ_res_event_date_time')
                ->label('LAST OCC RES EVENT DATE TIME'),
            ExportColumn::make('tamper_count')
                ->label('TAMPER COUNT'),
            ExportColumn::make('reset_count')
                ->label('RESET COUNT'),
            ExportColumn::make('last_md_reset_date_time')
                ->label('LAST MD RESET DATE TIME'),
            ExportColumn::make('electrician_sign')
                ->label('ELECTRICIAN SIGN'),
            ExportColumn::make('created_at')
                ->label('CREATED AT'),
            ExportColumn::make('updated_at')
                ->label('UPDATED AT'),
            ExportColumn::make('deleted_at')
                ->enabledByDefault(false),
            ExportColumn::make('updated_by')
                ->label('UPDATED BY'),
        ];
    }

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