package dev.lions.unionflow.server;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Field;

import static org.assertj.core.api.Assertions.assertThat;

/**
 * Tests de couverture pour UnionFlowServerApplication.buildBaseUrl().
 * Utilise des sous-classes pour contrôler getUnionflowDomain() sans manipulation d'env.
 */
@DisplayName("UnionFlowServerApplication.buildBaseUrl() - branches manquantes")
class UnionFlowServerApplicationBranchTest {

    /** Sous-classe qui permet de simuler une valeur spécifique de UNIONFLOW_DOMAIN. */
    @jakarta.enterprise.inject.Vetoed
    private static class TestableApp extends UnionFlowServerApplication {
        private final String domain;

        TestableApp(String domain) {
            this.domain = domain;
        }

        @Override
        protected String getUnionflowDomain() {
            return domain;
        }
    }

    private UnionFlowServerApplication buildApp(String activeProfile, String httpHost, int httpPort, String domain)
            throws Exception {
        UnionFlowServerApplication app = new TestableApp(domain);
        setField(app, "activeProfile", activeProfile);
        setField(app, "httpHost", httpHost);
        setField(app, "httpPort", httpPort);
        setField(app, "applicationName", "unionflow-server");
        setField(app, "applicationVersion", "3.0.0");
        setField(app, "quarkusVersion", "3.15.1");
        return app;
    }

    private void setField(Object target, String fieldName, Object value) throws Exception {
        Field field = UnionFlowServerApplication.class.getDeclaredField(fieldName);
        field.setAccessible(true);
        field.set(target, value);
    }

    // ── Branch : prod + UNIONFLOW_DOMAIN null → domain == null → false → localhost ──

    @Test
    @DisplayName("buildBaseUrl: profil prod, domain=null → condition false (A=false) → URL localhost")
    void buildBaseUrl_prodProfile_domainNull_returnsLocalhost() throws Exception {
        UnionFlowServerApplication app = buildApp("prod", "0.0.0.0", 8085, null);
        String url = app.buildBaseUrl();
        // domain == null → A (domain != null) = false → condition false → localhost
        assertThat(url).isEqualTo("http://localhost:8085");
    }

    // ── Branch : prod + UNIONFLOW_DOMAIN = "" → domain.isEmpty() = true → false → localhost ──

    @Test
    @DisplayName("buildBaseUrl: profil prod, domain='' → condition false (B=false) → URL localhost")
    void buildBaseUrl_prodProfile_emptyDomain_returnsLocalhost() throws Exception {
        UnionFlowServerApplication app = buildApp("prod", "0.0.0.0", 8085, "");
        String url = app.buildBaseUrl();
        // domain != null (A=true) && !domain.isEmpty() = false (B=false) → false → localhost
        assertThat(url).isEqualTo("http://localhost:8085");
    }

    // ── Branch : prod + UNIONFLOW_DOMAIN = "api.example.com" → return https://domain ──

    @Test
    @DisplayName("buildBaseUrl: profil prod, domain non-vide → retourne https://domain")
    void buildBaseUrl_prodProfile_domainSet_returnsHttpsDomain() throws Exception {
        UnionFlowServerApplication app = buildApp("prod", "0.0.0.0", 8085, "api.example.com");
        String url = app.buildBaseUrl();
        // domain != null (A=true) && !domain.isEmpty() (B=true) → true → https://domain
        assertThat(url).isEqualTo("https://api.example.com");
    }

    // ── Branch : httpHost != "0.0.0.0" → utilise httpHost directement ──

    @Test
    @DisplayName("buildBaseUrl: httpHost != 0.0.0.0 → utilise httpHost directement")
    void buildBaseUrl_customHost_usesHostDirectly() throws Exception {
        UnionFlowServerApplication app = buildApp("dev", "192.168.1.10", 8085, null);
        String url = app.buildBaseUrl();
        assertThat(url).isEqualTo("http://192.168.1.10:8085");
    }

    // ── Branch : httpHost == "0.0.0.0" → localhost ──

    @Test
    @DisplayName("buildBaseUrl: httpHost=0.0.0.0 → localhost")
    void buildBaseUrl_defaultHost_returnsLocalhost() throws Exception {
        UnionFlowServerApplication app = buildApp("dev", "0.0.0.0", 8080, null);
        String url = app.buildBaseUrl();
        assertThat(url).isEqualTo("http://localhost:8080");
    }
}
