<?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
    {
        Schema::create('calculation_configs', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('tenant_id')->comment('테넌트ID');
            $table->string('company_name', 100)->comment('업체명 (경동기업, 삼성물산 등)');
            $table->string('formula_type', 100)->comment('산출식 타입 (screen_weight, motor_capacity 등)');
            $table->string('version', 10)->default('v1.0')->comment('산출식 버전');
            $table->text('formula_expression')->comment('실제 계산식 표현식');
            $table->json('parameters')->comment('필요한 입력 파라미터 정의');
            $table->json('conditions')->nullable()->comment('적용 조건 (제품타입, 사이즈 범위 등)');
            $table->json('validation_rules')->nullable()->comment('파라미터 검증 규칙');
            $table->text('description')->nullable()->comment('산출식 설명');
            $table->boolean('is_active')->default(true)->comment('활성 상태');
            $table->timestamps();
            $table->unsignedBigInteger('created_by')->nullable();
            $table->unsignedBigInteger('updated_by')->nullable();

            $table->unique(['tenant_id', 'company_name', 'formula_type', 'version'], 'uq_calc_config_unique');
            $table->index(['tenant_id', 'company_name', 'is_active'], 'idx_calc_config_company');
            $table->index(['tenant_id', 'formula_type', 'is_active'], 'idx_calc_config_type');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('calculation_configs');
    }
};
