package com.hdkj.lt.modle.dto.recon;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;

/**
 * 运行指标 (metrics).
 * metrics_before / metrics_after 同构, 展开为实体扁平列;
 * feeder_load_rates_pct (动态键) 及 delta / judgement 整块用 JsonNode 承载, 入库为 JSON 列.
 */
@Data
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class MetricsDTO {

    @JsonProperty("metrics_before")
    private MetricsBeforeAfterDTO metricsBefore;

    @JsonProperty("metrics_after")
    private MetricsBeforeAfterDTO metricsAfter;

    @JsonProperty("delta")
    private JsonNode delta;

    @JsonProperty("judgement")
    private JsonNode judgement;

    @Data
    @NoArgsConstructor
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class MetricsBeforeAfterDTO {

        @JsonProperty("loading")
        private LoadingDTO loading;

        @JsonProperty("loss")
        private LossDTO loss;

        @JsonProperty("voltage")
        private VoltageDTO voltage;

        @JsonProperty("reliability")
        private ReliabilityDTO reliability;

        @JsonProperty("health")
        private HealthDTO health;
    }

    @Data
    @NoArgsConstructor
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class LoadingDTO {

        @JsonProperty("line_count")
        private Integer lineCount;

        @JsonProperty("ext_grid_count")
        private Integer extGridCount;

        @JsonProperty("max_loading_pct")
        private BigDecimal maxLoadingPct;

        @JsonProperty("avg_loading_pct")
        private BigDecimal avgLoadingPct;

        @JsonProperty("over_limit_count")
        private Integer overLimitCount;

        @JsonProperty("loading_limit_pct")
        private BigDecimal loadingLimitPct;

        private String unit;

        @JsonProperty("loading_source")
        private String loadingSource;

        /** 动态键: 键为馈线ID, 值为负载率 */
        @JsonProperty("feeder_load_rates_pct")
        private JsonNode feederLoadRatesPct;

        @JsonProperty("line_max_loading_pct")
        private BigDecimal lineMaxLoadingPct;

        @JsonProperty("line_avg_loading_pct")
        private BigDecimal lineAvgLoadingPct;

        @JsonProperty("line_over_limit_count")
        private Integer lineOverLimitCount;
    }

    @Data
    @NoArgsConstructor
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class LossDTO {

        @JsonProperty("line_loss_mw")
        private BigDecimal lineLossMw;

        @JsonProperty("trafo_loss_mw")
        private BigDecimal trafoLossMw;

        @JsonProperty("total_loss_mw")
        private BigDecimal totalLossMw;

        private String unit;
    }

    @Data
    @NoArgsConstructor
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class VoltageDTO {

        @JsonProperty("min_vm_pu")
        private BigDecimal minVmPu;

        @JsonProperty("max_vm_pu")
        private BigDecimal maxVmPu;

        @JsonProperty("avg_vm_pu")
        private BigDecimal avgVmPu;

        @JsonProperty("under_vmin_count")
        private Integer underVminCount;

        @JsonProperty("over_vmax_count")
        private Integer overVmaxCount;

        @JsonProperty("vmin_pu")
        private BigDecimal vminPu;

        @JsonProperty("vmax_pu")
        private BigDecimal vmaxPu;

        @JsonProperty("bus_count")
        private Integer busCount;

        private String unit;
    }

    @Data
    @NoArgsConstructor
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class ReliabilityDTO {

        private Boolean enabled;

        @JsonProperty("_comment")
        private String comment;
    }

    @Data
    @NoArgsConstructor
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class HealthDTO {

        private BigDecimal score;

        private BigDecimal base;

        private BigDecimal penalty;

        private ScoreItemsDTO weights;

        private ScoreItemsDTO components;
    }
}