<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     * price_histories 테이블 삭제 (prices로 완전 이관 완료 후)
     */
    public function up(): void
    {
        Schema::dropIfExists('price_histories');
    }

    /**
     * Reverse the migrations.
     * 롤백 시 price_histories 테이블 재생성
     */
    public function down(): void
    {
        Schema::create('price_histories', 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->string('price_type_code', 20)->comment('가격유형 (SALE/PURCHASE)');
            $table->unsignedBigInteger('client_group_id')->nullable()->comment('고객 그룹 ID');
            $table->decimal('price', 15, 4)->comment('단가');
            $table->date('started_at')->comment('적용 시작일');
            $table->date('ended_at')->nullable()->comment('적용 종료일');
            $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', 'item_type_code', 'item_id', 'client_group_id', 'started_at'], 'idx_price_histories_main');
            $table->foreign('client_group_id')->references('id')->on('client_groups')->onDelete('cascade');
        });
    }
};
