<?php

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

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

        if(!Schema::hasTable('users')) {
            Schema::create('users', function (Blueprint $table) {
                $table->id();
                $table->string('character_owner_hash')->index();
                $table->string('character_name');
                $table->unsignedBigInteger('character_id')->unique();
                $table->text('avatar');
                $table->text('token');
                $table->text('refresh_token')->nullable();
                $table->integer('expiresIn')->nullable();
                $table->text('user_jwt')->nullable();
                $table->rememberToken();
                $table->timestamps();
            });
        }

        if(!Schema::hasTable('sessions')) {
            Schema::create('sessions', function (Blueprint $table) {
                $table->string('id')->primary();
                $table->foreignId('user_id')->nullable()->index();
                $table->string('ip_address', 45)->nullable();
                $table->text('user_agent')->nullable();
                $table->longText('payload');
                $table->integer('last_activity')->index();
            });
        }

        if(!Schema::hasTable('esi_token')) {
            Schema::create('esi_token', function (Blueprint $table) {
                $table->id();
                $table->string('character_id');
                $table->text('access_token');
                $table->text('refresh_token');
                $table->string('inserted_at');
                $table->integer('expires_in');
            });
        }

        if(!Schema::hasTable('esi_scopes')) {
            Schema::create('esi_scopes', function (Blueprint $table) {
                $table->id();
                $table->string('character_id');
                $table->string('scope');
            });
        }

        if(!Schema::hasTable('user_alts')) {
            Schema::create('user_alts', function(Blueprint $table) {
                $table->id();
                $table->string('name');
                $table->unsignedBigInteger('main_id');
                $table->unsignedBigInteger('character_id')->unique();
                $table->string('avatar');
                $table->string('access_token')->nullable();
                $table->string('refresh_token')->nullable();
                $table->integer('inserted_at')->default(0);
                $table->integer('expires_in')->default(0);
                $table->string('owner_hash');
                $table->rememberToken();
                $table->timestamps();

                $table->foreign('main_id', 'fk_users_alts_main_id')
                      ->references('character_id')
                      ->on('users')
                      ->cascadeOnDelete();
            });
        }

        if(!Schema::hasTable('character_to_corporation')) {
            Schema::create('character_to_corporation', function(Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('character_id');
                $table->string('character_name');
                $table->unsignedBigInteger('corporation_id');
                $table->string('corporation_name');
            });
        }

        if(!Schema::hasTable('corporation_to_alliance')) {
            Schema::create('corporation_to_alliance', function(Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('corporation_id');
                $table->string('corporation_name');
                $table->unsignedBigInteger('alliance_id');
                $table->string('alliance_name');
            });
        }

        if(!Schema::hasTable('allowed_logins')) {
            Schema::create('allowed_logins', function(Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('entity_id');
                $table->enum('entity_type', [
                    'character',
                    'corporation',
                    'alliance',
                ]);
                $table->string('entity_name');
                $table->string('login_type');
                $table->timestamps();
            });
        }

        if(!Schema::hasTable('wiki_users')) {
            Schema::create('wiki_users', function(Blueprint $table) {
                $table->id();
                $table->string('login');
                $table->string('pass');
                $table->string('name');
                $table->string('mail')->default('')->nullable();
                $table->unique('login', 'user');
            });
        }

        if(!Schema::hasTable('wiki_members')) {
            Schema::create('wiki_members', function(Blueprint $table) {
                $table->integer('uid');
                $table->integer('gid');
                $table->string('groupname');
                $table->primary(['uid', 'gid']);
            });
        }

        if(!Schema::hasTable('wiki_groupnames')) {
            Schema::create('wiki_groupnames', function (Blueprint $table) {
                $table->id();
                $table->string('gname');
                $table->unique('id', 'id');
            });
        }
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('users');
        Schema::dropIfExists('password_reset_tokens');
        Schema::dropIfExists('sessions');
        Schema::dropIfExists('esi_token');
        Schema::dropIfExists('esi_scopes');
        Schema::dropIfExists('user_alts');
        Schema::dropIfExists('character_to_corporation');
        Schema::dropIfExists('corporation_to_alliance');
        Schema::dropIfExists('allowed_logins');
        Schema::dropIfExists('wiki_user');
        Schema::dropIfExists('wiki_member');
        Schema::dropIfExists('wiki_groupname');
    }
};
