<?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
    {
        // SAM 모듈 카탈로그
        Schema::create('ai_quotation_modules', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('tenant_id');
            $table->string('module_code', 50);
            $table->string('module_name', 100);
            $table->enum('category', ['basic', 'individual', 'addon']);
            $table->text('description')->nullable();
            $table->json('keywords')->nullable();
            $table->decimal('dev_cost', 12, 0)->default(0);
            $table->decimal('monthly_fee', 10, 0)->default(0);
            $table->boolean('is_active')->default(true);
            $table->integer('sort_order')->default(0);
            $table->json('options')->nullable();
            $table->timestamps();

            $table->unique(['tenant_id', 'module_code'], 'uk_tenant_module');
            $table->index('tenant_id');
            $table->index('category');
        });

        // AI 견적 요청/결과
        Schema::create('ai_quotations', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('tenant_id');
            $table->string('title', 200);
            $table->enum('input_type', ['text', 'voice', 'document'])->default('text');
            $table->longText('input_text')->nullable();
            $table->string('input_file_path', 500)->nullable();
            $table->string('ai_provider', 20)->default('gemini');
            $table->string('ai_model', 50)->nullable();
            $table->json('analysis_result')->nullable();
            $table->json('quotation_result')->nullable();
            $table->string('status', 20)->default('pending');
            $table->unsignedBigInteger('linked_quote_id')->nullable();
            $table->decimal('total_dev_cost', 12, 0)->default(0);
            $table->decimal('total_monthly_fee', 10, 0)->default(0);
            $table->unsignedBigInteger('created_by')->nullable();
            $table->json('options')->nullable();
            $table->timestamps();
            $table->softDeletes();

            $table->index('tenant_id');
            $table->index('status');
            $table->index('created_by');
        });

        // AI 추천 모듈 목록
        Schema::create('ai_quotation_items', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('ai_quotation_id');
            $table->unsignedBigInteger('module_id')->nullable();
            $table->string('module_code', 50);
            $table->string('module_name', 100);
            $table->boolean('is_required')->default(false);
            $table->text('reason')->nullable();
            $table->decimal('dev_cost', 12, 0)->default(0);
            $table->decimal('monthly_fee', 10, 0)->default(0);
            $table->integer('sort_order')->default(0);
            $table->json('options')->nullable();
            $table->timestamps();

            $table->foreign('ai_quotation_id')
                ->references('id')
                ->on('ai_quotations')
                ->cascadeOnDelete();

            $table->index('ai_quotation_id');
            $table->index('module_id');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('ai_quotation_items');
        Schema::dropIfExists('ai_quotations');
        Schema::dropIfExists('ai_quotation_modules');
    }
};
