package com.nisemoe.nise.service

import com.nisemoe.generated.tables.references.USERS
import org.jooq.DSLContext
import org.springframework.security.authentication.AnonymousAuthenticationToken
import org.springframework.security.core.context.SecurityContextHolder
import org.springframework.security.oauth2.core.user.DefaultOAuth2User
import org.springframework.stereotype.Service

@Service
class AuthService(
    private val dslContext: DSLContext
) {

    data class UserInfo(
        val userId: Long,
        val username: String
    )

    fun isAdmin(): Boolean {
        if(!this.isLoggedIn())
            return false

        val currentUser = this.getCurrentUser()
        return dslContext.fetchExists(USERS, USERS.USER_ID.eq(currentUser.userId).and(USERS.IS_ADMIN))
    }

    fun isLoggedIn(): Boolean {
        val authentication = SecurityContextHolder.getContext().authentication
        return !(authentication == null || authentication is AnonymousAuthenticationToken || authentication.principal == null)
    }

    fun getCurrentUser(): UserInfo {
        if(!this.isLoggedIn())
            throw IllegalStateException("User is not logged in")

        val authentication = SecurityContextHolder.getContext().authentication
        val userDetails = authentication.principal as DefaultOAuth2User

        return UserInfo(
            userId = (userDetails.attributes["id"] as Int).toLong(),
            username = userDetails.attributes["username"] as String
        )
    }

}