<?php

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

/**
 * Class CreateEntityClassesTable
 */
class CreateEntityClassesTable extends Migration
{
    protected $tableName = "entity_classes";

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create($this->tableName, function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->boolean('is_type')->default(false);
            $table->boolean('require_parent')->default(false);
            $table->unsignedInteger('entity_type_class_id')->nullable();
            $table->boolean('has_manufacturer')->default(false);
            $table->boolean('has_supplier')->default(false);
            $table->boolean('has_location')->default(false);
            $table->timestamps();

            // Self relation
            $table->foreign('entity_type_class_id')
                ->references('id')->on($this->tableName)
                ->onDelete('set null')->onUpdate('cascade');

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

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