<?php

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

class MercuryNew extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {

        Schema::create('mercury_nodes', function (Blueprint $table) {
            // General characteristics - PK, timestamps, soft-deletable
            $table->id();
            $table->timestamps();
            $table->softDeletes();

            // Characteristics
            $table->string('name')->nullable();
            $table->string('description')->nullable();
            $table->string('tag')->nullable();

            $table->ipAddress('server_v4');
            $table->ipAddress('server_v6');

            $table->smallInteger('limit')->nullable();

            $table->tinyInteger('hidden')->nullable();
            $table->tinyInteger('locked')->nullable();

        });

        Schema::create('mercury_gre', function(Blueprint $table) {
            // General characteristics - PK, timestamps, soft-deletable
            $table->id();
            $table->timestamps();
            $table->softDeletes();

            // Foreign identifiers
            $table->foreignId('organisation_id')->cascadeOnDelete()->constrained('organisations');
            $table->foreignId('asn_id')->cascadeOnDelete()->nullable()->constrained('asns');
            $table->foreignId('as_set_id')->cascadeOnDelete()->nullable()->constrained('as_sets');
            $table->foreignId('node_id')->cascadeOnDelete()->constrained('mercury_nodes');

            // Characteristics
            $table->string('name');

            $table->ipAddress('client_address');

            $table->ipAddress('gsl_device_v4');
            $table->ipAddress('gsl_device_v6');

            $table->ipAddress('client_device_v4');
            $table->ipAddress('client_device_v6');

            $table->enum('filter', ['asn', 'as-set']);

            $table->tinyInteger('mtu');
            $table->tinyInteger('tunnel_number');

            $table->tinyInteger('allow_full_tables')->nullable();
        });

        Schema::create('mercury_gre_routes', function(Blueprint $table) {
            // General characteristics - PK, timestamps, soft-deletable
            $table->id();
            $table->timestamps();
            $table->softDeletes();

            // Foreign identifiers
            $table->foreignId('gre_id')->cascadeOnDelete()->constrained('mercury_gre');

            // Characteristics
            $table->json('routes')->nullable();
            
        });

    }

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