package com.betriebsratkanzlei.legalconsenthub.notification import com.betriebsratkanzlei.legalconsenthub.security.CustomJwtTokenPrincipal import com.betriebsratkanzlei.legalconsenthub.user.UserService import com.betriebsratkanzlei.legalconsenthub_api.api.NotificationApi import com.betriebsratkanzlei.legalconsenthub_api.model.CreateNotificationDto import com.betriebsratkanzlei.legalconsenthub_api.model.NotificationDto import com.betriebsratkanzlei.legalconsenthub_api.model.PagedNotificationDto import org.springframework.http.ResponseEntity import org.springframework.security.core.context.SecurityContextHolder import org.springframework.web.bind.annotation.RestController import java.util.UUID @RestController class NotificationController( private val notificationService: NotificationService, private val notificationMapper: NotificationMapper, private val pagedNotificationMapper: PagedNotificationMapper, private val userService: UserService ) : NotificationApi { override fun getNotifications(page: Int, size: Int): ResponseEntity { val principal = SecurityContextHolder.getContext().authentication.principal as CustomJwtTokenPrincipal val recipientId = principal.id ?: throw IllegalStateException("User ID not found") val user = userService.getUserById(recipientId) val notifications = if (user.role != null) { notificationService.getNotificationsForUserAndGroup( recipientId = recipientId, userRole = user.role!!.value, page = page, size = size ) } else { notificationService.getNotificationsForUser( recipientId = recipientId, page = page, size = size ) } return ResponseEntity.ok(pagedNotificationMapper.toPagedNotificationDto(notifications)) } override fun getUnreadNotifications(): ResponseEntity> { val principal = SecurityContextHolder.getContext().authentication.principal as CustomJwtTokenPrincipal val recipientId = principal.id ?: throw IllegalStateException("User ID not found") val user = userService.getUserById(recipientId) val notifications = if (user.role != null) { notificationService.getUnreadNotificationsForUserAndGroup(recipientId, user.role!!.value) } else { notificationService.getUnreadNotificationsForUser(recipientId) } return ResponseEntity.ok(notifications.map { notificationMapper.toNotificationDto(it) }) } override fun getUnreadNotificationCount(): ResponseEntity { val principal = SecurityContextHolder.getContext().authentication.principal as CustomJwtTokenPrincipal val recipientId = principal.id ?: throw IllegalStateException("User ID not found") val user = userService.getUserById(recipientId) val count = if (user.role != null) { notificationService.getUnreadNotificationCountForUserAndGroup(recipientId, user.role!!.value) } else { notificationService.getUnreadNotificationCount(recipientId) } return ResponseEntity.ok(count) } override fun markAllNotificationsAsRead(): ResponseEntity { val principal = SecurityContextHolder.getContext().authentication.principal as CustomJwtTokenPrincipal val recipientId = principal.id ?: throw IllegalStateException("User ID not found") val user = userService.getUserById(recipientId) if (user.role != null) { notificationService.markAllAsReadForUserAndGroup(recipientId, user.role!!.value) } else { notificationService.markAllAsRead(recipientId) } return ResponseEntity.noContent().build() } override fun markNotificationAsRead(id: UUID): ResponseEntity { val notification = notificationService.markAsRead(id) ?: return ResponseEntity.notFound().build() return ResponseEntity.ok(notificationMapper.toNotificationDto(notification)) } override fun createNotification(createNotificationDto: CreateNotificationDto): ResponseEntity { val notification = notificationService.createNotification(createNotificationDto) return ResponseEntity.ok(notificationMapper.toNotificationDto(notification)) } }