<?php

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

class Base extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        /* Organisation Construct */
        Schema::create('organisations', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
            $table->softDeletes();

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

            $table->string('name');
            $table->json('billing')->nullable();
        });

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

            $table->timestamps();
            $table->softDeletes();

            $table->foreignId('parent_id')->cascadeOnDelete()->constrained('organisations');
            $table->foreignId('child_id')->cascadeOnDelete()->constrained('organisations');
        });

        /* Group Construct */
        Schema::create('groups', function (Blueprint $table) {
            $table->id();

            $table->timestamps();
            $table->softDeletes();

            $table->string('name');
         });

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

            $table->string('permission');
            $table->string('conditional')->nullable();
            $table->tinyInteger('status');

            $table->foreignId('group_id')->cascadeOnDelete()->constrained('groups');
        });

        /* User Construct */
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
            $table->softDeletes();
            $table->timestamp('suspended_at')->nullable();
            $table->timestamp('reset_at')->nullable();
            $table->timestamp('signup_at')->nullable();

            $table->string('name');
            $table->string('email')->unique();
            $table->string('phone')->nullable();
            $table->string('password');

            // Security questions
            $table->string('question_one')->nullable();
            $table->string('question_two')->nullable();
            $table->string('question_three')->nullable();
            $table->string('answer_one')->nullable();
            $table->string('answer_two')->nullable();
            $table->string('answer_three')->nullable();

            // Billing data
            $table->json('billing')->nullable();

            $table->string('signup_token')->unique()->nullable();
            $table->string('reset_token')->unique()->nullable();
            $table->rememberToken();

            $table->foreignId('organisation_id')->cascadeOnDelete()->nullable()->constrained('organisations');
            $table->foreignId('group_id')->nullOnDelete()->nullable()->constrained('groups');

        });

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

            $table->timestamps();
            $table->softDeletes();

            $table->foreignId('parent_id')->nullOnDelete()->constrained('users');
            $table->foreignId('child_id')->nullOnDelete()->constrained('users');
        });

        /* Product Access */
        Schema::create('product_rights', function(Blueprint $table) {
            $table->id();

            $table->timestamps();
            $table->softDeletes();

            $table->string('name');

            $table->foreignId('user_id')->cascadeOnDelete()->constrained('users')->nullable();
            $table->foreignId('organisation_id')->cascadeOnDelete()->constrained('organisations')->nullable();
        });

        /* Security Event Construct */
        Schema::create('security_events', function (Blueprint $table) {
            $table->id();
            $table->timestamps();

            $table->string('ip');
            $table->string('user_agent');

            $table->string('route')->nullable();
            $table->string('description')->nullable();
            $table->json('payload')->nullable();

            $table->foreignId('user_id')->nullOnDelete()->nullable()->constrained('users');
         });

        /* Failed Login Construct */
        Schema::create('failed_logins', function (Blueprint $table) {
            $table->id();
            $table->timestamps();

            $table->string('ip');
            $table->string('user_agent');
            
            $table->foreignId('user_id')->nullOnDelete()->constrained('users');
         });

        /* Logging Construct */
        Schema::create('automation_logs', function(Blueprint $table) {
            $table->id();

            $table->timestamp('timestamp');
            $table->string('source');
            $table->string('type', 10);
            $table->string('description');
        });

        /* ASN Construct */
        Schema::create('asns', function(Blueprint $table) {
            $table->id();
            $table->timestamps();
            $table->softDeletes();

            $table->integer('asn');
            $table->string('email_token')->unique();
            $table->tinyInteger('approved');

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

        /* AS-SET Construct */
        Schema::create('as_sets', function(Blueprint $table) {
            $table->id();
            $table->timestamps();
            $table->softDeletes();

            $table->integer('as_set');
            $table->string('email_token')->unique();
            $table->tinyInteger('approved');

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

        /* Module Management Constructs */
        Schema::create('modules', function(Blueprint $table) {
            $table->id();

            $table->timestamps();

            $table->string('name')->unique();
        });

        /* Alert Constructs */
        Schema::create('alerts', function(Blueprint $table) {
            $table->id();
            $table->timestamps();
            $table->softDeletes();

            $table->string('audience'); // Will typically be 'solitary-user' or 'organisation'
            $table->timestamp('live_at')->nullable();
            $table->timestamp('down_at')->nullable();

            $table->text('short_message');
            $table->text('long_message');

            $table->string('style');
        });

        Schema::create('alert_displays', function(Blueprint $table) {
            $table->foreignId('user_id')->cascadeOnDelete()->constrained('users');
            $table->foreignId('alert_id')->cascadeOnDelete()->constrained('alerts');
        });

        /* Laravel Constructs */
        Schema::create('failed_jobs', function (Blueprint $table) {
            $table->id();
            
            $table->string('uuid')->unique();
            $table->text('connection');
            $table->text('queue');
            $table->longText('payload');
            $table->longText('exception');
            $table->timestamp('failed_at')->useCurrent();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('event_logs');
        Schema::dropIfExists('security_events');
        Schema::dropIfExists('failed_logins');
        Schema::dropIfExists('failed_jobs');
        Schema::dropIfExists('password_resets');
        Schema::dropIfExists('organisation_attribute');
        Schema::dropIfExists('organisations');
        Schema::dropIfExists('group_permissions');
        Schema::dropIfExists('groups');
        Schema::dropIfExists('users');
    }
}
