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
                @NotBlank
                private String basic = "/basic";   // 자동차기본정보 엔드포인트 경로
                @NotBlank
                private String ledger = "/ledger"; // 자동차등록원부 엔드포인트 경로

                // 외부 VMIS-interface 호출용 전체 URL 조합 헬퍼
                public String buildBasicUrl() { return join(base, basic); }
                public String buildLedgerUrl() { return join(base, ledger); }

                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;
        }
    }
}
