package com.erp.systemtool.entity;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

/**
 * Audit Log Entity
 * 
 * Records sensitive operations for compliance and security auditing.
 * Migrations from PHP AuditLog model.
 */
@Data
@Entity
@Table(name = "audit_logs", indexes = {
    @Index(name = "idx_module_action", columnList = "module, action"),
    @Index(name = "idx_user_id", columnList = "user_id"),
    @Index(name = "idx_target", columnList = "target_type, target_id"),
    @Index(name = "idx_created_at", columnList = "created_at")
})
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class AuditLog {

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

    @Column(name = "tenant_id")
    private Integer tenantId;

    @Column(name = "user_id")
    private Integer userId;

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

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

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

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

    @Column(name = "target_id")
    private Long targetId;

    @Column(name = "old_value", columnDefinition = "TEXT")
    private String oldValue;

    @Column(name = "new_value", columnDefinition = "TEXT")
    private String newValue;

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

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

    @Column(name = "result", length = 32)
    private String result;

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

    @Column(name = "created_at")
    private LocalDateTime createdAt;

    @PrePersist
    protected void onCreate() {
        createdAt = LocalDateTime.now();
    }
}
