<?php

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

class CreateNewWebsiteObjectsSearchTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::dropIfExists('website_objects_search');

        Schema::create('website_objects_search', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('website_object_id');
            $table->string('language_id', 10);
            $table->string('title', 255);
            $table->longText('search_content');

            $table->unique(['website_object_id', 'language_id']);
            $table->index('language_id');
            $table->foreign('website_object_id')->references('website_object_id')
                ->on('website_objects')->onDelete('CASCADE')->onUpdate('RESTRICT');
        });

        DB::statement('ALTER TABLE website_objects_search ADD FULLTEXT idx_website_objects_search_ft(search_content)');
    }

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

        Schema::create('website_objects_search', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('website_object_id');
            $table->string('language_id', 10);
            $table->longText('primary_search');
            $table->longText('secondary_search');
            $table->unsignedInteger('image_id')->nullable();
            $table->string('title', 255);
            $table->text('content')->nullable();
            $table->string('content_type', 100);
            $table->string('group_label', 100);
            $table->string('uri', 255);


            $table->unique(['website_object_id', 'language_id']);
            $table->index('language_id');
            $table->foreign('website_object_id')->references('website_object_id')
                ->on('website_objects')->onDelete('CASCADE')->onUpdate('RESTRICT');

        });

        DB::statement('ALTER TABLE website_objects_search ADD FULLTEXT idx_website_objects_search_ft(primary_search, secondary_search)');
        DB::statement('ALTER TABLE website_objects_search ADD FULLTEXT idx_website_objects_primary_search_ft(primary_search)');
        DB::statement('ALTER TABLE website_objects_search ADD FULLTEXT idx_website_objects_secondary_search_ft(secondary_search)');
    }
}
