<?php

use Schema\Wrapper as Wrapper;

function core_auth_upgrade($pluginversion)
{
    if ($pluginversion < "0.0.1") {

        $table = Wrapper::get_table("auth_user");
        $table->addColumn("method", "string", array("null" => false, 'default' => 'manual'));
        $table->addColumn("username", "string", array("null" => false));
        $table->addColumn("name", "string", array("null" => false));
        $table->addColumn("email", "string", array("null" => false));
        $table->addColumn("password", "string", array("null" => true));
        $table->addIndex(['username'], ['unique' => true]);
        $table->addIndex(['email'],    ['unique' => true]);
        $table->addTimestamps();
        $table->addSoftDelete();
        $table->create();

        $table = Wrapper::get_table("auth_permission");
        $table->addColumn("name", "string", array("null" => false, "limit" => 255));
        $table->addColumn("description", "string", array("null" => true));
        $table->create();

        $table = Wrapper::get_table("auth_group");
        $table->addColumn("name", "string", array("null" => false, "limit" => 255));
        $table->addColumn("description", "string", array("null" => true));
        $table->create();

        $table = Wrapper::get_table("auth_group_user");
        $table->addColumn("user_id",  "integer", array("null" => false));
        $table->addColumn("group_id", "integer", array("null" => false));
        $table->addForeignKey('user_id',  Wrapper::get_table("auth_user"),  'id', ['delete' => 'SET_NULL', 'update' => 'NO_ACTION']);
        $table->addForeignKey('group_id', Wrapper::get_table("auth_group"), 'id', ['delete' => 'SET_NULL', 'update' => 'NO_ACTION']);
        $table->create();

        $table = Wrapper::get_table("auth_permission_group");
        $table->addColumn("group_id", "integer", array("null" => false));
        $table->addColumn("permission_id", "integer", array("null" => false));
        $table->addForeignKey('group_id',       Wrapper::get_table("auth_group"),       'id', ['delete' => 'SET_NULL', 'update' => 'NO_ACTION']);
        $table->addForeignKey('permission_id',  Wrapper::get_table("auth_permission"),  'id', ['delete' => 'SET_NULL', 'update' => 'NO_ACTION']);
        $table->create();

        $table = Wrapper::get_table("auth_permission_user");
        $table->addColumn("user_id", "integer", array("null" => false));
        $table->addColumn("permission_id", "integer", array("null" => false));
        $table->addColumn("action", "string", array("null" => false, 'limit' => 1));
        $table->addForeignKey('user_id',       Wrapper::get_table("auth_group"),      'id', ['delete' => 'SET_NULL', 'update' => 'NO_ACTION']);
        $table->addForeignKey('permission_id', Wrapper::get_table("auth_permission"), 'id', ['delete' => 'SET_NULL', 'update' => 'NO_ACTION']);
        $table->create();

        Wrapper::exec(file_get_contents(__DIR__ . '/001_auth_seeding.sql'));

        App\Core\Sanity\MigratorController::getInstance()->update_plugin_version("core_auth", "1.0.0");

        return;
    }

    /**
     * Create logs tables
     */
    if ($pluginversion < "1.0.1") {
        $table = Wrapper::get_table("auth_log");
        $table->addColumn("user_id",       "integer", array("null" => false));
        $table->addColumn("permission_id", "integer", array("null" => false));
        $table->addColumn("real_user_id",  "integer", array("null" => false));
        $table->addForeignKey('user_id',      Wrapper::get_table("auth_user"), 'id');
        $table->addForeignKey('real_user_id', Wrapper::get_table("auth_user"), 'id');
        $table->create();

        App\Core\Sanity\MigratorController::getInstance()->update_plugin_version("core_auth", "1.0.1");
    }

    /**
     * Create token tables
     */
    if ($pluginversion < "1.0.2") {
        $table = Wrapper::get_table("auth_token");
        $table->addColumn("user_id", "integer", array("null" => false));
        $table->addColumn("token", "string", array("null" => false, "limit" => 64));
        $table->addForeignKey('user_id',  Wrapper::get_table("auth_user"),  'id', ['delete' => 'SET_NULL', 'update' => 'NO_ACTION']);
        $table->create();

        App\Core\Sanity\MigratorController::getInstance()->update_plugin_version("core_auth", "1.0.2");
    }
}

function core_auth_rollback($pluginversion)
{
    if ($pluginversion > "0.1.0") {
        $table = Wrapper::get_table("auth");
        $table->drop();
        return;
    }
}
