<?php

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

class CreatePhraseAndLangTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('phrases', function (Blueprint $table) {
            $table->bigIncrements('id');

            $table->unsignedBigInteger('backend_user_id')->nullable();
            $table->foreign('backend_user_id')->references('id')
                ->on('backend_users')->onDelete('SET NULL')->onUpdate('CASCADE');

            $table->boolean('is_content_type_specific');
            $table->boolean('is_loader_title');

            $table->timestamps();
        });

        Schema::create('phrase_languages', function (Blueprint $table) {
            $table->bigIncrements('translation_id');

            $table->unsignedBigInteger('phrase_id');
            $table->string('language_id', 10);

            $table->string('title', 150);
            $table->text('phrase');

            $table->unique(['phrase_id', 'language_id'], 'idx_phrase_lang');
            $table->foreign('phrase_id')
                ->references('id')
                ->on('phrases')
                ->onDelete('CASCADE')
                ->onUpdate('CASCADE');

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

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