<?php

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

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');

            $table->unsignedInteger('account_id')->nullable();
            $table->unsignedInteger('m_level')->nullable();
            $table->unsignedInteger('m_sponsor')->nullable();

            $table->unsignedTinyInteger('admin')->default(0);
            $table->unsignedTinyInteger('wizard')->default(0);
            $table->boolean('blocked')->default(false);

            $table->boolean('confirmed')->default(false);
            $table->string('confirmation_code', 30)->index()->nullable();
            $table->timestamp('confirmation_date')->nullable();
            $table->dateTime('confirmation_code_to')->nullable();
            $table->unsignedTinyInteger('confirmation_code_remider')->default(0);

            $table->boolean('active')->default(false);
            $table->timestamp('active_date')->nullable();
            $table->dateTime('agreement')->nullable();

            $table->char('lang', 2)->nullable();
            $table->text('notes')->nullable();

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

            $table->timestamp('release_account')->nullable();
            $table->timestamp('account_payment')->nullable();

            $table->decimal('payment_credit', 10,2)->default(0);

            $table->string('payment_methods')->nullable();
            $table->unsignedInteger('next_m_level')->nullable();
            $table->boolean('abo_options')->default(false);

            $table->boolean('test_mode')->default(false);
            $table->text('settings')->nullable();

            $table->rememberToken();

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

            $table->foreign('account_id')
                ->references('id')
                ->on('user_accounts');

            $table->foreign('m_level')
                ->references('id')
                ->on('user_levels');

            $table->foreign('next_m_level')
                ->references('id')
                ->on('user_levels');

            $table->foreign('m_sponsor')
                ->references('id')
                ->on('users');

        });

    }

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