package incheon.sgp.common.service;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;

/**
 * 기상청 초단기예보 API Response DTO
 */
@JsonIgnoreProperties(ignoreUnknown = true)
public class KmaUltraSrtResponse {

    @JsonProperty("response")
    private Response response;

    public Response getResponse() { return response; }
    public void setResponse(Response response) { this.response = response; }

    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class Response {
        @JsonProperty("header") private Header header;
        @JsonProperty("body") private Body body;

        public Header getHeader() { return header; }
        public void setHeader(Header header) { this.header = header; }

        public Body getBody() { return body; }
        public void setBody(Body body) { this.body = body; }
    }

    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class Header {
        @JsonProperty("resultCode") private String resultCode;
        @JsonProperty("resultMsg") private String resultMsg;

        public String getResultCode() { return resultCode; }
        public void setResultCode(String resultCode) { this.resultCode = resultCode; }

        public String getResultMsg() { return resultMsg; }
        public void setResultMsg(String resultMsg) { this.resultMsg = resultMsg; }
    }

    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class Body {
        @JsonProperty("dataType") private String dataType;
        @JsonProperty("numOfRows") private int numOfRows;
        @JsonProperty("pageNo") private int pageNo;
        @JsonProperty("totalCount") private int totalCount;
        @JsonProperty("items") private Items items;

        public String getDataType() { return dataType; }
        public void setDataType(String dataType) { this.dataType = dataType; }

        public int getNumOfRows() { return numOfRows; }
        public void setNumOfRows(int numOfRows) { this.numOfRows = numOfRows; }

        public int getPageNo() { return pageNo; }
        public void setPageNo(int pageNo) { this.pageNo = pageNo; }

        public int getTotalCount() { return totalCount; }
        public void setTotalCount(int totalCount) { this.totalCount = totalCount; }

        public Items getItems() { return items; }
        public void setItems(Items items) { this.items = items; }
    }

    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class Items {
        @JsonProperty("item") private List<Item> item;
        public List<Item> getItem() { return item; }
        public void setItem(List<Item> item) { this.item = item; }
    }

    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class Item {
        // 공통
        @JsonProperty("baseDate") private String baseDate;
        @JsonProperty("baseTime") private String baseTime;
        @JsonProperty("nx") private int nx;
        @JsonProperty("ny") private int ny;
        @JsonProperty("category") private String category;

        // 실황(getUltraSrtNcst) 계열
        @JsonProperty("obsrValue") private String obsrValue;

        // 예보(getUltraSrtFcst) 계열
        @JsonProperty("fcstDate") private String fcstDate;
        @JsonProperty("fcstTime") private String fcstTime;
        @JsonProperty("fcstValue") private String fcstValue;

        public String getBaseDate() { return baseDate; }
        public void setBaseDate(String baseDate) { this.baseDate = baseDate; }

        public String getBaseTime() { return baseTime; }
        public void setBaseTime(String baseTime) { this.baseTime = baseTime; }

        public int getNx() { return nx; }
        public void setNx(int nx) { this.nx = nx; }

        public int getNy() { return ny; }
        public void setNy(int ny) { this.ny = ny; }

        public String getCategory() { return category; }
        public void setCategory(String category) { this.category = category; }

        public String getObsrValue() { return obsrValue; }
        public void setObsrValue(String obsrValue) { this.obsrValue = obsrValue; }

        public String getFcstDate() { return fcstDate; }
        public void setFcstDate(String fcstDate) { this.fcstDate = fcstDate; }

        public String getFcstTime() { return fcstTime; }
        public void setFcstTime(String fcstTime) { this.fcstTime = fcstTime; }

        public String getFcstValue() { return fcstValue; }
        public void setFcstValue(String fcstValue) { this.fcstValue = fcstValue; }

        /** obsrValue 우선, 없으면 fcstValue */
        public String value() {
            if (obsrValue != null && !obsrValue.isBlank()) return obsrValue;
            if (fcstValue != null && !fcstValue.isBlank()) return fcstValue;
            return null;
        }

        /** 예보면 fcstDate/fcstTime, 아니면 baseDate/baseTime */
        public String effectiveDate() { return (fcstDate != null && !fcstDate.isBlank()) ? fcstDate : baseDate; }
        public String effectiveTime() { return (fcstTime != null && !fcstTime.isBlank()) ? fcstTime : baseTime; }
    }
}
