package com.cameleer.server.core.outbound;

import com.cameleer.server.core.http.TrustMode;

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

public record OutboundConnection(
        UUID id,
        String tenantId,
        String name,
        String description,
        String url,
        OutboundMethod method,
        Map<String, String> defaultHeaders,
        String defaultBodyTmpl,
        TrustMode tlsTrustMode,
        List<String> tlsCaPemPaths,
        String hmacSecretCiphertext,
        OutboundAuth auth,
        List<UUID> allowedEnvironmentIds,
        Instant createdAt,
        String createdBy,
        Instant updatedAt,
        String updatedBy
) {
    public OutboundConnection {
        defaultHeaders = defaultHeaders == null ? Map.of() : Map.copyOf(defaultHeaders);
        tlsCaPemPaths = tlsCaPemPaths == null ? List.of() : List.copyOf(tlsCaPemPaths);
        allowedEnvironmentIds = allowedEnvironmentIds == null ? List.of() : List.copyOf(allowedEnvironmentIds);
        if (auth == null) auth = new OutboundAuth.None();
    }

    public boolean isAllowedInEnvironment(UUID environmentId) {
        return allowedEnvironmentIds.isEmpty() || allowedEnvironmentIds.contains(environmentId);
    }
}
