package com.gcsc.connection.monitoring.entity;

import com.gcsc.connection.service.entity.SnpService;
import jakarta.persistence.Column;
import jakarta.persistence.ConstraintMode;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.ForeignKey;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Getter
@NoArgsConstructor(access = lombok.AccessLevel.PROTECTED)
@Entity
@Table(name = "snp_service_health_log", schema = "common")
public class SnpServiceHealthLog {

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

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "service_id", nullable = false,
        foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
    private SnpService service;

    @Column(name = "previous_status", length = 10)
    private String previousStatus;

    @Column(name = "current_status", length = 10, nullable = false)
    private String currentStatus;

    @Column(name = "response_time")
    private Integer responseTime;

    @Column(name = "error_message", columnDefinition = "TEXT")
    private String errorMessage;

    @Column(name = "checked_at", nullable = false)
    private LocalDateTime checkedAt;

    @Builder
    public SnpServiceHealthLog(SnpService service, String previousStatus, String currentStatus,
                               Integer responseTime, String errorMessage, LocalDateTime checkedAt) {
        this.service = service;
        this.previousStatus = previousStatus;
        this.currentStatus = currentStatus;
        this.responseTime = responseTime;
        this.errorMessage = errorMessage;
        this.checkedAt = checkedAt != null ? checkedAt : LocalDateTime.now();
    }
}
