<?php

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

class ChangeEventItemBlockRelation extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('event_item_blocks', function (Blueprint $table) {
            DB::statement('ALTER TABLE event_item_blocks CHARACTER SET utf8 COLLATE utf8_general_ci');
            $table->dropForeign(['event_item_id']);
            $table->dropForeign(['block_id']);
            $table->dropPrimary();
            $table->string('language_id', 10)->default('en');

            $table->primary(['event_item_id', 'block_id', 'language_id']);
            $table->foreign('event_item_id')
              ->references('id')->on('event_items')
              ->onDelete('CASCADE')->onUpdate('CASCADE');
            $table->foreign('block_id')
              ->references('id')->on('blocks')
              ->onDelete('CASCADE')->onUpdate('CASCADE');
            $table->foreign('language_id')
              ->references('id')->on('languages')
              ->onDelete('CASCADE')->onUpdate('CASCADE');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('event_item_blocks', function (Blueprint $table) {
            $table->dropForeign(['event_item_id']);
            $table->dropForeign(['block_id']);
            $table->dropForeign(['language_id']);
            $table->dropPrimary();
            $table->dropColumn('language_id');

            $table->primary(['event_item_id', 'block_id']);
            $table->foreign('event_item_id')
              ->references('id')->on('event_items')
              ->onDelete('CASCADE')->onUpdate('CASCADE');
            $table->foreign('block_id')
              ->references('id')->on('blocks')
              ->onDelete('CASCADE')->onUpdate('CASCADE');
        });
    }
}
