package com.cameleer.server.app.outbound.dto;

import com.cameleer.server.core.http.TrustMode;
import com.cameleer.server.core.outbound.OutboundAuth;
import com.cameleer.server.core.outbound.OutboundMethod;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;

import java.util.List;
import java.util.Map;
import java.util.UUID;

public record OutboundConnectionRequest(
        @NotBlank @Size(max = 100) String name,
        @Size(max = 2000) String description,
        @NotBlank @Pattern(regexp = "^https://.+", message = "URL must be HTTPS") String url,
        @NotNull OutboundMethod method,
        Map<String, String> defaultHeaders,
        String defaultBodyTmpl,
        @NotNull TrustMode tlsTrustMode,
        List<String> tlsCaPemPaths,
        String hmacSecret,
        @NotNull @Valid OutboundAuth auth,
        List<UUID> allowedEnvironmentIds
) {
    public OutboundConnectionRequest {
        defaultHeaders = defaultHeaders == null ? Map.of() : defaultHeaders;
        tlsCaPemPaths = tlsCaPemPaths == null ? List.of() : tlsCaPemPaths;
        allowedEnvironmentIds = allowedEnvironmentIds == null ? List.of() : allowedEnvironmentIds;
        if (tlsTrustMode != null && tlsTrustMode == TrustMode.TRUST_PATHS && tlsCaPemPaths.isEmpty()) {
            throw new IllegalArgumentException("tlsCaPemPaths must not be empty when tlsTrustMode = TRUST_PATHS");
        }
    }
}
