<?php

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

class CreatePointsTransactionTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('points_transactions', function (Blueprint $table) {
            $table->increments('id');
            $table->string('identifier', 32)->nullable()->index();
            $table->integer('user_id')->unsigned()->nullable()->index();
            $table->enum('action', ['add', 'substract'])->default('add');
            $table->integer('amount')->default(0);
            $table->integer('task_id')->unsigned()->nullable()->index();
            $table->string('description')->nullable();
            $table->integer('created_by')->unsigned()->nullable();
            $table->timestamps();
            $table->softDeletes();

            $table->index('created_at');
        });

        Schema::table('users', function (Blueprint $table) {
            $table->integer('points')->default(0)->after('suspended');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('points_transactions');

        Schema::table('users', function (Blueprint $table) {
            $table->dropColumn('points');
        });
    }
}
