package go.kr.project.api.config.properties;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

@Data
@Component
@ConfigurationProperties(prefix = "vmis")
@Validated
public class VmisProperties {

    @NotNull
    private RestTemplateProps restTemplate = new RestTemplateProps();
    @NotNull
    private ExternalProps external = new ExternalProps();

    @Data
    public static class ExternalProps {
        @NotNull
        private ApiProps api = new ApiProps();

        @Data
        public static class ApiProps {
            @NotNull
            private UrlProps url = new UrlProps();

            @Data
            public static class UrlProps {
                @NotBlank
                private String base = "http://localhost:8081/api/v1/vehicles"; // 기본 Base URL

                @NotNull
                private ApiTypeProps basic = new ApiTypeProps();  // 자동차기본정보 API 설정

                @NotNull
                private ApiTypeProps ledger = new ApiTypeProps(); // 자동차등록원부(갑) API 설정

                @Data
                public static class ApiTypeProps {
                    @NotBlank
                    private String oldOrNew = "new";  // API 버전 선택 ("old" 또는 "new")

                    @NotBlank
                    private String oldUrl = "/old-basic";  // 구 API 엔드포인트

                    @NotBlank
                    private String newUrl = "/new-basic";  // 신 API 엔드포인트

                    // 선택된 API URL 반환
                    public String getSelectedUrl() {
                        return "old".equalsIgnoreCase(oldOrNew) ? oldUrl : newUrl;
                    }
                }

                // 외부 VMIS-interface 호출용 전체 URL 조합 헬퍼
                // YAML 설정(old-or-new)에 따라 자동으로 선택
                public String buildBasicUrl() {
                    return join(base, basic.getSelectedUrl());
                }

                public String buildLedgerUrl() {
                    return join(base, ledger.getSelectedUrl());
                }

                // 명시적으로 old/new 버전 선택 (하위 호환성 및 명시적 호출용)
                public String buildOldBasicUrl() {
                    return join(base, basic.getOldUrl());
                }

                public String buildNewBasicUrl() {
                    return join(base, basic.getNewUrl());
                }

                public String buildOldLedgerUrl() {
                    return join(base, ledger.getOldUrl());
                }

                public String buildNewLedgerUrl() {
                    return join(base, ledger.getNewUrl());
                }

                private String join(String base, String path) {
                    String b = base;
                    String p = path;
                    if (b.endsWith("/")) {
                        b = b.substring(0, b.length() - 1);
                    }
                    if (!p.startsWith("/")) {
                        p = "/" + p;
                    }
                    return b + p;
                }
            }
        }
    }

    @Data
    public static class RestTemplateProps {
        @NotNull
        private TimeoutConfig timeout = new TimeoutConfig();
        @NotNull
        private ConnectionPoolConfig connectionPool = new ConnectionPoolConfig();
        @NotNull
        private RateLimitConfig rateLimit = new RateLimitConfig();

        @Data
        public static class TimeoutConfig {
            private int connectTimeoutMillis = 10000;
            private int readTimeoutMillis = 12000;
        }

        @Data
        public static class ConnectionPoolConfig {
            private int maxTotal = 100;
            private int maxPerRoute = 20;
        }

        @Data
        public static class RateLimitConfig {
            private double permitsPerSecond = 5.0;
        }
    }
}
