package cokr.xit.ens.core.aop;

import cokr.xit.ens.core.aop.code.AccessStatusCd;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Builder;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import javax.persistence.*;
import java.time.LocalDateTime;

@Entity
@Getter
@RequiredArgsConstructor
@Table(name = "ens_access_log", schema = "", catalog = "")
public class AccessLog {


	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long logId;
	
	@Column(length = 2000)
	private String accessToken;
	
	private String reqSystemId;
	
	private String sessionId;
	                              
	private String ip;
	
	private String httpMethod;
	
	private String url;
	                              
	private String uri;
	                              
	@Lob                             
	private String param;
	@Lob                           
	private String response;
	
	@Enumerated(EnumType.STRING)
	private AccessStatusCd status;
	
	@Lob
	private String errorMsg;
	
	@CreationTimestamp             
	private LocalDateTime startDt;
	
	@UpdateTimestamp               
	private LocalDateTime endDt;
	
	

	@Builder(builderClassName = "reqBuilder", builderMethodName = "reqBuilder")
	public AccessLog(String accessToken, String sessionId, String ip, String httpMethod, String url, String uri, String param) {
		this.accessToken = accessToken;
		this.sessionId = sessionId;
		this.ip = ip;
		this.httpMethod = httpMethod;
		this.url = url;
		this.uri = uri;
		this.param = param;
		this.status = AccessStatusCd.req;
	}

	public void setResponseCompleted(String response) {
		this.status = AccessStatusCd.cmplt;
		this.response = response;
	}
	public void setResponseFail(String errorMsg) {
		this.status = AccessStatusCd.fail;
		this.errorMsg = errorMsg;
	}
	public void setResponseNoAuth(String errorMsg) {
		this.status = AccessStatusCd.noAuth;
		this.errorMsg = errorMsg;
	}
	
}
