<?php

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

return new class extends Migration
{
    public function up(): void
    {
        // interview_questions 확장
        Schema::table('interview_questions', function (Blueprint $table) {
            $table->text('ai_hint')->nullable()->after('options')->comment('AI 분석 힌트/가이드');
            $table->string('expected_format', 100)->nullable()->after('ai_hint')->comment('예상 답변 형식 (mm, 원/kg 등)');
            $table->json('depends_on')->nullable()->after('expected_format')->comment('조건부 표시 조건');
            $table->string('domain', 50)->nullable()->after('depends_on')->comment('도메인 영역');
        });

        // question_type 컬럼 확장 (varchar(20) → varchar(50))
        Schema::table('interview_questions', function (Blueprint $table) {
            $table->string('question_type', 50)->default('checkbox')
                ->comment('질문유형: checkbox|text|number|select|multi_select|file_upload|formula_input|table_input|bom_tree|price_table|dimension_diagram')
                ->change();
        });

        // interview_answers 확장
        Schema::table('interview_answers', function (Blueprint $table) {
            $table->json('answer_data')->nullable()->after('answer_text')->comment('구조화 답변 (테이블, 수식, BOM 등)');
            $table->json('attachments')->nullable()->after('answer_data')->comment('첨부 파일 경로 목록');
        });

        // interview_sessions 확장
        Schema::table('interview_sessions', function (Blueprint $table) {
            $table->unsignedBigInteger('interview_project_id')->nullable()
                ->after('tenant_id')->comment('프로젝트 ID');
            $table->string('session_type', 20)->default('checklist')
                ->after('status')->comment('세션 유형: checklist|structured|voice|review');
            $table->unsignedBigInteger('voice_recording_id')->nullable()
                ->after('session_type')->comment('음성 녹음 ID');

            $table->index('interview_project_id', 'idx_interview_sessions_project');
        });
    }

    public function down(): void
    {
        Schema::table('interview_sessions', function (Blueprint $table) {
            $table->dropIndex('idx_interview_sessions_project');
            $table->dropColumn(['interview_project_id', 'session_type', 'voice_recording_id']);
        });

        Schema::table('interview_answers', function (Blueprint $table) {
            $table->dropColumn(['answer_data', 'attachments']);
        });

        Schema::table('interview_questions', function (Blueprint $table) {
            $table->string('question_type', 20)->default('checkbox')
                ->comment('질문유형: checkbox/text')->change();
            $table->dropColumn(['ai_hint', 'expected_format', 'depends_on', 'domain']);
        });
    }
};
