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

            // 리비전 정보
            $table->integer('revision_number')->comment('리비전 번호');
            $table->dateTime('changed_at')->comment('변경 일시');
            $table->unsignedBigInteger('changed_by')->comment('변경자 ID');
            $table->string('change_reason', 500)->nullable()->comment('변경 사유');

            // 변경 스냅샷 (JSON)
            $table->json('before_snapshot')->nullable()->comment('변경 전 데이터');
            $table->json('after_snapshot')->comment('변경 후 데이터');

            $table->timestamp('created_at')->useCurrent();

            // 인덱스
            $table->index('price_id', 'idx_revisions_price');
            $table->index('tenant_id', 'idx_revisions_tenant');
            $table->unique(['price_id', 'revision_number'], 'idx_revisions_unique');

            // Foreign Key
            $table->foreign('price_id')->references('id')->on('prices')->onDelete('cascade');
        });
    }

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