<?php

/**
 * 活动日志表迁移
 *
 * 依据《网络安全法》第二十一条，日志留存不少于 180 天。
 * 依据等保 2.0 (GB/T 22239)，审计记录不可删除/修改。
 * 依据《个人信息保护法》第二十八条，禁止记录敏感个人信息明文。
 */

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('activity_log', function (Blueprint $table) {
            $table->id();
            $table->string('log_name', 100)->nullable()->index()->comment('日志分类名称');
            $table->text('description')->comment('日志描述');
            $table->nullableMorphs('subject', 'activity_log_subject_index');
            $table->nullableMorphs('causer', 'activity_log_causer_index');
            $table->string('event', 50)->nullable()->index()->comment('触发事件: created/updated/deleted');
            $table->uuid('batch_uuid')->nullable()->index()->comment('批次 UUID');
            $table->json('properties')->nullable()->comment('属性快照（JSON）');
            $table->unsignedBigInteger('tenant_id')->nullable()->index()->comment('租户 ID（多租户隔离）');
            $table->timestamps();

            $table->index(['log_name', 'created_at']);
            $table->index(['subject_type', 'subject_id', 'created_at']);
            $table->index(['causer_type', 'causer_id', 'created_at']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('activity_log');
    }
};
