<?php

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

class Mercury extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        // Drop the old cruft
        Schema::dropIfExists('mercury_tunnels');
        Schema::dropIfExists('mercury_bgp_snapshots');
        Schema::dropIfExists('mercury_appliances');
        Schema::dropIfExists('mercury_organisation_attributes');
        
        Schema::create('mercury_appliances', function (Blueprint $table) {
            // General characteristics - PK, timestamps, soft-deletable
            $table->id();
            $table->timestamps();
            $table->softDeletes();

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

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

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

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

            $table->timestamp('reported_at')->nullable();

        });

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

            // Foreign identifiers
            $table->foreignId('appliance_id')->cascadeOnDelete()->nullable()->constrained('mercury_appliances');

            $table->text('checksum');
            $table->longText('compressed');
        });

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

            // Foreign identifiers
            $table->foreignId('organisation_id')->cascadeOnDelete()->constrained('organisations');

            // Characteristics
            $table->tinyInteger('vip')->nullable();
            $table->tinyInteger('allow_high')->nullable();
            $table->tinyInteger('allow_announcements')->nullable();
            $table->tinyInteger('allow_downstreaming')->nullable();
            $table->integer('aggregate_bandwidth')->nullable();
            $table->tinyInteger('suspended')->nullable();
        });

        Schema::create('mercury_tunnels', 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()->constrained('asns');
            $table->foreignId('appliance_id')->cascadeOnDelete()->constrained('mercury_appliances');

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

            $table->string('billing_state');
            $table->string('bgp_state');
            $table->string('tunnel_state');

            $table->ipAddress('local_address');
            $table->ipAddress('remote_address');
            $table->integer('remote_port')->nullable();

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

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

            $table->string('type'); // Typically VXLAN or GRE, freeform to accomodate for anything new

            // Admin specific characteristics
            $table->tinyInteger('allow_announcements')->nullable();
            $table->tinyInteger('allow_downstreaming')->nullable();
            $table->tinyInteger('no_bgp')->nullable();
        });

    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('mercury_tunnels');
        Schema::dropIfExists('mercury_bgp_snapshots');
        Schema::dropIfExists('mercury_appliances');
        Schema::dropIfExists('mercury_organisation_attributes');
        
    }
}
