<?php

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

class UserTable extends Migration
{
    const TABLE = 'table_user';

    public function user()
	{
		Schema::create(static::TABLE, function (Blueprint $table) {
			$table->increments('id');
			$table->uuid('uuid')->unique();

			$table->string('first_name')->nullable();
			$table->string('last_name')->nullable();
			$table->string('password')->nullable(true);

			$table->string('email')->nullable();

			$table->string('phone_code')->nullable();
			$table->string('phone_number')->nullable();

			$table->string('nickname')->nullable();

			$table->boolean('is_registered')->default(false);
			$table->boolean('is_active')->default(0);
			$table->boolean('is_verified')->default(0);
			$table->boolean('is_system_user')->default(0);

			$table->unsignedInteger('avatar_id')->nullable();

			$table->boolean('is_notification_push')->default(1);
			$table->boolean('is_notification_app')->default(1);

			$table->integer('last_notification_seen')->default(0);

			$table->dateTime('last_activity_at')->nullable();

			$table->unsignedSmallInteger('count_capsules')->default(0);
			$table->unsignedSmallInteger('count_friends')->default(0);
			$table->unsignedSmallInteger('count_comments')->default(0);
			$table->unsignedSmallInteger('count_subscriptions')->default(0);

            $table->boolean('is_on_boarding_done')->default(false);
            $table->boolean('is_test')->nullable();

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

            $table->foreign('avatar_id')->references('id')->on(UserTable::TABLE_USER_AVATAR)->onDelete('set null');

            $table->index('phone_number');
            $table->index('phone_code');
            $table->index(['phone_code', 'phone_number']);
            $table->index('nickname');
            $table->index('email');
            $table->index('is_registered');
            $table->index('created_at');
            $table->index('updated_at');
		});

        DB::statement("CREATE INDEX idx_user_full_name ON ".UserTable::TABLE." ((first_name || ' ' || last_name))");
        DB::statement("CREATE INDEX idx_user_year_created_at ON ".UserTable::TABLE." (date_part('year', created_at))");
        DB::statement("CREATE INDEX idx_user_month_created_at ON ".UserTable::TABLE." (date_part('month', created_at))");
	}

	const TABLE_TOKEN = 'table_user_token';

	public function tokenTable()
	{
		Schema::create(self::TABLE_TOKEN, function (Blueprint $table) {
			$table->increments('id');
			$table->integer('user_id');
			$table->string('access_token')->unique();
			$table->timestamp('expires_in')->nullable();

			$table->timestamps();

            $table->foreign('user_id')->references('id')->on(UserTable::TABLE)->onDelete('cascade');

            $table->index('access_token');
            $table->index(['access_token', 'expires_in']);
		});
	}

	const TABLE_PHONE_CONFIRM = 'table_user_phone_confirm';

	public function phoneConfirm()
	{
		Schema::create(static::TABLE_PHONE_CONFIRM, function (Blueprint $table) {
			$table->increments('id');
			$table->unsignedInteger('user_id')->nullable();

			$table->string('phone_code')->nullable();
			$table->string('phone_number')->nullable();
			$table->unsignedInteger('code');

			$table->timestamps();

            $table->foreign('user_id')->references('id')->on(UserTable::TABLE)->onDelete('cascade');

            $table->unique('code');
		});
	}

	const TABLE_USER_AVATAR = 'table_user_avatar';

	public function userAvatar()
	{
		Schema::create(static::TABLE_USER_AVATAR, function (Blueprint $table) {
			$table->increments('id');
			$table->uuid('uuid')->unique();

			$table->string('hash')->nullable();
			$table->string('ext')->nullable();
			$table->string('mime_type')->nullable();
			$table->string('size')->nullable();
			$table->string('width')->nullable();
			$table->string('height')->nullable();

			$table->string('aws_key')->nullable();
			$table->string('aws_bucket')->nullable();

			$table->timestamps();

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

	const TABLE_PASSWORD = 'table_user_password_reset';

	public function passwordReset()
	{
		Schema::create(static::TABLE_PASSWORD, function (Blueprint $table) {

			$table->increments('id');

			$table->uuid('token')->unique();
			$table->integer('user_id')->nullable();

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

			$table->timestamps();

            $table->foreign('user_id')->references('id')->on(UserTable::TABLE)->onDelete('cascade');

            $table->index(['expire_at']);
		});
	}

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        // User avatar
        $this->userAvatar();

    	// User table
		$this->user();

		// Token table
		$this->tokenTable();

		// Phone confirm
		$this->phoneConfirm();

		// Password reset
		$this->passwordReset();



    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
		Schema::dropIfExists(self::TABLE_PASSWORD);

		Schema::dropIfExists(self::TABLE_USER_AVATAR);

		Schema::dropIfExists(self::TABLE_PHONE_CONFIRM);

		Schema::dropIfExists(self::TABLE_TOKEN);

        Schema::dropIfExists(static::TABLE);
    }
}
