package ru.myitschool.work.data.mapper

import ru.myitschool.work.data.dto.UserInfoDto
import ru.myitschool.work.domain.profile.entities.UserInfoEntity
import java.text.SimpleDateFormat
import java.util.Locale
import javax.inject.Inject

class UserInfoMapper @Inject constructor() {

    operator fun invoke(model: UserInfoDto): Result<UserInfoEntity> {
        return kotlin.runCatching {
            UserInfoEntity(
                fullname = model.fullname ?: error("fullname is null"),
                imageUrl = model.imageUrl ?: error("imageUrl is null"),
                position = model.position ?: error("position is null"),
                lastEntryMillis = model.lastEntry?.let { date ->
                    simpleDateFormat.parse(date)?.time ?: error("parse lastEntry error")
                } ?: error("lastEntry is null")
            )
        }
    }

    private companion object {
        private val simpleDateFormat = SimpleDateFormat(
            "yyyy-MM-dd'T'HH:mm:ss",
            Locale.US
        )
    }
}