<?php

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

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

        Schema::dropIfExists('chain_element_outcomes');
        Schema::dropIfExists('chain_elements');
        Schema::dropIfExists('chain_element_matches');
        Schema::dropIfExists('chain_element_actions');

        Schema::dropColumns('prefix_goliath_firewall', ['chain_id']);

        Schema::dropColumns('goliath_asn_attributes', ['default_chain']);

        Schema::table('goliath_asn_attributes', function (Blueprint $table) {
            $table->foreignId('default_chain')->cascadeOnDelete()->nullable()->constrained('goliath_rulesets');
        });

        Schema::dropIfExists('chains');
        Schema::dropIfExists('dynamic_latencies');
        Schema::dropIfExists('latency_locations');
        Schema::dropIfExists('mercury_tunnels');
        Schema::dropIfExists('mercury_bgp_snapshots');
        Schema::dropIfExists('mercury_appliances;');

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

            // Foreign identifiers
            $table->foreignId('prefix_id')->cascadeOnDelete()->constrained('prefixes');

            // General characteristics - PK, timestamps, soft-deletable
            $table->tinyInteger('loose')->nullable();
            $table->json('subregion_info')->nullable(); // We're never querying by this on an automated or regular basis - But we are needing it. Faster to have this than a pivot. 
        });

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

            // Foreign identifiers
            $table->foreignId('prefix_id')->cascadeOnDelete()->constrained('prefixes');

            // General characteristics - PK, timestamps, soft-deletable
            $table->text('bpf');
        });
    }

    public function down()
    {
        Schema::dropIfExists('prefix_goliath_loose');
        Schema::dropIfExists('prefix_goliath_emergency_bpf');
    }
}
