package com.example.furumi_android.data.remote.model

import com.example.furumi_android.domain.model.AuthSession
import com.example.furumi_android.domain.model.AuthTokens
import com.example.furumi_android.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
        }
    )
}
