package com.gcsc.connection.service.entity;

import com.gcsc.connection.common.entity.BaseEntity;
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 lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Getter
@NoArgsConstructor(access = lombok.AccessLevel.PROTECTED)
@Entity
@Table(name = "snp_service", schema = "common")
public class SnpService extends BaseEntity {

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

    @Column(name = "service_code", length = 50, nullable = false, unique = true)
    private String serviceCode;

    @Column(name = "service_name", length = 200, nullable = false)
    private String serviceName;

    @Column(name = "service_url", length = 500)
    private String serviceUrl;

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

    @Column(name = "health_check_url", length = 500)
    private String healthCheckUrl;

    @Column(name = "health_check_interval", nullable = false)
    private Integer healthCheckInterval = 30;

    @Enumerated(EnumType.STRING)
    @Column(name = "health_status", length = 10, nullable = false)
    private ServiceStatus healthStatus = ServiceStatus.UNKNOWN;

    @Column(name = "health_checked_at")
    private LocalDateTime healthCheckedAt;

    @Column(name = "health_response_time")
    private Integer healthResponseTime;

    @Column(name = "is_active", nullable = false)
    private Boolean isActive = true;

    @Builder
    public SnpService(String serviceCode, String serviceName, String serviceUrl,
                      String description, String healthCheckUrl, Integer healthCheckInterval,
                      ServiceStatus healthStatus, Boolean isActive) {
        this.serviceCode = serviceCode;
        this.serviceName = serviceName;
        this.serviceUrl = serviceUrl;
        this.description = description;
        this.healthCheckUrl = healthCheckUrl;
        this.healthCheckInterval = healthCheckInterval != null ? healthCheckInterval : 30;
        this.healthStatus = healthStatus != null ? healthStatus : ServiceStatus.UNKNOWN;
        this.isActive = isActive != null ? isActive : true;
    }

    public void update(String serviceName, String serviceUrl, String description,
                       String healthCheckUrl, Integer healthCheckInterval, Boolean isActive) {
        if (serviceName != null) this.serviceName = serviceName;
        if (serviceUrl != null) this.serviceUrl = serviceUrl;
        if (description != null) this.description = description;
        if (healthCheckUrl != null) this.healthCheckUrl = healthCheckUrl;
        if (healthCheckInterval != null) this.healthCheckInterval = healthCheckInterval;
        if (isActive != null) this.isActive = isActive;
    }

    public void updateHealthStatus(ServiceStatus status, Integer responseTime) {
        this.healthStatus = status;
        this.healthResponseTime = responseTime;
        this.healthCheckedAt = LocalDateTime.now();
    }
}
