package com.eactive.eai.rms.data.entity.onl.djb.webhook;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

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

@Entity
@Table(name = "PTL_WEBHOOK_SEND_LOG")
@Getter
@Setter
@NoArgsConstructor
@EntityListeners(AuditingEntityListener.class)
public class WebhookSendLog {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "webhook_send_log_seq")
    @SequenceGenerator(
            name      = "webhook_send_log_seq",
            sequenceName = "SEQ_PTL_WEBHOOK_SEND_LOG",
            allocationSize = 1
    )
    private Long id;

    @Column(name = "TARGET_URL", length = 500)
    private String targetUrl;

    @Column(name = "PROXY_URL", length = 500)
    private String proxyUrl;

    @Column(name = "EVENT_TYPE", length = 100)
    private String eventType;

    @Lob
    @Column(name = "PAYLOAD")
    private String payload;

    @Column(name = "SIGNATURE", length = 500)
    private String signature;

    @Column(name = "STATUS_CODE")
    private Integer statusCode;

    @Column(name = "ORG_ID")
    private String orgId;

    @Lob
    @Column(name = "RESPONSE_BODY")
    private String responseBody;

    @Column(name = "SUCCESS", length = 1)
    private String success;          // 오라클 CHAR(1) : 'Y' / 'N'

    @Lob
    @Column(name = "ERROR_MESSAGE")
    private String errorMessage;

    @Column(name = "RETRY_COUNT")
    private Integer retryCount = 0;

    @CreatedDate
    @Column(name = "CREATED_AT", updatable = false)
    private LocalDateTime createdAt;

    @Column(name = "SENT_AT")
    private LocalDateTime sentAt;

    @Column(name = "SEND_BY", length = 100)
    private String sendBy;

    /* Boolean 편의 메서드 */
    public void setSuccess(Boolean success) {
        this.success = (success != null && success) ? "Y" : "N";
    }

    public Boolean getSuccess() {
        return "Y".equalsIgnoreCase(this.success);
    }
}