<?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('property_interests', function (Blueprint $table): void {
            $table->jsonb('qualification')->nullable()->after('status');
            $table->foreignId('assigned_user_id')->nullable()->after('qualification')->constrained('users')->nullOnDelete();
        });

        DB::table('property_interests')->where('status', 'registered')->update(['status' => 'new']);

        Schema::create('conversation_summaries', function (Blueprint $table): void {
            $table->uuid('id')->primary();
            $table->foreignUuid('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->foreignUuid('contact_id')->constrained('contacts')->cascadeOnDelete();
            $table->foreignUuid('conversation_id')->constrained('conversations')->cascadeOnDelete();
            $table->text('body');
            $table->jsonb('structured')->nullable();
            $table->timestamps();

            $table->index(['tenant_id', 'conversation_id', 'created_at']);
        });

        Schema::create('human_handoffs', function (Blueprint $table): void {
            $table->uuid('id')->primary();
            $table->foreignUuid('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->foreignUuid('contact_id')->constrained('contacts')->cascadeOnDelete();
            $table->foreignUuid('conversation_id')->constrained('conversations')->cascadeOnDelete();
            $table->foreignUuid('conversation_summary_id')->constrained('conversation_summaries')->cascadeOnDelete();
            $table->string('status', 32)->default('waiting');
            $table->unsignedInteger('priority_score')->default(0);
            $table->foreignId('assigned_user_id')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('queued_at');
            $table->timestamp('claimed_at')->nullable();
            $table->timestamp('completed_at')->nullable();
            $table->timestamps();

            $table->index(['tenant_id', 'status', 'priority_score']);
            $table->index(['tenant_id', 'queued_at']);
        });

        Schema::create('lead_scores', function (Blueprint $table): void {
            $table->uuid('id')->primary();
            $table->foreignUuid('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->foreignUuid('human_handoff_id')->constrained('human_handoffs')->cascadeOnDelete();
            $table->unsignedSmallInteger('urgency_score')->default(0);
            $table->unsignedSmallInteger('interest_score')->default(0);
            $table->unsignedSmallInteger('properties_viewed')->default(0);
            $table->boolean('requested_visit')->default(false);
            $table->unsignedInteger('wait_minutes')->default(0);
            $table->unsignedInteger('total_score')->default(0);
            $table->jsonb('factors')->nullable();
            $table->timestamps();

            $table->unique('human_handoff_id');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('lead_scores');
        Schema::dropIfExists('human_handoffs');
        Schema::dropIfExists('conversation_summaries');

        Schema::table('property_interests', function (Blueprint $table): void {
            $table->dropConstrainedForeignId('assigned_user_id');
            $table->dropColumn('qualification');
        });

        DB::table('property_interests')->where('status', 'new')->update(['status' => 'registered']);
    }
};
