<?php

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

class CreateProjectTemplateTables extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('projects_template_cfgs', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('entry_id');
            $table->unsignedInteger('backend_user_id')->nullable();
            $table->nullableTimestamps();
            $table->string('project_count', 5)->nullable();

            $table->foreign('backend_user_id')->references('id')
                ->on('backend_users')->onDelete('SET NULL')->onUpdate('CASCADE');
            $table->foreign('entry_id')->references('id')
                ->on('entries')->onDelete('CASCADE')->onUpdate('CASCADE');
        });

        Schema::create('projects_template_cfgs_lang', function (Blueprint $table) {
            $table->increments('translation_id');
            $table->unsignedInteger('projects_template_cfg_id');
            $table->string('language_id', 10);
            $table->string('title', 150);
            $table->string('hero_title', 150)->nullable();
            $table->text('hero_content')->nullable();

            // Languages
            $table->unique(['projects_template_cfg_id', 'language_id'], 'idx_projects_temp_cfg_lang');
            $table->foreign('projects_template_cfg_id')
                ->references('id')
                ->on('projects_template_cfgs')
                ->onDelete('CASCADE')
                ->onUpdate('CASCADE');
        });
    }

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