<?php

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

class DefaultCleanout extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        // Drop the old Corero cruft (Yay) - It is not needed, we also do not need to recreate it on a drop. It is fully removed.
        Schema::dropIfExists('mitigation_organisation_attributes');
        Schema::dropIfExists('mitigation_organisation_default_rulesets');
        Schema::dropIfExists('mitigation_organisation_default_sensitivities');
        Schema::dropIfExists('prefix_mitigation');
        Schema::dropIfExists('mitigation_rulesets');
        Schema::dropIfExists('mitigation_sensitivities');
        Schema::dropIfExists('firewall_rules');

        Schema::dropIfExists('prefix_asn_peers');
        Schema::dropIfExists('code_user_reset');
        Schema::dropIfExists('code_asn_claim');
        Schema::dropIfExists('code_as_set_claim');


        Schema::create('prefix_asn_peers', function (Blueprint $table) {
            $table->integer('peer');
            $table->integer('owner');
            
            $table->foreignId('prefix_id')->cascadeOnDelete()->constrained('prefixes');
        });

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

            $table->timestamp('expiry_at');

            $table->string('token');
           
            $table->foreignId('user_id')->cascadeOnDelete()->constrained('users');
        });

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

            $table->timestamp('expiry_at');

            $table->string('token');

            $table->foreignId('organisation_id')->cascadeOnDelete()->constrained('organisations');
            $table->foreignId('asn_id')->cascadeOnDelete()->constrained('asns');
        });

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

            $table->timestamp('expiry_at');

            $table->string('token');

            $table->foreignId('organisation_id')->cascadeOnDelete()->constrained('organisations');
            $table->foreignId('as_set')->cascadeOnDelete()->constrained('as_sets');
        });

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

        Schema::table('asns', function (Blueprint $table) {
            $table->dropColumn('email_token');
            $table->dropColumn('override_routelist');
        });

        Schema::table('as_sets', function (Blueprint $table) {
            $table->dropColumn('email_token');
            $table->dropColumn('as_set');
        });

        Schema::table('as_sets', function (Blueprint $table) {
            $table->string('as_set');
        });
    }

    public function down() {}
}
