package com.betriebsratkanzlei.legalconsenthub.user

import com.betriebsratkanzlei.legalconsenthub_api.model.UserDto
import org.springframework.stereotype.Component

@Component
class UserMapper(
    private val roleConverter: UserRoleConverter
) {
    fun toUserDto(user: User): UserDto {
        val organizationRolesDto = roleConverter.convertToMap(user.organizationRoles)

        return UserDto(
            id = user.id,
            name = user.name,
            status = user.status,
            organizationRoles = organizationRolesDto
        )
    }

    fun toUser(userDto: UserDto): User {
        val user = User(
            id = userDto.id,
            name = userDto.name,
            status = userDto.status
        )

        userDto.organizationRoles.forEach { (orgId, roles) ->
            roleConverter.setRolesForOrganization(user.organizationRoles, orgId, roles)
        }

        return user
    }
}
