<?php

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

/**
 * Class CreateParametersTable
 */
class CreateEntityParametersTable extends Migration
{
    protected $tableName = 'parameters';

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create($this->tableName, function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('entity_class_parameter_id');
            $table->unsignedInteger('entity_id');
            $table->string(EntityParameter::STRING, EntityParameter::STRING_DB_LEN)->nullable();
            $table->tinyInteger(EntityParameter::TINY_INT)->nullable();
            $table->integer(EntityParameter::INT)->nullable();
            $table->dateTime(EntityParameter::DATETIME)->nullable();
            $table->binary( EntityParameter::BLOB)->nullable();

            // Metadata of this parameter
            $table->foreign('entity_class_parameter_id')
                ->references('id')->on('class_parameters')
                ->onDelete('cascade')->onUpdate('cascade');

            // Which entity this parameter belongs
            $table->foreign('entity_id')
                ->references('id')->on('entities')
                ->onDelete('cascade')->onUpdate('cascade');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists($this->tableName);
    }
}
