<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        $sql = <<<'SQL'
            CREATE TABLE before_test_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,

                serial_number				TEXT DEFAULT NULL,
                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',

                tested_by					TEXT DEFAULT NULL,
                updated_by					TEXT DEFAULT 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,

                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('before_test_readings');
    }
};
