<?php

namespace App\Exports;

use App\Models\TestingPanelReading;
use Illuminate\Contracts\Queue\ShouldQueue;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithChunkReading;
// use Maatwebsite\Excel\Concerns\ShouldQueue;
use Maatwebsite\Excel\Concerns\WithHeadings;

// class TestingPanelReadingExport implements FromCollection
// {
//     /**
//     * @return \Illuminate\Support\Collection
//     */
//     public function collection()
//     {
//         return TestingPanelReading::all();
//     }
// }

// class TestingPanelReadingExport implements FromQuery, WithChunkReading, ShouldQueue, WithHeadings
// {
//     protected array $ids;
//     protected int $counter = 0;

//     public function __construct(array $ids)
//     {
//         $this->ids = $ids;
//     }
//     public function query()
//     {
//         return TestingPanelReading::query()
//         ->whereIn('id', $this->ids)
//         ->select('id', 'plant_id', 'line_id', 'motor_testing_master_id', 'machine_id', 'output', 'serial_number', 'winded_serial_number', 'before_fr_volt', 'before_fr_cur', 'before_fr_pow', 'before_fr_res_ry', 'before_fr_res_yb', 'before_fr_res_br', 'before_fr_ir', 'before_fr_ir_r', 'before_fr_ir_y', 'before_fr_ir_b', 'before_fr_freq', 'before_fr_speed', 'after_fr_vol', 'after_fr_cur', 'after_fr_pow', 'after_fr_ir_hot','after_fr_ir_hot_r', 'after_fr_ir_hot_y', 'after_fr_ir_hot_b', 'after_fr_ir_cool', 'after_fr_ir_cool_r', 'after_fr_ir_cool_y', 'after_fr_ir_cool_b', 'after_fr_freq', 'after_fr_speed', 'after_fr_leak_cur', 'locked_rt_volt', 'locked_rt_cur', 'locked_rt_pow', 'no_load_pickup_volt', 'room_temperature', 'hv_test', 'batch_number', 'batch_count', 'result', 'remark', 'rework_count', 'update_count', 'output_flag', 'tested_by', 'updated_by', 'created_at', 'updated_at', 'scanned_at', 'created_at');
//     }

//     public function chunkSize(): int
//     {
//         return 1000; // process 1000 rows at a time
//     }

//     public function headings(): array
//     {
//         return ['NO', 'PLANT', 'LINE', 'MOTORTESTINGMASTER', 'MACHINE', 'OUTPUT', 'SERIAL NUMBER', 'WINDED SERIAL NUMBER', 'BEFORE FR VOLT', 'BEFORE FR CUR', 'BEFORE FR POW', 'BEFORE FR RES RY', 'BEFORE FR RES YB', 'BEFORE FR RES BR', 'BEFORE FR IR', 'BEFORE FR IR R', 'BEFORE FR IR Y', 'BEFORE FR IR B', 'BEFORE FR FREQ', 'BEFORE FR SPEED', 'AFTER FR VOL', 'AFTER FR CUR', 'AFTER FR POW', 'AFTER FR IR HOT', 'AFTER FR IR HOT R', 'AFTER FR IR HOT Y', 'AFTER FR IR HOT B', 'AFTER FR IR COOL', 'AFTER FR IR COOL R', 'AFTER FR IR COOL Y', 'AFTER FR IR COOL B', 'AFTER FR FREQ', 'AFTER FR SPEED', 'AFTER FR LEAK CUR', 'LOCKED RT VOLT', 'LOCKED RT CUR', 'LOCKED RT POW', 'NO LOAD PICKUP VOLT', 'ROOM TEMPERATURE', 'HV TEST', 'BATCH NUMBER', 'BATCH COUNT', 'RESULT', 'REMARK', 'REWORK COUNT', 'UPDATE COUNT', 'OUTPUT FLAG', 'TETSED BY', 'UPDATED BY', 'CREATED AT', 'UPDATED AT', 'SCANNED AT', 'CREATED AT'];
//     }

