package com.ttstd.signaling.model;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Index;
import jakarta.persistence.Table;

import java.time.Instant;

/**
 * 安全审计日志（持久化到数据库）。
 *
 * <p>记录登录、激活、封禁、踢线等安全敏感操作，用于事后追溯。
 */
@Entity
@Table(name = "audit_log", indexes = {
        @Index(name = "idx_audit_actor", columnList = "actor_id"),
        @Index(name = "idx_audit_action", columnList = "action"),
        @Index(name = "idx_audit_created", columnList = "created_at")
})
public class AuditLog {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    /** 主体类型：USER / DEVICE / ADMIN */
    @Column(name = "actor_type", length = 16)
    private String actorType;

    @Column(name = "actor_id", length = 64)
    private String actorId;

    @Column(name = "action", length = 48, nullable = false)
    private String action;

    @Column(name = "target_id", length = 64)
    private String targetId;

    /** SUCCESS / FAILURE */
    @Column(name = "result", length = 16)
    private String result;

    @Column(name = "ip", length = 64)
    private String ip;

    @Column(name = "detail", length = 512)
    private String detail;

    @Column(name = "created_at", nullable = false)
    private Instant createdAt;

    protected AuditLog() {
        this.createdAt = Instant.now();
    }

    public AuditLog(String actorType, String actorId, String action,
                    String targetId, String result, String ip, String detail) {
        this.actorType = actorType;
        this.actorId = actorId;
        this.action = action;
        this.targetId = targetId;
        this.result = result;
        this.ip = ip;
        this.detail = detail;
        this.createdAt = Instant.now();
    }

    public Long getId() { return id; }
    public String getActorType() { return actorType; }
    public String getActorId() { return actorId; }
    public String getAction() { return action; }
    public String getTargetId() { return targetId; }
    public String getResult() { return result; }
    public String getIp() { return ip; }
    public String getDetail() { return detail; }
    public Instant getCreatedAt() { return createdAt; }
}
