package de.krisenvorrat.server

import com.auth0.jwt.JWT
import com.auth0.jwt.algorithms.Algorithm
import java.util.Date

internal const val TEST_JWT_SECRET = "test-jwt-secret-for-unit-tests-minimum-32-chars"
internal const val TEST_JWT_ISSUER = "krisenvorrat"
internal const val TEST_JWT_AUDIENCE = "krisenvorrat-client"
internal const val TEST_USER_ID = "test-user-id-001"
internal const val TEST_USERNAME = "testuser"
internal const val TEST_ADMIN_ID = "test-admin-id-001"
internal const val TEST_ADMIN_USERNAME = "testadmin"

internal fun testMapConfig(vararg extra: Pair<String, String>) = listOf(
    "krisenvorrat.jwtSecret" to TEST_JWT_SECRET,
    "krisenvorrat.jwtIssuer" to TEST_JWT_ISSUER,
    "krisenvorrat.jwtAudience" to TEST_JWT_AUDIENCE,
    "krisenvorrat.accessTokenExpiryMs" to "3600000",
    "krisenvorrat.refreshTokenExpiryMs" to "2592000000"
) + extra.toList()

internal fun createTestAccessToken(
    userId: String = TEST_USER_ID,
    username: String = TEST_USERNAME,
    isAdmin: Boolean = false
): String = JWT.create()
    .withAudience(TEST_JWT_AUDIENCE)
    .withIssuer(TEST_JWT_ISSUER)
    .withClaim("userId", userId)
    .withClaim("username", username)
    .withClaim("isAdmin", isAdmin)
    .withClaim("type", "access")
    .withExpiresAt(Date(System.currentTimeMillis() + 3_600_000))
    .sign(Algorithm.HMAC256(TEST_JWT_SECRET))

internal fun createTestRefreshToken(userId: String = TEST_USER_ID): String = JWT.create()
    .withAudience(TEST_JWT_AUDIENCE)
    .withIssuer(TEST_JWT_ISSUER)
    .withClaim("userId", userId)
    .withClaim("type", "refresh")
    .withExpiresAt(Date(System.currentTimeMillis() + 2_592_000_000))
    .sign(Algorithm.HMAC256(TEST_JWT_SECRET))
