package com.microservices.mon.k8s.domain;

import com.alibaba.fastjson2.JSONObject;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;


@Data
@ApiModel("K8S应用环境变量")
public class K8sApplicationEnv {

    @ApiModelProperty(value = "环境变量类型")
    private String type;

    @ApiModelProperty(value = "环境变量键")
    private String key;

    @ApiModelProperty(value = "环境变量值")
    private JSONObject valueObj;

    /**
     * 将 Kubernetes 容器 env 数组中的单个元素（JSONObject 形式）转换成内部统一的 K8sApplicationEnv 对象。
     *
     * <p>支持解析以下四种 value 来源：</p>
     * <ul>
     *   <li>普通键值对：{@code {"name":"KEY","value":"VAL"}}</li>
     *   <li>资源字段引用：{@code {"name":"CPU_LIMIT","valueFrom":{"resourceFieldRef":{...}}}}</li>
     *   <li>ConfigMap 键引用：{@code {"name":"CM_KEY","valueFrom":{"configMapKeyRef":{...}}}}</li>
     *   <li>Secret 键引用：{@code {"name":"SECRET_KEY","valueFrom":{"secretKeyRef":{...}}}}</li>
     *   <li>Pod 字段引用：{@code {"name":"POD_IP","valueFrom":{"fieldRef":{...}}}}</li>
     * </ul>
     *
     * @param envJsonObject 单条 env 的原始 JSON 对象；允许为 null
     * @return 转换后的 K8sApplicationEnv 实例；无法识别或入参为 null 时返回 null
     */
    public static K8sApplicationEnv envJsonObjectToK8sApplicationEnv(JSONObject envJsonObject) {
        K8sApplicationEnv k8sApplicationEnv = null;
        if (envJsonObject != null) {
            JSONObject valueObj = new JSONObject();
            if (envJsonObject.containsKey("value")) {
                k8sApplicationEnv = new K8sApplicationEnv();
                k8sApplicationEnv.setKey(envJsonObject.getString("name"));
                k8sApplicationEnv.setType(ApplicationEnvType.KEY_VALUE_PAIR.getKey());
                valueObj.put("value", envJsonObject.getString("value"));
                k8sApplicationEnv.setValueObj(valueObj);
            } else if (envJsonObject.containsKey("valueFrom")) {
                JSONObject valueFromJson = envJsonObject.getJSONObject("valueFrom");
                if (valueFromJson.containsKey("resourceFieldRef")) {
                    k8sApplicationEnv = new K8sApplicationEnv();
                    k8sApplicationEnv.setKey(envJsonObject.getString("name"));
                    k8sApplicationEnv.setType(ApplicationEnvType.RESOURCE.getKey());
                    JSONObject resourceFieldRef = valueFromJson.getJSONObject("resourceFieldRef");
                    valueObj.put("name", resourceFieldRef.getString("containerName"));
                    valueObj.put("key", resourceFieldRef.getString("resource"));
                    k8sApplicationEnv.setValueObj(valueObj);
                } else if (valueFromJson.containsKey("configMapKeyRef")) {
                    k8sApplicationEnv = new K8sApplicationEnv();
                    k8sApplicationEnv.setKey(envJsonObject.getString("name"));
                    k8sApplicationEnv.setType(ApplicationEnvType.CONFIG_MAP_KEY.getKey());
                    JSONObject configMapKeyRef = valueFromJson.getJSONObject("configMapKeyRef");
                    valueObj.put("name", configMapKeyRef.getString("name"));
                    valueObj.put("key", configMapKeyRef.getString("key"));
                    k8sApplicationEnv.setValueObj(valueObj);
                } else if (valueFromJson.containsKey("secretKeyRef")) {
                    k8sApplicationEnv = new K8sApplicationEnv();
                    k8sApplicationEnv.setKey(envJsonObject.getString("name"));
                    k8sApplicationEnv.setType(ApplicationEnvType.SECRET_KEY.getKey());
                    JSONObject secretKeyRef = valueFromJson.getJSONObject("secretKeyRef");
                    valueObj.put("name", secretKeyRef.getString("name"));
                    valueObj.put("key", secretKeyRef.getString("key"));
                    k8sApplicationEnv.setValueObj(valueObj);
                } else if (valueFromJson.containsKey("fieldRef")) {
                    k8sApplicationEnv = new K8sApplicationEnv();
                    k8sApplicationEnv.setKey(envJsonObject.getString("name"));
                    k8sApplicationEnv.setType(ApplicationEnvType.POD_FIELD.getKey());
                    JSONObject fieldRef = valueFromJson.getJSONObject("fieldRef");
                    valueObj.put("value", fieldRef.getString("fieldPath"));
                    k8sApplicationEnv.setValueObj(valueObj);
                }
            }
        }
        return k8sApplicationEnv;
    }

    /**
     * 将 Kubernetes 容器 envFrom 数组中的单个元素（JSONObject 形式）转换成内部统一的 K8sApplicationEnv 对象。
     *
     * <p>支持解析以下两种来源：</p>
     * <ul>
     *   <li>整个 ConfigMap：{@code {"prefix":"PRE_","configMapRef":{"name":"cm-name"}}}</li>
     *   <li>整个 Secret：{@code {"prefix":"PRE_","secretRef":{"name":"secret-name"}}}</li>
     * </ul>
     *
     * <p>注意：prefix 可能为 null。</p>
     *
     * @param envFromJson 单条 envFrom 的原始 JSON 对象；允许为 null
     * @return 转换后的 K8sApplicationEnv 实例；无法识别或入参为 null 时返回 null
     */
    public static K8sApplicationEnv envFromJsonObjectToK8sApplicationEnv(JSONObject envFromJson) {
        K8sApplicationEnv k8sApplicationEnv = null;
        if (envFromJson != null) {
            JSONObject valueObj = new JSONObject();
            if (envFromJson.containsKey("configMapRef")) {
                k8sApplicationEnv = new K8sApplicationEnv();
                k8sApplicationEnv.setKey(envFromJson.getString("prefix"));
                k8sApplicationEnv.setType(ApplicationEnvType.CONFIGMAP.getKey());
                JSONObject configMapRef = envFromJson.getJSONObject("configMapRef");
                valueObj.put("name", configMapRef.getString("name"));
                k8sApplicationEnv.setValueObj(valueObj);
            } else if (envFromJson.containsKey("secretRef")) {
                k8sApplicationEnv = new K8sApplicationEnv();
                k8sApplicationEnv.setKey(envFromJson.getString("prefix"));
                k8sApplicationEnv.setType(ApplicationEnvType.SECRET.getKey());
                JSONObject secretRef = envFromJson.getJSONObject("secretRef");
                valueObj.put("name", secretRef.getString("name"));
                k8sApplicationEnv.setValueObj(valueObj);
            }
        }
        return k8sApplicationEnv;
    }
}
