<?php

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

class CreateArticlesTableOnEpac extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::connection('site_epac')->create('articles', function (Blueprint $table) {
            $table->id();
            $table->string('titre')->nullable(false);
            $table->string('miniature')->nullable(false);
            $table->unsignedBigInteger('departement_id')->nullable(false);
            //$table->foreign('departement_id')->references('id')->on('departements')->onDelete('cascade');
            $table->unsignedBigInteger('user_id')->nullable(false);
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->enum('statut', ['PUBLISHED', 'PENDING', 'BLOCKED', 'SIGNALED', 'DELETED'])->default('PUBLISHED');
            $table->dateTime('created_at')->nullable(false)->default(DB::raw('CURRENT_TIMESTAMP'));
            $table->dateTime('updated_at')->nullable(false)->default(DB::raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));
        });

        Schema::connection('site_epac')->create('sections', function (Blueprint $table) {
            $table->id();
            $table->string('titre')->nullable(false);
            $table->unsignedBigInteger('article_id')->nullable(false);
            $table->foreign('article_id')->references('id')->on('articles')->onDelete('cascade');
            $table->enum('statut', ['PUBLISHED', 'PENDING', 'BLOCKED', 'SIGNALED', 'DELETED'])->default('PUBLISHED');
            $table->dateTime('created_at')->nullable(false)->default(DB::raw('CURRENT_TIMESTAMP'));
            $table->dateTime('updated_at')->nullable(false)->default(DB::raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));
        });

        Schema::connection('site_epac')->create('paragraphes', function (Blueprint $table) {
            $table->id();
            $table->longText('contenu')->nullable(false);
            $table->unsignedBigInteger('section_id')->nullable(false);
            $table->foreign('section_id')->references('id')->on('sections')->onDelete('cascade');
            $table->enum('statut', ['PUBLISHED', 'PENDING', 'BLOCKED', 'SIGNALED', 'DELETED'])->default('PUBLISHED');
            $table->dateTime('created_at')->nullable(false)->default(DB::raw('CURRENT_TIMESTAMP'));
            $table->dateTime('updated_at')->nullable(false)->default(DB::raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));
        });

        Schema::connection('site_epac')->create('attachements', function (Blueprint $table) {
            $table->id();
            $table->string('nom')->nullable(false);
            $table->string('url')->nullable(false);
            $table->unsignedBigInteger('article_id')->nullable(false);
            $table->foreign('article_id')->references('id')->on('articles')->onDelete('cascade');
            $table->enum('statut', ['PUBLISHED', 'PENDING', 'BLOCKED', 'SIGNALED', 'DELETED'])->default('PUBLISHED');
            $table->dateTime('created_at')->nullable(false)->default(DB::raw('CURRENT_TIMESTAMP'));
            $table->dateTime('updated_at')->nullable(false)->default(DB::raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::connection('site_epac')->dropIfExists('attachements');
        Schema::connection('site_epac')->dropIfExists('paragraphes');
        Schema::connection('site_epac')->dropIfExists('sections');
        Schema::connection('site_epac')->dropIfExists('articles');
    }
}
