package cloud.nstart.terravault.ordinis.domain.audit;

import cloud.nstart.terravault.ordinis.domain.DataScope;
import com.fasterxml.jackson.databind.JsonNode;
import io.hypersistence.utils.hibernate.type.json.JsonType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import org.hibernate.annotations.Type;

import java.time.OffsetDateTime;
import java.util.UUID;

/**
 * Юридически значимый audit log. Source of truth для compliance, отдельно от
 * технических логов в Loki. Retention — по регламенту ЦУОД.
 */
@Entity
@Table(name = "audit_log")
public class AuditLog {

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

  @Column(name = "event_time", nullable = false)
  private OffsetDateTime eventTime;

  @Column(name = "event_type", nullable = false, length = 64)
  private String eventType;

  @Column(name = "user_id", length = 128)
  private String userId;

  @Enumerated(EnumType.STRING)
  @Column(name = "user_scope", length = 32)
  private DataScope userScope;

  @Column(name = "dictionary_id")
  private UUID dictionaryId;

  @Column(name = "dictionary_name", length = 128)
  private String dictionaryName;

  @Column(name = "record_id")
  private UUID recordId;

  @Column(name = "business_key", length = 256)
  private String businessKey;

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

  @Type(JsonType.class)
  @Column(name = "payload_before", columnDefinition = "jsonb")
  private JsonNode payloadBefore;

  @Type(JsonType.class)
  @Column(name = "payload_after", columnDefinition = "jsonb")
  private JsonNode payloadAfter;

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

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

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

  @Column(name = "user_agent", columnDefinition = "TEXT")
  private String userAgent;

  public AuditLog() {}

  public Long getId() { return id; }
  public OffsetDateTime getEventTime() { return eventTime; }
  public String getEventType() { return eventType; }
  public String getUserId() { return userId; }
  public DataScope getUserScope() { return userScope; }
  public UUID getDictionaryId() { return dictionaryId; }
  public String getDictionaryName() { return dictionaryName; }
  public UUID getRecordId() { return recordId; }
  public String getBusinessKey() { return businessKey; }
  public String getAction() { return action; }
  public JsonNode getPayloadBefore() { return payloadBefore; }
  public JsonNode getPayloadAfter() { return payloadAfter; }
  public String getTraceId() { return traceId; }
  public String getRequestId() { return requestId; }
  public String getIpAddress() { return ipAddress; }
  public String getUserAgent() { return userAgent; }

  public void setEventTime(OffsetDateTime v) { this.eventTime = v; }
  public void setEventType(String v) { this.eventType = v; }
  public void setUserId(String v) { this.userId = v; }
  public void setUserScope(DataScope v) { this.userScope = v; }
  public void setDictionaryId(UUID v) { this.dictionaryId = v; }
  public void setDictionaryName(String v) { this.dictionaryName = v; }
  public void setRecordId(UUID v) { this.recordId = v; }
  public void setBusinessKey(String v) { this.businessKey = v; }
  public void setAction(String v) { this.action = v; }
  public void setPayloadBefore(JsonNode v) { this.payloadBefore = v; }
  public void setPayloadAfter(JsonNode v) { this.payloadAfter = v; }
  public void setTraceId(String v) { this.traceId = v; }
  public void setRequestId(String v) { this.requestId = v; }
  public void setIpAddress(String v) { this.ipAddress = v; }
  public void setUserAgent(String v) { this.userAgent = v; }
}
