<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('decisions', function (Blueprint $table) {
            if (! Schema::hasColumn('decisions', 'auto_mail')) {
                $table->boolean('auto_mail')->default(false)->after('color_tag');
            }
            if (! Schema::hasColumn('decisions', 'email_template_id')) {
                $table->foreignId('email_template_id')->nullable()->after('auto_mail')->constrained('email_templates')->nullOnDelete();
            }
        });
    }

    public function down(): void
    {
        Schema::table('decisions', function (Blueprint $table) {
            if (Schema::hasColumn('decisions', 'email_template_id')) {
                $table->dropConstrainedForeignId('email_template_id');
            }
            if (Schema::hasColumn('decisions', 'auto_mail')) {
                $table->dropColumn('auto_mail');
            }
        });
    }
};
