<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('environment_support', function (Blueprint $table) {

            $table->id();

            $table->foreignId('environment_id')
                ->constrained('environments')
                ->cascadeOnDelete()
                ->cascadeOnUpdate();

            $table->foreignId('support_id')
                ->constrained('supports')
                ->cascadeOnDelete()
                ->cascadeOnUpdate();

            $table->string('comment')->nullable();
            $table->date('start_at');
            $table->date('end_at');

            $table->timestamps();
        });

        Schema::table('environments', function (Blueprint $table) {

            $table->foreign('environment_support_id')
                ->references('id')
                ->on('environment_support')
                ->nullOnDelete()
                ->cascadeOnUpdate();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('environments', function (Blueprint $table) {
            $table->dropForeign(['environment_support_id']);
        });

        Schema::dropIfExists('environment_support');
    }
};
