<?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('prices', function (Blueprint $table) {
            $table->id()->comment('ID');
            $table->foreignId('tenant_id')->comment('테넌트 ID');

            // 품목 연결
            $table->string('item_type_code', 20)->comment('품목유형 (PRODUCT/MATERIAL)');
            $table->unsignedBigInteger('item_id')->comment('품목 ID');
            $table->unsignedBigInteger('client_group_id')->nullable()->comment('고객그룹 ID (NULL=기본가)');

            // 원가 정보
            $table->decimal('purchase_price', 15, 4)->nullable()->comment('매입단가 (표준원가)');
            $table->decimal('processing_cost', 15, 4)->nullable()->comment('가공비');
            $table->decimal('loss_rate', 5, 2)->nullable()->comment('LOSS율 (%)');

            // 판매가 정보
            $table->decimal('margin_rate', 5, 2)->nullable()->comment('마진율 (%)');
            $table->decimal('sales_price', 15, 4)->nullable()->comment('판매단가');
            $table->enum('rounding_rule', ['round', 'ceil', 'floor'])->default('round')->comment('반올림 규칙');
            $table->integer('rounding_unit')->default(1)->comment('반올림 단위 (1,10,100,1000)');

            // 메타 정보
            $table->string('supplier', 255)->nullable()->comment('공급업체');
            $table->date('effective_from')->comment('적용 시작일');
            $table->date('effective_to')->nullable()->comment('적용 종료일');
            $table->text('note')->nullable()->comment('비고');

            // 상태 관리
            $table->enum('status', ['draft', 'active', 'inactive', 'finalized'])->default('draft')->comment('상태');
            $table->boolean('is_final')->default(false)->comment('최종 확정 여부');
            $table->dateTime('finalized_at')->nullable()->comment('확정 일시');
            $table->unsignedBigInteger('finalized_by')->nullable()->comment('확정자 ID');

            // 감사 컬럼
            $table->foreignId('created_by')->nullable()->comment('생성자 ID');
            $table->foreignId('updated_by')->nullable()->comment('수정자 ID');
            $table->foreignId('deleted_by')->nullable()->comment('삭제자 ID');
            $table->timestamps();
            $table->softDeletes();

            // 인덱스
            $table->index('tenant_id', 'idx_prices_tenant');
            $table->index(['tenant_id', 'item_type_code', 'item_id'], 'idx_prices_item');
            $table->index(['tenant_id', 'effective_from', 'effective_to'], 'idx_prices_effective');
            $table->index(['tenant_id', 'status'], 'idx_prices_status');
            $table->unique(
                ['tenant_id', 'item_type_code', 'item_id', 'client_group_id', 'effective_from', 'deleted_at'],
                'idx_prices_unique'
            );

            // Foreign Key
            $table->foreign('client_group_id')->references('id')->on('client_groups')->onDelete('set null');
        });
    }

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