<?php

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

class RefactorTradefairsUseGeoForeignKeys extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('tradefairs', function (Blueprint $table) {
            $table->unsignedBigInteger('continent_id')->nullable()->after('fair_logo');
            $table->unsignedInteger('country_id')->nullable();
            $table->unsignedInteger('city_id')->nullable();
        });

        Schema::table('tradefairs', function (Blueprint $table) {
            if (Schema::hasColumn('tradefairs', 'continent')) {
                $table->dropColumn(['continent', 'country', 'eventvenue']);
            }
        });

        Schema::table('tradefairs', function (Blueprint $table) {
            $table->foreign('continent_id')->references('id')->on('continents')->nullOnDelete();
            $table->foreign('country_id')->references('id')->on('countries')->nullOnDelete();
            $table->foreign('city_id')->references('id')->on('cities')->nullOnDelete();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('tradefairs', function (Blueprint $table) {
            $table->dropForeign(['continent_id']);
            $table->dropForeign(['country_id']);
            $table->dropForeign(['city_id']);
        });

        Schema::table('tradefairs', function (Blueprint $table) {
            $table->dropColumn(['continent_id', 'country_id', 'city_id']);
        });

        Schema::table('tradefairs', function (Blueprint $table) {
            $table->string('continent', 100)->nullable()->after('fair_logo');
            $table->string('country', 100)->nullable();
            $table->string('eventvenue', 100)->nullable();
        });
    }
}
