<?php

declare(strict_types=1);

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('academy_prompt_templates', function (Blueprint $table): void {
            if (! Schema::hasColumn('academy_prompt_templates', 'documentation')) {
                $table->json('documentation')->nullable()->after('workflow_notes');
            }

            if (! Schema::hasColumn('academy_prompt_templates', 'placeholders')) {
                $table->json('placeholders')->nullable()->after('documentation');
            }

            if (! Schema::hasColumn('academy_prompt_templates', 'helper_prompts')) {
                $table->json('helper_prompts')->nullable()->after('placeholders');
            }

            if (! Schema::hasColumn('academy_prompt_templates', 'prompt_variants')) {
                $table->json('prompt_variants')->nullable()->after('helper_prompts');
            }
        });
    }

    public function down(): void
    {
        Schema::table('academy_prompt_templates', function (Blueprint $table): void {
            $columns = array_values(array_filter([
                Schema::hasColumn('academy_prompt_templates', 'documentation') ? 'documentation' : null,
                Schema::hasColumn('academy_prompt_templates', 'placeholders') ? 'placeholders' : null,
                Schema::hasColumn('academy_prompt_templates', 'helper_prompts') ? 'helper_prompts' : null,
                Schema::hasColumn('academy_prompt_templates', 'prompt_variants') ? 'prompt_variants' : null,
            ]));

            if ($columns !== []) {
                $table->dropColumn($columns);
            }
        });
    }
};