<?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('lead_stages', function (Blueprint $table) {
            $table->string('lifecycle_key')->nullable()->index();
            $table->boolean('is_active')->default(true)->index();
            $table->boolean('is_terminal')->default(false);
        });

        $canonical = [
            1 => ['new', 'New Lead', false],
            2 => ['need_analysis', 'Need Analysis', false],
            3 => ['attempted_contact', 'Attempted to Contact', false],
            4 => ['introductory_meeting', 'Introductory Meeting', false],
            5 => ['contacted', 'Contacted', false],
            6 => ['qualified', 'Qualified', true],
            7 => ['unqualified', 'Unqualified', true],
        ];
        foreach ($canonical as $id => [$key, $name, $terminal]) {
            DB::table('lead_stages')->where('id', $id)->update([
                'name' => $name, 'lifecycle_key' => $key, 'is_active' => true, 'is_terminal' => $terminal,
            ]);
        }
        DB::table('lead_stages')->whereNotIn('id', array_keys($canonical))->update(['is_active' => false]);

        Schema::table('leads', function (Blueprint $table) {
            $table->text('unqualified_reason')->nullable()->after('rating');
            $table->foreignId('duplicate_of_id')->nullable()->index()->after('unqualified_reason');
            $table->timestamp('completed_at')->nullable()->after('duplicate_of_id');
        });
        Schema::table('cases', fn (Blueprint $table) => $table->foreignId('lead_id')->nullable()->index()->after('contact_id'));

        Schema::create('lead_project', function (Blueprint $table) {
            $table->id(); $table->foreignId('lead_id')->index(); $table->foreignId('project_id')->index();
            $table->timestamps(); $table->unique(['lead_id', 'project_id']);
        });
        Schema::create('lead_survey', function (Blueprint $table) {
            $table->id(); $table->foreignId('lead_id')->index(); $table->foreignId('survey_id')->index();
            $table->timestamps(); $table->unique(['lead_id', 'survey_id']);
        });
        Schema::create('lead_conversion_histories', function (Blueprint $table) {
            $table->id();
            $table->foreignId('business_id')->index();
            $table->foreignId('lead_id')->index();
            $table->foreignId('deal_id')->nullable()->index();
            $table->foreignId('converted_by')->nullable()->index();
            $table->string('status')->default('completed');
            $table->json('duplicate_matches')->nullable();
            $table->json('relationship_snapshot')->nullable();
            $table->text('notes')->nullable();
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('lead_conversion_histories');
        Schema::dropIfExists('lead_survey');
        Schema::dropIfExists('lead_project');
        Schema::table('cases', fn (Blueprint $table) => $table->dropColumn('lead_id'));
        Schema::table('leads', fn (Blueprint $table) => $table->dropColumn(['unqualified_reason', 'duplicate_of_id', 'completed_at']));
        Schema::table('lead_stages', fn (Blueprint $table) => $table->dropColumn(['lifecycle_key', 'is_active', 'is_terminal']));
    }
};
