<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('sales_scenario_checklists', function (Blueprint $table) {
            // scenario_type 컬럼 추가 (tenant_id 다음에)
            if (!Schema::hasColumn('sales_scenario_checklists', 'scenario_type')) {
                $table->enum('scenario_type', ['sales', 'manager'])
                    ->default('sales')
                    ->comment('시나리오 유형')
                    ->after('tenant_id');
            }
        });

        Schema::table('sales_scenario_checklists', function (Blueprint $table) {
            // checkpoint_id 컬럼 추가 (step_id 다음에)
            if (!Schema::hasColumn('sales_scenario_checklists', 'checkpoint_id')) {
                $table->string('checkpoint_id', 50)
                    ->nullable()
                    ->comment('체크포인트 ID')
                    ->after('step_id');
            }
        });

        Schema::table('sales_scenario_checklists', function (Blueprint $table) {
            // checked_at 컬럼 추가 (is_checked 다음에)
            if (!Schema::hasColumn('sales_scenario_checklists', 'checked_at')) {
                $table->timestamp('checked_at')
                    ->nullable()
                    ->comment('체크 일시')
                    ->after('is_checked');
            }
        });

        Schema::table('sales_scenario_checklists', function (Blueprint $table) {
            // checked_by 컬럼 추가 (checked_at 다음에)
            if (!Schema::hasColumn('sales_scenario_checklists', 'checked_by')) {
                $table->unsignedBigInteger('checked_by')
                    ->nullable()
                    ->comment('체크한 사용자 ID')
                    ->after('checked_at');
            }
        });

        Schema::table('sales_scenario_checklists', function (Blueprint $table) {
            // memo 컬럼 추가 (checked_by 다음에)
            if (!Schema::hasColumn('sales_scenario_checklists', 'memo')) {
                $table->text('memo')
                    ->nullable()
                    ->comment('메모')
                    ->after('checked_by');
            }
        });

        // 인덱스 추가 (이미 존재하면 무시)
        // UNIQUE KEY 추가
        $uniqueExists = DB::select("SHOW INDEX FROM sales_scenario_checklists WHERE Key_name = 'sales_scenario_checkpoint_unique'");
        if (empty($uniqueExists)) {
            Schema::table('sales_scenario_checklists', function (Blueprint $table) {
                $table->unique(
                    ['tenant_id', 'scenario_type', 'step_id', 'checkpoint_id'],
                    'sales_scenario_checkpoint_unique'
                );
            });
        }

        // INDEX 추가
        $indexExists = DB::select("SHOW INDEX FROM sales_scenario_checklists WHERE Key_name = 'sales_scenario_checklists_tenant_id_scenario_type_index'");
        if (empty($indexExists)) {
            Schema::table('sales_scenario_checklists', function (Blueprint $table) {
                $table->index(
                    ['tenant_id', 'scenario_type'],
                    'sales_scenario_checklists_tenant_id_scenario_type_index'
                );
            });
        }
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('sales_scenario_checklists', function (Blueprint $table) {
            // 인덱스 삭제
            $table->dropIndex('sales_scenario_checklists_tenant_id_scenario_type_index');
            $table->dropUnique('sales_scenario_checkpoint_unique');

            // 컬럼 삭제
            $table->dropColumn([
                'scenario_type',
                'checkpoint_id',
                'checked_at',
                'checked_by',
                'memo',
            ]);
        });
    }
};
