<?php

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

class CreateClimateJourneyTables extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('journey_template_cfgs', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('entry_id');
            $table->unsignedInteger('backend_user_id')->nullable();
            $table->nullableTimestamps();
            $table->unsignedInteger('hero_image_id')->nullable();
            $table->unsignedInteger('hero_image_mobile_id')->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');
            $table->foreign('hero_image_id')->references('id')
                ->on('media_items')->onDelete('SET NULL')->onUpdate('CASCADE');
            $table->foreign('hero_image_mobile_id')->references('id')
                ->on('media_items')->onDelete('SET NULL')->onUpdate('CASCADE');
        });

        Schema::create('journey_template_cfgs_lang', function (Blueprint $table) {
            $table->increments('translation_id');
            $table->unsignedInteger('journey_template_cfg_id');
            $table->string('language_id', 10);
            $table->string('title', 150);
            $table->string('hero_title', 150)->nullable();
            $table->string('hero_pretitle', 100)->nullable();
            $table->string('hero_subtitle', 255)->nullable();
            $table->string('end_title', 150)->nullable();
            $table->text('end_content')->nullable();
            $table->text('end_cta')->nullable();

            // Languages
            $table->unique(['journey_template_cfg_id', 'language_id'], 'idx_journey_template_cfg_lang');
            $table->foreign('journey_template_cfg_id')
                ->references('id')
                ->on('journey_template_cfgs')
                ->onDelete('CASCADE')
                ->onUpdate('CASCADE');
        });

        Schema::create('journey_blocks', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('journey_template_cfg_id');
            $table->unsignedInteger('backend_user_id')->nullable();
            $table->nullableTimestamps();
            $table->unsignedInteger('hero_image_id')->nullable();
            $table->unsignedInteger('hero_image_mobile_id')->nullable();
            $table->unsignedInteger('icon_id')->nullable();
            $table->string('icon_name', 20)->nullable();
            $table->string('color', 7)->nullable();
            $table->string('block_position', 20)->nullable();

            $table->foreign('journey_template_cfg_id')->references('id')
                ->on('journey_template_cfgs')->onDelete('CASCADE')->onUpdate('CASCADE');
            $table->foreign('backend_user_id')->references('id')
                ->on('backend_users')->onDelete('SET NULL')->onUpdate('CASCADE');
            $table->foreign('hero_image_id')->references('id')
                ->on('media_items')->onDelete('SET NULL')->onUpdate('CASCADE');
            $table->foreign('hero_image_mobile_id')->references('id')
                ->on('media_items')->onDelete('SET NULL')->onUpdate('CASCADE');
            $table->foreign('icon_id')->references('id')
                ->on('media_items')->onDelete('SET NULL')->onUpdate('CASCADE');
        });

        Schema::create('journey_blocks_lang', function (Blueprint $table) {
            $table->increments('translation_id');
            $table->unsignedInteger('journey_block_id');
            $table->string('language_id', 10);
            $table->string('title', 150);
            $table->text('short_description')->nullable();
            $table->string('image_title', 150)->nullable();
            $table->text('image_cta')->nullable();
            $table->string('detail_title_1', 150)->nullable();
            $table->text('detail_description_1')->nullable();
            $table->text('detail_cta_1')->nullable();
            $table->string('detail_title_2', 150)->nullable();
            $table->text('detail_description_2')->nullable();
            $table->text('detail_cta_2')->nullable();
            $table->string('detail_title_3', 150)->nullable();
            $table->text('detail_description_3')->nullable();
            $table->text('detail_cta_3')->nullable();

            // Languages
            $table->unique(['journey_block_id', 'language_id'], 'idx_journey_block_lang');
            $table->foreign('journey_block_id')
                ->references('id')
                ->on('journey_blocks')
                ->onDelete('CASCADE')
                ->onUpdate('CASCADE');
        });
/*
        Schema::create('journey_block_journey_template_cfg', function (Blueprint $table) {
            $table->unsignedInteger('journey_template_cfg_id');
            $table->unsignedInteger('journey_block_id');
            $table->integer('position')->nullable();
            $table->primary(['journey_template_cfg_id', 'journey_block_id'], 'journey_templ_cfg_block_pk');

            $table->foreign('journey_template_cfg_id', 'journey_templ_cfg_block_fk1')->references('id')
                ->on('journey_template_cfgs')->onDelete('CASCADE')->onUpdate('CASCADE');
            $table->foreign('journey_block_id', 'journey_templ_cfg_block_fk2')->references('id')
                ->on('journey_blocks')->onDelete('CASCADE')->onUpdate('CASCADE');
        });
        */
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        /*
        Schema::dropIfExists('journey_block_journey_template_cfg');
        */
        Schema::dropIfExists('journey_blocks_lang');
        Schema::dropIfExists('journey_blocks');
        Schema::dropIfExists('journey_template_cfgs_lang');
        Schema::dropIfExists('journey_template_cfgs');
    }
}
