<?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 mfm_readings (
                id					            BIGINT GENERATED always AS IDENTITY PRIMARY KEY,
                plant_id                           BIGINT NOT NULL,
                mfm_meter_id                       BIGINT NOT NULL,

                apparent_energy_received		   TEXT DEFAULT NULL,
                reactive_energy_received		   TEXT DEFAULT NULL,
                active_energy_received             TEXT DEFAULT NULL,
                active_power_r                     TEXT DEFAULT NULL,
                active_power_y                     TEXT DEFAULT NULL,
                active_power_b                     TEXT DEFAULT NULL,
                active_power_total                 TEXT DEFAULT NULL,
                voltage_ry                         TEXT DEFAULT NULL,
                voltage_yb                         TEXT DEFAULT NULL,
                voltage_br                         TEXT DEFAULT NULL,
                current_r                          TEXT DEFAULT NULL,
                current_y                          TEXT DEFAULT NULL,
                current_b                          TEXT DEFAULT NULL,
                current_n                          TEXT DEFAULT NULL,
                voltage_r_n                        TEXT DEFAULT NULL,
                voltage_y_n                        TEXT DEFAULT NULL,
                voltage_b_n                        TEXT DEFAULT NULL,
                frequency                          TEXT DEFAULT NULL,
                power_factor_r                     TEXT DEFAULT NULL,
                power_factor_y                     TEXT DEFAULT NULL,
                power_factor_b                     TEXT DEFAULT NULL,
                power_factor_total                 TEXT DEFAULT NULL,

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

                UNIQUE (plant_id, created_at, mfm_meter_id),

                FOREIGN KEY (plant_id)             REFERENCES  plants (id),
                FOREIGN KEY (mfm_meter_id)         REFERENCES  mfm_meters (id)

            );
        SQL;

        DB::statement($sql);
    }

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