<?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('sop_template_versions', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('sop_template_id')->comment('模板ID');
            $table->string('version', 50)->comment('版本号');
            $table->text('change_log')->nullable()->comment('变更说明');
            $table->json('content_snapshot')->nullable()->comment('内容快照');
            $table->unsignedBigInteger('created_by')->nullable()->comment('创建人');
            $table->timestamp('created_at')->nullable();

            // 添加外键约束
            $table->foreign('sop_template_id')
                  ->references('id')
                  ->on('sop_templates')
                  ->onDelete('cascade');

            // 添加索引
            $table->index(['sop_template_id', 'version'], 'idx_template_version');
        });
    }

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