package com.betriebsratkanzlei.legalconsenthub.notification

import com.betriebsratkanzlei.legalconsenthub.user.User
import com.betriebsratkanzlei.legalconsenthub.user.UserMapper
import com.betriebsratkanzlei.legalconsenthub_api.model.CreateNotificationDto
import com.betriebsratkanzlei.legalconsenthub_api.model.NotificationDto
import org.springframework.stereotype.Component

@Component
class NotificationMapper(
    private val userMapper: UserMapper,
) {
    fun toNotificationDto(notification: Notification): NotificationDto =
        NotificationDto(
            id = notification.id!!,
            title = notification.title,
            message = notification.message,
            clickTarget = notification.clickTarget,
            isRead = notification.isRead,
            recipient = notification.recipient?.let { userMapper.toUserDto(it) },
            type = notification.type,
            organizationId = notification.organizationId,
            createdAt = notification.createdAt!!,
            targetRoles = notification.targetRoles,
        )

    fun toNotification(
        createNotificationDto: CreateNotificationDto,
        recipient: User? = null,
        targetRoles: String? = null,
    ): Notification {
        if (recipient != null && targetRoles != null) {
            throw IllegalArgumentException("Only one of recipient or targetRoles can be provided")
        }

        return Notification(
            title = createNotificationDto.title,
            message = createNotificationDto.message,
            clickTarget = createNotificationDto.clickTarget,
            recipient = recipient,
            targetRoles = targetRoles,
            type = createNotificationDto.type,
            organizationId = createNotificationDto.organizationId,
        )
    }
}
