<?php

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

class CreateLoopGroupsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('loop_groups', function (Blueprint $table) {
            $table->id();
            $table->bigInteger('loop_id')->unsigned()->index();
            /**
             * "max_budget" is the assumption budget for this group
             * so the outcome from this group every month must not exceed this value,
             *
             * "real_budget" the actual budget of the group after loop has been closed
             *
             * "paid" in each month object is the money must be paid every month to users.
             *
             * "remain" in each month object, demonstrates remains amount of money in this month.
             * remain= max_budget - sum(loop_group_users.share of this month for this loop group)
             * so it demonstrates if there is another place
             *
             * "Score" the minmum score the user must have to can choose this month as a collection month
             *
             * "credit" may be plus or minus values,
             * positive values mean extra money in the group credit,
             * whereas negative values mean money taken from the system to cover the  paid amount
             */
            $table->json('setting');
            $table->tinyInteger('period')->virtualAs('setting->"$.period"')->index();

            // Joins
            $table->foreign('loop_id')->references('id')->on('loops')->onDelete('cascade')->onUpdate('cascade');

            // Set engine to InnoDB it's useful in join and constraint
            $table->engine = 'InnoDB';
        });
    }

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