<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * 영업 관리 테이블에 tenant_prospect_id 추가
     * - 가망고객 단계에서도 영업/매니저 체크리스트 사용 가능하게 함
     */
    public function up(): void
    {
        // sales_tenant_managements 테이블에 tenant_prospect_id 추가
        Schema::table('sales_tenant_managements', function (Blueprint $table) {
            $table->unsignedBigInteger('tenant_prospect_id')->nullable()->after('tenant_id');
            $table->foreign('tenant_prospect_id')->references('id')->on('tenant_prospects')->onDelete('cascade');

            // tenant_id를 nullable로 변경 (가망고객 단계에서는 tenant 없음)
            $table->unsignedBigInteger('tenant_id')->nullable()->change();
        });

        // sales_scenario_checklists 테이블에 tenant_prospect_id 추가
        Schema::table('sales_scenario_checklists', function (Blueprint $table) {
            $table->unsignedBigInteger('tenant_prospect_id')->nullable()->after('tenant_id');
            $table->foreign('tenant_prospect_id')->references('id')->on('tenant_prospects')->onDelete('cascade');

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

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('sales_scenario_checklists', function (Blueprint $table) {
            $table->dropForeign(['tenant_prospect_id']);
            $table->dropColumn('tenant_prospect_id');
        });

        Schema::table('sales_tenant_managements', function (Blueprint $table) {
            $table->dropForeign(['tenant_prospect_id']);
            $table->dropColumn('tenant_prospect_id');
        });
    }
};
