<?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('tickets', function (Blueprint $table) {
            $table->unsignedBigInteger('merged_into_ticket_id')->nullable()->after('status_id');
            $table->timestamp('merged_at')->nullable()->after('merged_into_ticket_id');
            $table->unsignedBigInteger('merged_by_admin_id')->nullable()->after('merged_at');

            $table->foreign('merged_into_ticket_id')
                ->references('id')->on('tickets')
                ->nullOnDelete();
            $table->index(['merged_into_ticket_id', 'merged_at']);
        });
    }

    public function down(): void
    {
        Schema::table('tickets', function (Blueprint $table) {
            $table->dropForeign(['merged_into_ticket_id']);
            $table->dropIndex(['merged_into_ticket_id', 'merged_at']);
            $table->dropColumn(['merged_into_ticket_id', 'merged_at', 'merged_by_admin_id']);
        });
    }
};
