<?php

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

class CreateNodes extends Migration
{
	/**
	 * Run the migrations.
	 *
	 * @return void
	 */
	public function up()
	{
		Schema::create('protocols', function (Blueprint $table) {
			$table->increments('id');
			$table->timestamps();
			$table->string('name');
			$table->integer('port')->nullable();
			$table->boolean('active');
		});

		Schema::create('software', function (Blueprint $table) {
			$table->increments('id');
			$table->timestamps();
			$table->string('name');
			$table->boolean('active');
		});

		Schema::create('nodes', function (Blueprint $table) {
			$table->increments('id');
			$table->timestamps();

			$table->integer('zone_id')->nullable()->index();
			$table->foreign('zone_id')->references('id')->on('zones');

			$table->integer('region_id')->nullable();
			$table->integer('host_id')->nullable()->index();
			$table->integer('hub_id')->nullable()->index();
			$table->integer('node_id')->nullable()->index();
			$table->integer('point_id')->default(0);

			$table->boolean('active');
			$table->string('status')->nullable();
			$table->string('system');
			$table->string('sysop');
			$table->string('location');
			$table->string('email')->nullable();
			$table->string('address')->nullable();
			$table->integer('port')->nullable();
			$table->integer('baud')->nullable();
			$table->string('phone')->nullable();
			$table->string('notes')->nullable();

			$table->boolean('is_zc')->default(FALSE);
			$table->boolean('is_rc')->default(FALSE);
			$table->boolean('is_hub')->default(FALSE);
			$table->boolean('is_host')->default(FALSE);

			$table->string('sespass')->nullable();
			$table->string('pktpass',8)->nullable();
			$table->string('ticpass')->nullable();
			$table->string('fixpass')->nullable();

			$table->string('zt',10)->nullable();

			$table->unique(['zone_id','host_id','node_id','point_id']);
			$table->unique(['zone_id','zt']);


			$table->integer('protocol_id')->nullable();
			$table->foreign('protocol_id')->references('id')->on('protocols');
			$table->integer('software_id')->nullable();
			$table->foreign('software_id')->references('id')->on('software');

//			$table->unique(['zone_id','host_id','id']);
//			$table->index(['zone_id','host_id']);
//			$table->index(['zone_id','id']);
//			$table->foreign(['zone_id','host_id'])->references(['zone_id','id'])->on('nodes');
		});

	}

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