package io.cameleer.server.app.security;

import org.junit.jupiter.api.Test;

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

/** Unit tests for {@link OidcProviderNameDeriver}. No Spring context. */
class OidcProviderNameDeriverTest {

    @Test
    void logtoIssuer_returnsLogto() {
        assertThat(OidcProviderNameDeriver.deriveName("https://auth.logto.example/")).isEqualTo("Logto");
        assertThat(OidcProviderNameDeriver.deriveName("https://logto.cameleer.local")).isEqualTo("Logto");
    }

    @Test
    void keycloakIssuer_returnsKeycloak() {
        assertThat(OidcProviderNameDeriver.deriveName("https://keycloak.example/realms/cameleer")).isEqualTo("Keycloak");
    }

    @Test
    void auth0Issuer_returnsAuth0() {
        assertThat(OidcProviderNameDeriver.deriveName("https://example.auth0.com/")).isEqualTo("Auth0");
    }

    @Test
    void oktaIssuer_returnsOkta() {
        assertThat(OidcProviderNameDeriver.deriveName("https://dev-123.okta.com/")).isEqualTo("Okta");
        assertThat(OidcProviderNameDeriver.deriveName("https://login.oktapreview.com/")).isEqualTo("Okta");
    }

    @Test
    void unknownIssuer_returnsGenericLabel() {
        assertThat(OidcProviderNameDeriver.deriveName("https://idp.example.com/")).isEqualTo("Single Sign-On");
    }

    @Test
    void blankOrNullIssuer_returnsGenericLabel() {
        assertThat(OidcProviderNameDeriver.deriveName("")).isEqualTo("Single Sign-On");
        assertThat(OidcProviderNameDeriver.deriveName(null)).isEqualTo("Single Sign-On");
        assertThat(OidcProviderNameDeriver.deriveName("   ")).isEqualTo("Single Sign-On");
    }

    @Test
    void malformedUri_returnsGenericLabel() {
        assertThat(OidcProviderNameDeriver.deriveName("not a url")).isEqualTo("Single Sign-On");
    }

    @Test
    void caseInsensitiveMatching() {
        assertThat(OidcProviderNameDeriver.deriveName("https://AUTH.LOGTO.EXAMPLE/")).isEqualTo("Logto");
    }
}
