package com.cloudhandson.ddsbackoffice.config;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.time.Duration;
import org.junit.jupiter.api.Test;

class DdsMcpIamPropertiesTest {

  @Test
  void derivesTheOciIdentityDomainTokenEndpoint() {
    var properties = properties("https://idcs-example.identity.oraclecloud.com", "");

    assertTrue(properties.configured());
    assertEquals("https://idcs-example.identity.oraclecloud.com/oauth2/v1/token",
        properties.tokenEndpoint().toString());
  }

  @Test
  void doesNotReportPartialOauthConfigurationAsReady() {
    var properties = new DdsMcpIamProperties("", "", "client", "secret", "", Duration.ofSeconds(10), Duration.ofSeconds(60));

    assertFalse(properties.configured());
  }

  @Test
  void rejectsANonHttpsTokenEndpoint() {
    var properties = properties("", "http://identity.example.test/oauth2/v1/token");

    assertThrows(IllegalStateException.class, properties::tokenEndpoint);
  }

  private DdsMcpIamProperties properties(String domainUrl, String tokenUri) {
    return new DdsMcpIamProperties(domainUrl, tokenUri, "client", "secret", "database-scope",
        Duration.ofSeconds(10), Duration.ofSeconds(60));
  }
}
