# Generated by Django 5.2.10 on 2026-03-04 05:55

import uuid

import django.core.serializers.json
from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ("audit", "0005_attributionreport_attribution_method"),
    ]

    operations = [
        migrations.CreateModel(
            name="OperationLogModel",
            fields=[
                (
                    "id",
                    models.UUIDField(
                        default=uuid.uuid4,
                        editable=False,
                        primary_key=True,
                        serialize=False,
                        verbose_name="日志ID",
                    ),
                ),
                (
                    "request_id",
                    models.CharField(
                        db_index=True,
                        help_text="用于关联请求链路",
                        max_length=64,
                        verbose_name="链路追踪ID",
                    ),
                ),
                ("user_id", models.IntegerField(db_index=True, null=True, verbose_name="用户ID")),
                (
                    "username",
                    models.CharField(default="anonymous", max_length=150, verbose_name="用户名"),
                ),
                (
                    "ip_address",
                    models.GenericIPAddressField(blank=True, null=True, verbose_name="IP地址"),
                ),
                (
                    "user_agent",
                    models.CharField(blank=True, max_length=500, verbose_name="User Agent"),
                ),
                (
                    "source",
                    models.CharField(
                        db_index=True,
                        default="MCP",
                        help_text="MCP/SDK/API",
                        max_length=20,
                        verbose_name="来源",
                    ),
                ),
                (
                    "client_id",
                    models.CharField(
                        blank=True, db_index=True, max_length=100, verbose_name="客户端ID"
                    ),
                ),
                (
                    "operation_type",
                    models.CharField(
                        db_index=True,
                        help_text="MCP_CALL/API_ACCESS/DATA_MODIFY",
                        max_length=50,
                        verbose_name="操作类型",
                    ),
                ),
                (
                    "module",
                    models.CharField(
                        db_index=True,
                        help_text="signal/policy/backtest/...",
                        max_length=50,
                        verbose_name="模块",
                    ),
                ),
                (
                    "action",
                    models.CharField(
                        help_text="CREATE/READ/UPDATE/DELETE/EXECUTE",
                        max_length=50,
                        verbose_name="动作",
                    ),
                ),
                (
                    "resource_type",
                    models.CharField(blank=True, max_length=50, verbose_name="资源类型"),
                ),
                (
                    "resource_id",
                    models.CharField(
                        blank=True, db_index=True, max_length=100, null=True, verbose_name="资源ID"
                    ),
                ),
                (
                    "mcp_tool_name",
                    models.CharField(
                        blank=True,
                        db_index=True,
                        max_length=120,
                        null=True,
                        verbose_name="MCP工具名",
                    ),
                ),
                (
                    "mcp_client_id",
                    models.CharField(blank=True, max_length=100, verbose_name="MCP客户端ID"),
                ),
                ("mcp_role", models.CharField(blank=True, max_length=30, verbose_name="MCP角色")),
                (
                    "sdk_version",
                    models.CharField(blank=True, max_length=50, verbose_name="SDK版本"),
                ),
                (
                    "request_method",
                    models.CharField(default="MCP", max_length=10, verbose_name="请求方法"),
                ),
                (
                    "request_path",
                    models.CharField(blank=True, max_length=500, verbose_name="请求路径"),
                ),
                (
                    "request_params",
                    models.JSONField(
                        default=dict,
                        encoder=django.core.serializers.json.DjangoJSONEncoder,
                        help_text="已脱敏",
                        verbose_name="请求参数",
                    ),
                ),
                (
                    "response_status",
                    models.IntegerField(db_index=True, default=200, verbose_name="响应状态码"),
                ),
                ("response_message", models.TextField(blank=True, verbose_name="响应消息")),
                (
                    "error_code",
                    models.CharField(blank=True, max_length=50, verbose_name="错误代码"),
                ),
                (
                    "timestamp",
                    models.DateTimeField(auto_now_add=True, db_index=True, verbose_name="时间戳"),
                ),
                (
                    "duration_ms",
                    models.IntegerField(blank=True, null=True, verbose_name="耗时(ms)"),
                ),
                (
                    "checksum",
                    models.CharField(
                        blank=True, help_text="SHA-256", max_length=64, verbose_name="校验和"
                    ),
                ),
            ],
            options={
                "verbose_name": "操作审计日志",
                "verbose_name_plural": "操作审计日志",
                "db_table": "audit_operation_log",
                "ordering": ["-timestamp"],
                "indexes": [
                    models.Index(fields=["user_id", "-timestamp"], name="idx_audit_user_ts"),
                    models.Index(fields=["operation_type", "module"], name="idx_audit_type_module"),
                    models.Index(fields=["mcp_tool_name", "-timestamp"], name="idx_audit_tool_ts"),
                    models.Index(
                        fields=["response_status", "-timestamp"], name="idx_audit_status_ts"
                    ),
                    models.Index(fields=["source", "-timestamp"], name="idx_audit_source_ts"),
                ],
            },
        ),
    ]
