<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     * sales_consultations 테이블에 tenant_prospect_id 컬럼 추가 (가망고객 상담 기록 지원)
     */
    public function up(): void
    {
        Schema::table('sales_consultations', function (Blueprint $table) {
            // tenant_prospect_id 컬럼 추가 (tenant_id 다음에)
            $table->unsignedBigInteger('tenant_prospect_id')
                ->nullable()
                ->after('tenant_id')
                ->comment('가망고객 ID (테넌트 전환 전)');

            // tenant_id를 nullable로 변경 (가망고객일 경우 null)
            $table->unsignedBigInteger('tenant_id')->nullable()->change();

            // 인덱스 추가
            $table->index(['tenant_prospect_id', 'scenario_type'], 'consultations_prospect_scenario_idx');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('sales_consultations', function (Blueprint $table) {
            $table->dropIndex('consultations_prospect_scenario_idx');
            $table->dropColumn('tenant_prospect_id');

            // tenant_id를 다시 not nullable로 변경
            $table->unsignedBigInteger('tenant_id')->nullable(false)->change();
        });
    }
};
