<?php

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

return new class extends Migration {
    public function up(): void
    {
        Schema::table('tickets', function (Blueprint $table) {
            $table->foreignId('campaign_id')->nullable()->index()->after('source_id');
            $table->text('description')->nullable()->after('topic');
            $table->text('comments')->nullable()->after('description');
        });
        Schema::table('notes', fn (Blueprint $table) => $table->foreignId('ticket_id')->nullable()->index()->after('deal_id'));

        Schema::create('ticket_status_transitions', function (Blueprint $table) {
            $table->id();
            $table->foreignId('business_id')->nullable()->index();
            $table->foreignId('from_stage_id')->index();
            $table->foreignId('to_stage_id')->index();
            $table->boolean('active')->default(true);
            $table->boolean('requires_comment')->default(false);
            $table->timestamps();
            $table->unique(['business_id', 'from_stage_id', 'to_stage_id'], 'ticket_transition_unique');
        });

        Schema::create('ticket_move_histories', function (Blueprint $table) {
            $table->id();
            $table->foreignId('business_id')->index();
            $table->foreignId('ticket_id')->index();
            $table->string('destination_type');
            $table->unsignedBigInteger('destination_id');
            $table->foreignId('moved_by')->nullable()->index();
            $table->text('reason')->nullable();
            $table->json('relationship_snapshot')->nullable();
            $table->timestamps();
            $table->index(['destination_type', 'destination_id']);
        });

        Schema::create('ticket_assignment_histories', function (Blueprint $table) {
            $table->id();
            $table->foreignId('business_id')->index();
            $table->foreignId('ticket_id')->index();
            $table->foreignId('from_user_id')->nullable()->index();
            $table->foreignId('to_user_id')->nullable()->index();
            $table->foreignId('assigned_by')->nullable()->index();
            $table->text('note')->nullable();
            $table->timestamps();
        });

        $stages = DB::table('ticket_stages')->orderBy('id')->pluck('id')->values();
        foreach ($stages as $index => $stageId) {
            if (isset($stages[$index + 1])) {
                DB::table('ticket_status_transitions')->insert([
                    'business_id' => null,
                    'from_stage_id' => $stageId,
                    'to_stage_id' => $stages[$index + 1],
                    'active' => true,
                    'requires_comment' => false,
                    'created_at' => now(),
                    'updated_at' => now(),
                ]);
            }
        }
    }

    public function down(): void
    {
        Schema::dropIfExists('ticket_assignment_histories');
        Schema::dropIfExists('ticket_move_histories');
        Schema::dropIfExists('ticket_status_transitions');
        Schema::table('notes', fn (Blueprint $table) => $table->dropColumn('ticket_id'));
        Schema::table('tickets', fn (Blueprint $table) => $table->dropColumn([
            'campaign_id', 'description', 'comments',
        ]));
    }
};
