<?php

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

class Endpoints extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('api_scopes', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
            $table->softDeletes();
            
            $table->string('name');
        });

        Schema::create('api_tokens', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
            $table->softDeletes();
            
            // Name for EU purposes
            $table->string('name')->nullable();

            // Token identifier and hashed token 
            $table->string('identifier');
            $table->string('token');

            // Whether it is active or not
            $table->tinyInteger('active');

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

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

            $table->string('ip');

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

            $table->foreignId('token_id')->nullOnDelete()->nullable()->constrained('api_tokens');
         });

        Schema::create('api_token_bearer', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
            $table->softDeletes();
            
            // Name for EU purposes
            $table->string('bearer');
            $table->timestamp('expiration_at')->default(DB::raw('current_timestamp'));

            // Metadata
            $table->foreignId('token_id')->cascadeOnDelete()->constrained('api_tokens');
        });

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

            $table->foreignId('api_token_id')->cascadeOnDelete()->constrained('api_tokens');
            $table->foreignId('api_scopes_id')->cascadeOnDelete()->constrained('api_scopes');
        });

        Schema::create('api_token_whitelist', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
            $table->softDeletes();
        
            // IP Address to whitelist
            $table->ipAddress('address');

            // Metadata
            $table->foreignId('token_id')->cascadeOnDelete()->constrained('api_tokens');
            $table->foreignId('organisation_id')->cascadeOnDelete()->constrained('organisations');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('api_tokens');
        Schema::dropIfExists('api_token_permissions');
        Schema::dropIfExists('whitelisted_clients');
    }
}
