package com.kamco.cd.kamcoback.postgres.entity;

import com.kamco.cd.kamcoback.log.dto.AuditLogDto;
import com.kamco.cd.kamcoback.log.dto.EventStatus;
import com.kamco.cd.kamcoback.log.dto.EventType;
import com.kamco.cd.kamcoback.postgres.CommonCreateEntity;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "tb_audit_log")
public class AuditLogEntity extends CommonCreateEntity {
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "audit_log_uid", nullable = false)
  private Long id;

  @Column(name = "user_uid")
  private Long userUid;

  @Enumerated(EnumType.STRING)
  private EventType eventType;

  @Enumerated(EnumType.STRING)
  private EventStatus eventStatus;

  @Column(name = "menu_uid")
  private String menuUid;

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

  @Column(name = "request_uri")
  private String requestUri;

  @Column(name = "request_body")
  private String requestBody;

  @Column(name = "error_log_uid")
  private Long errorLogUid;

  public AuditLogEntity(
      Long userUid,
      EventType eventType,
      EventStatus eventStatus,
      String menuUid,
      String ipAddress,
      String requestUri,
      String requestBody,
      Long errorLogUid) {
    this.userUid = userUid;
    this.eventType = eventType;
    this.eventStatus = eventStatus;
    this.menuUid = menuUid;
    this.ipAddress = ipAddress;
    this.requestUri = requestUri;
    this.requestBody = requestBody;
    this.errorLogUid = errorLogUid;
  }

  public AuditLogDto.Basic toDto() {
    return new AuditLogDto.Basic(
        this.id,
        this.userUid,
        this.eventType,
        this.eventStatus,
        this.menuUid,
        this.ipAddress,
        this.requestUri,
        this.requestBody,
        this.errorLogUid,
        super.getCreatedDate());
  }

  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append(this.id)
        .append("\n")
        .append(this.userUid)
        .append("\n")
        .append(this.eventType)
        .append("\n")
        .append(this.eventStatus)
        .append("\n")
        .append(this.menuUid)
        .append("\n")
        .append(this.ipAddress)
        .append("\n")
        .append(this.requestUri)
        .append("\n")
        .append(this.requestBody)
        .append("\n")
        .append(this.errorLogUid);
    return sb.toString();
  }
}
