<?php

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

class DecemberCrunch extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('prefixes', function (Blueprint $table) {
            $table->tinyInteger('setup_required')->nullable();
        });

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

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

            $table->tinyInteger('is_large')->nullable();
            $table->string('community');
        });

        Schema::table('prefix_goliath_flags', function (Blueprint $table) {
            $table->smallInteger('flag_heuristics_level')->nullable();

            $table->foreignId('region_id')->cascadeOnDelete()->nullable()->constrained('goliath_regions');
            $table->foreignId('subregion_id')->cascadeOnDelete()->nullable()->constrained('goliath_subregions');
            $table->foreignId('metro_id')->cascadeOnDelete()->nullable()->constrained('goliath_metro');
        });

        Schema::create('goliath_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->bigInteger('allowed_supernets')->nullable();
            $table->bigInteger('allowed_subnets')->nullable();
            $table->tinyInteger('allow_heuristics')->nullable();
            $table->tinyInteger('allow_synproxy')->nullable();
            $table->tinyInteger('suspended')->nullable();
        });

        Schema::table('geo_ip_organisation_attributes', function (Blueprint $table) {
            $table->tinyInteger('vip')->nullable();
            $table->tinyInteger('suspended')->nullable();
        }); 
    }

    public function down()
    {
        Schema::dropIfExists('goliath_organisation_attributes');
        Schema::dropIfExists('prefix_community');

        Schema::table('prefixes', function (Blueprint $table) {
            $table->dropColumn('setup_required');
        });

        Schema::table('prefix_goliath_flags', function (Blueprint $table) {
            $table->dropColumn('flag_heuristics_level');

            $table->dropColumn('region_id');
            $table->dropColumn('subregion_id');
            $table->dropColumn('metro_id');
        });

        Schema::table('geo_ip_organisation_attributes', function (Blueprint $table) {
            $table->dropColumn('vip');
            $table->dropColumn('suspended');
        });
    }
}