//     public function map($record): array
//     {
//         $this->counter++;
//         return [
//             $this->counter,       // No.
//             $record->plant_id,
//             $record->line_id,
//             $record->motor_testing_master_id,
//             $record->machine_id,
//             $record->output,
//             $record->serial_number,
//             $record->winded_serial_number,
//             $record->before_fr_volt,
//             $record->before_fr_cur,
//             $record->before_fr_pow,
//             $record->before_fr_res_ry,
//             $record->before_fr_res_yb,
//             $record->before_fr_res_br,
//             $record->before_fr_ir,
//             $record->before_fr_ir_r,
//             $record->before_fr_ir_y,
//             $record->before_fr_ir_b,
//             $record->before_fr_freq,
//             $record->before_fr_speed,
//             $record->after_fr_vol,
//             $record->after_fr_cur,
//             $record->after_fr_pow,
//             $record->after_fr_ir_hot,
//             $record->after_fr_ir_hot_r,
//             $record->after_fr_ir_hot_y,
//             $record->after_fr_ir_hot_b,
//             $record->after_fr_ir_cool,
//             $record->after_fr_ir_cool_r,
//             $record->after_fr_ir_cool_y,
//             $record->after_fr_ir_cool_b,
//             $record->after_fr_freq,
//             $record->after_fr_speed,
//             $record->after_fr_leak_cur,
//             $record->locked_rt_volt,
//             $record->locked_rt_cur,
//             $record->locked_rt_pow,
//             $record->no_load_pickup_volt,
//             $record->room_temperature,
//             $record->hv_test,
//             $record->batch_number,
//             $record->batch_count,
//             $record->result,
//             $record->remark,
//             $record->rework_count,
//             $record->update_count,
//             $record->output_flag,
//             $record->tested_by,
//             $record->updated_by,
//            // $record->created_at,
//             $record->updated_at,
//             $record->scanned_at,
//             $record->created_at,
//         ];
//     }
// }

class TestingPanelReadingExport implements FromCollection, WithHeadings
{
    protected $records;
    protected $isAllStarDelta;

    public function __construct($records, $isAllStarDelta)
    {
        $this->records = $records;
        $this->isAllStarDelta = $isAllStarDelta;
    }

    public function collection()
    {
        return collect($this->records)->map(function ($record) {
            if (!$this->isAllStarDelta) {
                return [
                    'Date' => date('Y-m-d', strtotime($record['created_at'] ?? '')),
                    'Output' => $record['output'] ?? '',
                    'Motor SNo' => $record['serial_number'] ?? '',
                    'Item Code' => $record->motorTestingMaster->item->code ?? '',
                    'Motor Type' => $record->motorTestingMaster->item->description ?? '',
                    'IR_Hot' => $record['after_fr_ir_hot'] ?? '',
                    'IR_Cool' => $record['after_fr_ir_cool'] ?? '',
                    'Leakage_Current' => $record['after_fr_leak_cur'] ?? '',
                ];
            } else {
                return [
                    'Date' => date('Y-m-d', strtotime($record['created_at'] ?? '')),
                    'Output' => $record['output'] ?? '',
                    'Motor SNo' => $record['serial_number'] ?? '',
                    'Item Code' => $record->motorTestingMaster->item->code ?? '',
                    'Motor Type' => $record->motorTestingMaster->item->description ?? '',
                    'IR_Hot_R' => $record['after_fr_ir_hot_r'] ?? '',
                    'IR_Hot_Y' => $record['after_fr_ir_hot_y'] ?? '',
                    'IR_Hot_B' => $record['after_fr_ir_hot_b'] ?? '',
                    'IR_Cool_R' => $record['after_fr_ir_cool_r'] ?? '',
                    'IR_Cool_Y' => $record['after_fr_ir_cool_y'] ?? '',
                    'IR_Cool_B' => $record['after_fr_ir_cool_b'] ?? '',
                    'Leakage_Current' => $record['after_fr_leak_cur'] ?? '',
                ];
            }
        });
    }

    public function headings(): array
    {
        return array_keys($this->collection()->first() ?? []);
    }
}
