package com.cameleer.server.app.runtime;

import com.cameleer.server.core.runtime.ResolvedContainerConfig;
import org.junit.jupiter.api.Test;

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

import static org.junit.jupiter.api.Assertions.*;

class TraefikLabelBuilderTest {

    private static ResolvedContainerConfig config(boolean externalRouting, String certResolver) {
        return new ResolvedContainerConfig(
                512, null, 500, null,
                8080, List.of(), Map.of(),
                true, true,
                "path", "example.com", "https://cameleer.example.com",
                1, "blue-green",
                true, true,
                "spring-boot", "", List.of(),
                externalRouting,
                certResolver
        );
    }

    @Test
    void build_emitsTraefikLabelsWhenExternalRoutingEnabled() {
        Map<String, String> labels = TraefikLabelBuilder.build(
                "myapp", "dev", "acme", config(true, null), 0, "abcdef01");

        assertEquals("true", labels.get("traefik.enable"));
        assertEquals("8080", labels.get("traefik.http.services.dev-myapp.loadbalancer.server.port"));
        assertEquals("PathPrefix(`/dev/myapp/`)", labels.get("traefik.http.routers.dev-myapp.rule"));
    }

    @Test
    void build_omitsAllTraefikLabelsWhenExternalRoutingDisabled() {
        Map<String, String> labels = TraefikLabelBuilder.build(
                "myapp", "dev", "acme", config(false, null), 0, "abcdef01");

        long traefikLabelCount = labels.keySet().stream()
                .filter(k -> k.startsWith("traefik."))
                .count();
        assertEquals(0, traefikLabelCount, "expected no traefik.* labels but found: " + labels);
    }

    @Test
    void build_preservesIdentityLabelsWhenExternalRoutingDisabled() {
        Map<String, String> labels = TraefikLabelBuilder.build(
                "myapp", "dev", "acme", config(false, null), 2, "abcdef01");

        assertEquals("cameleer-server", labels.get("managed-by"));
        assertEquals("acme", labels.get("cameleer.tenant"));
        assertEquals("myapp", labels.get("cameleer.app"));
        assertEquals("dev", labels.get("cameleer.environment"));
        assertEquals("2", labels.get("cameleer.replica"));
        assertEquals("abcdef01", labels.get("cameleer.generation"));
        assertEquals("dev-myapp-2-abcdef01", labels.get("cameleer.instance-id"));
    }

    @Test
    void build_emitsCertResolverLabelWhenConfigured() {
        Map<String, String> labels = TraefikLabelBuilder.build(
                "myapp", "dev", "acme", config(true, "letsencrypt"), 0, "abcdef01");

        assertEquals("true", labels.get("traefik.http.routers.dev-myapp.tls"));
        assertEquals("letsencrypt", labels.get("traefik.http.routers.dev-myapp.tls.certresolver"));
    }

    @Test
    void build_omitsCertResolverLabelWhenNull() {
        Map<String, String> labels = TraefikLabelBuilder.build(
                "myapp", "dev", "acme", config(true, null), 0, "abcdef01");

        assertEquals("true", labels.get("traefik.http.routers.dev-myapp.tls"),
                "sslOffloading=true should still mark the router TLS-enabled");
        assertNull(labels.get("traefik.http.routers.dev-myapp.tls.certresolver"),
                "cert resolver label must be omitted when none is configured");
    }

    @Test
    void build_omitsCertResolverLabelWhenBlank() {
        Map<String, String> labels = TraefikLabelBuilder.build(
                "myapp", "dev", "acme", config(true, "   "), 0, "abcdef01");

        assertNull(labels.get("traefik.http.routers.dev-myapp.tls.certresolver"),
                "whitespace-only cert resolver must be treated as unset");
    }
}
