package org.nstart.dep265.requestservice

import java.nio.file.Files
import java.nio.file.Path
import kotlin.io.path.readText
import kotlin.test.Test
import kotlin.test.assertTrue

class RequestServiceConfigTest {
    @Test
    fun `local profile uses concrete postgres datasource`() {
        val config = configRepositoryFile().readText()

        assertTrue(config.contains("on-profile: local"))
        assertTrue(config.contains("url: jdbc:postgresql://\${pcp.infra.postgres.host}:\${pcp.infra.postgres.port}/pcp_requests"))
        assertTrue(config.contains("username: postgres"))
        assertTrue(config.contains("password: password"))
    }

    @Test
    fun `shared datasource keeps environment placeholders for deployed profiles`() {
        val config = configRepositoryFile().readText()

        assertTrue(config.contains("url: \"\${SPRING_DATASOURCE_URL}\""))
        assertTrue(config.contains("username: \"\${SPRING_DATASOURCE_USERNAME}\""))
        assertTrue(config.contains("password: \"\${SPRING_DATASOURCE_PASSWORD}\""))
    }

    private fun configRepositoryFile(): Path {
        val rootPath = Path.of("config-repo/pcp-request-service.yaml")
        if (Files.exists(rootPath)) {
            return rootPath
        }
        return Path.of("../../config-repo/pcp-request-service.yaml")
    }
}
