<?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
    {
        // events table extra columns
        Schema::table('events', function (Blueprint $table) {
            if (! Schema::hasColumn('events', 'key')) {
                $table->string('key')->nullable()->after('id');
            }
            if (! Schema::hasColumn('events', 'description')) {
                $table->text('description')->nullable()->after('name');
            }
            if (! Schema::hasColumn('events', 'active')) {
                $table->boolean('active')->default(true)->after('name');
            }
            if (! Schema::hasColumn('events', 'config')) {
                $table->json('config')->nullable()->after('active');
            }
        });

        // decision_event pivot enhancements
        if (Schema::hasTable('decision_event')) {
            Schema::table('decision_event', function (Blueprint $table) {
                if (! Schema::hasColumn('decision_event', 'run_order')) {
                    $table->integer('run_order')->nullable()->after('event_id');
                }
                if (! Schema::hasColumn('decision_event', 'active')) {
                    $table->boolean('active')->default(true)->after('run_order');
                }
                if (! Schema::hasColumn('decision_event', 'config')) {
                    $table->json('config')->nullable()->after('active');
                }
            });
        }

        // logs table
        if (! Schema::hasTable('decision_event_logs')) {
            Schema::create('decision_event_logs', function (Blueprint $table) {
                $table->id();
                $table->foreignIdFor(\App\Models\Decision::class)->nullable();
                $table->foreignIdFor(\App\Models\Event::class, 'event_id');
                $table->foreignIdFor(\App\Models\Activity::class);
                $table->string('handler')->nullable();
                $table->enum('status', ['queued', 'running', 'succeeded', 'failed', 'skipped'])->default('queued');
                $table->text('message')->nullable();
                $table->string('idempotency_key')->unique();
                $table->timestamp('started_at')->nullable();
                $table->timestamp('finished_at')->nullable();
                $table->timestamps();
                $table->index(['decision_id', 'event_id']);
                $table->index(['activity_id']);
            });
        }
    }

    public function down(): void
    {
        if (Schema::hasTable('decision_event_logs')) {
            Schema::drop('decision_event_logs');
        }

        if (Schema::hasTable('decision_event')) {
            Schema::table('decision_event', function (Blueprint $table) {
                if (Schema::hasColumn('decision_event', 'config')) {
                    $table->dropColumn('config');
                }
                if (Schema::hasColumn('decision_event', 'active')) {
                    $table->dropColumn('active');
                }
                if (Schema::hasColumn('decision_event', 'run_order')) {
                    $table->dropColumn('run_order');
                }
            });
        }

        Schema::table('events', function (Blueprint $table) {
            if (Schema::hasColumn('events', 'config')) {
                $table->dropColumn('config');
            }
            if (Schema::hasColumn('events', 'active')) {
                $table->dropColumn('active');
            }
            if (Schema::hasColumn('events', 'description')) {
                $table->dropColumn('description');
            }
            if (Schema::hasColumn('events', 'key')) {
                $table->dropColumn('key');
            }
        });
    }
};
