<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        // 상품 카테고리 테이블
        Schema::create('sales_product_categories', function (Blueprint $table) {
            $table->id();
            $table->string('code', 50)->unique()->comment('카테고리 코드 (manufacturing, construction)');
            $table->string('name', 100)->comment('카테고리명 (제조 업체, 공사 업체)');
            $table->text('description')->nullable()->comment('설명');
            $table->string('base_storage', 20)->default('100GB')->comment('기본 제공 용량');
            $table->integer('display_order')->default(0)->comment('표시 순서');
            $table->boolean('is_active')->default(true);
            $table->timestamps();
            $table->softDeletes();
        });

        // 상품 테이블
        Schema::create('sales_products', function (Blueprint $table) {
            $table->id();
            $table->foreignId('category_id')->constrained('sales_product_categories')->comment('카테고리 FK');
            $table->string('code', 50)->comment('상품 코드');
            $table->string('name', 100)->comment('상품명');
            $table->text('description')->nullable()->comment('프로그램 타입/설명');
            $table->decimal('development_fee', 15, 2)->default(0)->comment('개발비 (가입비)');
            $table->decimal('subscription_fee', 15, 2)->default(0)->comment('월 구독료');
            $table->decimal('commission_rate', 5, 2)->default(25.00)->comment('수당 비율 (%)');
            $table->boolean('allow_flexible_pricing')->default(true)->comment('재량권 허용');
            $table->boolean('is_required')->default(false)->comment('필수 선택 여부');
            $table->integer('display_order')->default(0)->comment('표시 순서');
            $table->boolean('is_active')->default(true);
            $table->timestamps();
            $table->softDeletes();

            $table->unique(['category_id', 'code'], 'uk_category_code');
        });

        // 계약별 선택 상품 테이블
        Schema::create('sales_contract_products', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained('tenants')->comment('테넌트 FK');
            $table->foreignId('management_id')->constrained('sales_tenant_managements')->comment('영업관리 FK');
            $table->foreignId('category_id')->constrained('sales_product_categories')->comment('선택한 카테고리');
            $table->foreignId('product_id')->constrained('sales_products')->comment('선택한 상품');
            $table->decimal('development_fee', 15, 2)->nullable()->comment('적용된 개발비 (협상 가능)');
            $table->decimal('subscription_fee', 15, 2)->nullable()->comment('적용된 구독료');
            $table->decimal('discount_rate', 5, 2)->default(0)->comment('할인율 (%)');
            $table->text('notes')->nullable()->comment('비고');
            $table->foreignId('created_by')->nullable()->constrained('users')->comment('등록자');
            $table->timestamps();

            $table->index(['tenant_id', 'category_id']);
        });
    }

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