<?php

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

class Addresses extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
		Schema::create('addresses', function (Blueprint $table) {
			$table->id();
			$table->timestamps();
			$table->boolean('active');
			$table->boolean('validated')->default(FALSE);

			$table->bigInteger('zone_id');
			$table->foreign('zone_id')->references('id')->on('zones');

			$table->integer('region_id');
			$table->integer('host_id');
			$table->integer('node_id');
			$table->integer('point_id');
			$table->integer('status')->nullable();		// @note Used to record Down/Private/Pending, etc

			$table->integer('role');

			$table->bigInteger('system_id');
			$table->foreign('system_id')->references('id')->on('systems');

			$table->bigInteger('hub_id')->nullable();
			$table->foreign('hub_id')->references('id')->on('addresses');

			$table->softDeletes();
		});

		DB::statement("CREATE UNIQUE INDEX addresses_active ON addresses (zone_id,host_id,node_id,point_id) WHERE active = true");
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
		Schema::dropIfExists('address_echomail');
		Schema::dropIfExists('addresses');
    }
}