package cy.hexor.furumi.data.remote.model

import cy.hexor.furumi.domain.model.AuthSession
import cy.hexor.furumi.domain.model.AuthTokens
import cy.hexor.furumi.domain.model.User

fun LoginResponse.toDomainSession(
    serverBaseUrl: String,
    receivedAtEpochSeconds: Long
): AuthSession {
    return AuthSession(
        serverBaseUrl = serverBaseUrl,
        user = user.toDomain(),
        tokens = tokens.toDomain(receivedAtEpochSeconds)
    )
}

fun UserResponse.toDomain(): User {
    return User(
        id = id,
        name = name,
        role = role
    )
}

fun TokenResponse.toDomain(receivedAtEpochSeconds: Long): AuthTokens {
    return AuthTokens(
        accessToken = accessToken,
        refreshToken = refreshToken,
        tokenType = tokenType,
        expiresInSeconds = expiresInSeconds,
        expiresAtEpochSeconds = if (expiresInSeconds > 0) {
            receivedAtEpochSeconds + expiresInSeconds
        } else {
            0L
        }
    )
}
