<?php

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('operation_logs', function (Blueprint $table) {
            $table->id();
            $table->foreignId('store_id')->constrained();
            $table->unsignedBigInteger('user_id');
            $table->string('user_name', 50);
            $table->string('module', 50)->comment('操作模块');
            $table->string('action', 50)->comment('操作类型 create/update/delete');
            $table->string('target', 100)->nullable()->comment('操作对象');
            $table->unsignedBigInteger('target_id')->nullable();
            $table->string('ip', 45)->nullable();
            $table->string('method', 10)->nullable()->comment('HTTP方法');
            $table->string('url', 500)->nullable()->comment('请求URL');
            $table->json('before')->nullable()->comment('变更前数据');
            $table->json('after')->nullable()->comment('变更后数据');
            $table->timestamp('created_at')->nullable();

            $table->index(['store_id', 'created_at']);
            $table->index('user_id');
            $table->index('module');
        });
    }

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