<?php

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

class AddColumnsBlocksTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('blocks', function (Blueprint $table) {
            $table->date('date')->nullable();

            // Image
            $table->unsignedBigInteger('image_id')->nullable();
            $table->foreign('image_id')->references('id')
                ->on('media_items')->onDelete('SET NULL')->onUpdate('CASCADE');

            // Image Mobile
            $table->unsignedBigInteger('image_mobile_id')->nullable();
            $table->foreign('image_mobile_id')->references('id')
                ->on('media_items')->onDelete('SET NULL')->onUpdate('CASCADE');

            // Document
            $table->unsignedBigInteger('document_id')->nullable();
            $table->foreign('document_id')->references('id')
                ->on('media_items')->onDelete('SET NULL')->onUpdate('CASCADE');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('blocks', function (Blueprint $table) {
            $table->dropForeign(['image_id']);
            $table->dropForeign(['image_mobile_id']);
            $table->dropForeign(['document_id']);
            $table->dropColumn('image_id');
            $table->dropColumn('image_mobile_id');
            $table->dropColumn('document_id');
        });
    }
}
