<?php

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

class CreateProjectAndLangTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('projects', function (Blueprint $table) {
            $table->bigIncrements('id');

            $table->unsignedBigInteger('main_image_id')->nullable();
            $table->foreign('main_image_id')->references('id')
                ->on('media_items')->onDelete('RESTRICT')->onUpdate('RESTRICT');

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

            $table->unsignedBigInteger('backend_user_id')->nullable();
            $table->foreign('backend_user_id')->references('id')
                ->on('backend_users')->onDelete('SET NULL')->onUpdate('CASCADE');

            $table->string('project_type', 20)->nullable();

            $table->timestamps();
        });

        Schema::create('project_languages', function (Blueprint $table) {
            $table->bigIncrements('translation_id');
            $table->unsignedBigInteger('project_id');
            $table->string('language_id', 10);

            $table->string('project_title', 300);
            $table->string('title', 150);
            $table->string('project_keywords', 150)->nullable();

            $table->string('meta_title', 150)->nullable();
            $table->string('meta_description', 300)->nullable();

            $table->unique(['project_id', 'language_id'], 'idx_project_lang');
            $table->foreign('project_id')
                ->references('id')
                ->on('projects')
                ->onDelete('CASCADE')
                ->onUpdate('CASCADE');

            $table->timestamps();
        });
    }

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