package io.wdd.rpc.status;

import com.fasterxml.jackson.core.JsonProcessingException;
import io.wdd.rpc.message.OctopusMessage;
import io.wdd.rpc.message.OctopusMessageType;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

import java.time.LocalDateTime;

import static io.wdd.common.config.OctopusObjectMapperConfig.OctopusObjectMapper;

@Data
@AllArgsConstructor
@NoArgsConstructor
@SuperBuilder(toBuilder = true)
public class OctopusStatusMessage {

    // below two will be used by both server and agent
    // 存储所有Agent的实时健康状态， 1代表健康 0代表失败
    public static final String ALL_AGENT_STATUS_REDIS_KEY = "ALL_AGENT_HEALTHY_STATUS";
    public static final String HEALTHY_STATUS_MESSAGE_TYPE = "PING";
    public static final String ALL_STATUS_MESSAGE_TYPE = "ALL";
    public static final String METRIC_STATUS_MESSAGE_TYPE = "METRIC";
    public static final String APP_STATUS_MESSAGE_TYPE = "APP";

    /**
     * which kind of status should be return
     * metric => short time message
     * all => all agent status message
     * healthy => check for healthy
     */
    String statusType;


    public static OctopusMessage ConstructAgentStatusMessage(String statusType, String agentTopicName, LocalDateTime currentTime) {

        OctopusStatusMessage statusMessage = OctopusStatusMessage
                .builder()
                .statusType(statusType)
                .build();

        String ops;
        try {
            ops = OctopusObjectMapper.writeValueAsString(statusMessage);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }

        return OctopusMessage
                .builder()
                .octopusMessageType(OctopusMessageType.STATUS)
                .uuid(agentTopicName)
                .init_time(currentTime)
                .content(ops)
                .build();

    }

}
