<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        $sql = <<<'SQL'
            CREATE TABLE testing_panel_readings (
                id                          BIGINT GENERATED always AS IDENTITY PRIMARY KEY,

                plant_id                    BIGINT NOT NULL,
                line_id                     BIGINT NOT NULL,
                motor_testing_master_id     BIGINT NOT NULL,
                machine_id                  BIGINT NOT NULL,

                output                		TEXT NOT NULL,
                serial_number				TEXT NOT NULL,
                before_fr_volt				TEXT DEFAULT '0',
                before_fr_cur				TEXT DEFAULT '0',
                before_fr_pow				TEXT DEFAULT '0',
                before_fr_res_ry			TEXT DEFAULT '0',
                before_fr_res_yb			TEXT DEFAULT '0',
                before_fr_res_br			TEXT DEFAULT '0',
                before_fr_ir				TEXT DEFAULT '0',
                before_fr_ir_r				TEXT DEFAULT '0',
                before_fr_ir_y				TEXT DEFAULT '0',
                before_fr_ir_b				TEXT DEFAULT '0',
                before_fr_freq				TEXT DEFAULT '0',
                before_fr_speed				TEXT DEFAULT '0',
                after_fr_vol				TEXT DEFAULT '0',
                after_fr_cur				TEXT DEFAULT '0',
                after_fr_pow				TEXT DEFAULT '0',
                after_fr_ir_hot				TEXT DEFAULT '0',
                after_fr_ir_hot_r			TEXT DEFAULT '0',
                after_fr_ir_hot_y    		TEXT DEFAULT '0',
                after_fr_ir_hot_b			TEXT DEFAULT '0',
                after_fr_ir_cool			TEXT DEFAULT '0',
                after_fr_ir_cool_r			TEXT DEFAULT '0',
                after_fr_ir_cool_y			TEXT DEFAULT '0',
                after_fr_ir_cool_b			TEXT DEFAULT '0',
                after_fr_freq				TEXT DEFAULT '0',
                after_fr_speed				TEXT DEFAULT '0',
                after_fr_leak_cur			TEXT DEFAULT '0',
                locked_rt_volt				TEXT DEFAULT '0',
                locked_rt_cur				TEXT DEFAULT '0',
                locked_rt_pow				TEXT DEFAULT '0',
                no_load_pickup_volt			TEXT DEFAULT '0',
                room_temperature			TEXT NULL,
                hv_test						TEXT NULL,
                batch_number				TEXT NULL,
                batch_count					TEXT NOT NULL DEFAULT '0',
                result						TEXT NULL,
                remark						TEXT NULL,

                rework_count				TEXT NOT NULL DEFAULT '0',
                update_count                TEXT NOT NULL DEFAULT '0',
                output_flag                 TEXT NOT NULL DEFAULT '0',

                tested_by					TEXT NULL,
                updated_by					TEXT NULL,


                created_at                  TIMESTAMP NOT NULL DEFAULT NOW(),
                updated_at                  TIMESTAMP NOT NULL DEFAULT NOW(),
                scanned_at					TIMESTAMP NOT NULL DEFAULT NOW(),
                deleted_at                  TIMESTAMP,

                UNIQUE (plant_id, motor_testing_master_id, serial_number, machine_id, line_id, rework_count, update_count),

                FOREIGN KEY (plant_id)                  REFERENCES  plants (id),
                FOREIGN KEY (motor_testing_master_id)   REFERENCES  motor_testing_masters (id),
                FOREIGN KEY (machine_id)                REFERENCES  machines (id),
                FOREIGN KEY (line_id)                   REFERENCES  lines (id)
            );
        SQL;

        DB::statement($sql);
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('testing_panel_readings');
    }
};
