package com.betriebsratkanzlei.legalconsenthub.notification

import com.betriebsratkanzlei.legalconsenthub.security.CustomJwtTokenPrincipal
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
) : NotificationApi {

    override fun getNotifications(page: Int, size: Int): ResponseEntity<PagedNotificationDto> {
        val principal = SecurityContextHolder.getContext().authentication.principal as CustomJwtTokenPrincipal
        val recipientId = principal.id ?: throw IllegalStateException("User ID not found")
        val organizationId = principal.organizationId ?: throw IllegalStateException("Organization ID not found")

        val notifications = notificationService.getNotifications(
            recipientId = recipientId,
            organizationId = organizationId,
            page = page,
            size = size
        )

        return ResponseEntity.ok(pagedNotificationMapper.toPagedNotificationDto(notifications))
    }

    override fun getUnreadNotifications(): ResponseEntity<List<NotificationDto>> {
        val principal = SecurityContextHolder.getContext().authentication.principal as CustomJwtTokenPrincipal
        val recipientId = principal.id ?: throw IllegalStateException("User ID not found")
        val organizationId = principal.organizationId ?: throw IllegalStateException("Organization ID not found")

        val notifications = notificationService.getUnreadNotifications(
            recipientId = recipientId,
            organizationId = organizationId
        )

        return ResponseEntity.ok(notifications.map { notificationMapper.toNotificationDto(it) })
    }

    override fun getUnreadNotificationCount(): ResponseEntity<kotlin.Long> {
        val principal = SecurityContextHolder.getContext().authentication.principal as CustomJwtTokenPrincipal
        val recipientId = principal.id ?: throw IllegalStateException("User ID not found")
        val organizationId = principal.organizationId ?: throw IllegalStateException("Organization ID not found")

        val count = notificationService.getUnreadNotificationCount(
            recipientId = recipientId,
            organizationId = organizationId
        )

        return ResponseEntity.ok(count)
    }

    override fun markAllNotificationsAsRead(): ResponseEntity<Unit> {
        val principal = SecurityContextHolder.getContext().authentication.principal as CustomJwtTokenPrincipal
        val recipientId = principal.id ?: throw IllegalStateException("User ID not found")
        val organizationId = principal.organizationId ?: throw IllegalStateException("Organization ID not found")

        notificationService.markAllAsRead(
            recipientId = recipientId,
            organizationId = organizationId
        )

        return ResponseEntity.noContent().build()
    }

    override fun markNotificationAsRead(id: UUID): ResponseEntity<NotificationDto> {
        val notification = notificationService.markNotificationAsRead(id)
        return ResponseEntity.ok(notificationMapper.toNotificationDto(notification))
    }

    override fun createNotification(createNotificationDto: CreateNotificationDto): ResponseEntity<NotificationDto> {
        val notification = notificationService.createNotification(createNotificationDto)
        return ResponseEntity.ok(notificationMapper.toNotificationDto(notification))
    }
}
