package handlers

import (
	"net/http"

	apperrors "veza-backend-api/internal/errors"
	"veza-backend-api/internal/services"

	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
)

var NotificationHandlersInstance *NotificationHandlers

type NotificationHandlers struct {
	notificationService *services.NotificationService
}

func NewNotificationHandlers(notificationService *services.NotificationService) {
	NotificationHandlersInstance = &NotificationHandlers{
		notificationService: notificationService,
	}
}

// GetNotifications retrieves all notifications for the authenticated user
// GET /api/v1/notifications
// BE-API-016: Implement notifications endpoints
func (nh *NotificationHandlers) GetNotifications(c *gin.Context) {
	userID, ok := GetUserIDUUID(c)
	if !ok {
		return // Erreur déjà envoyée par GetUserIDUUID
	}

	read := c.DefaultQuery("read", "")
	var unreadOnly bool
	if read == "false" {
		unreadOnly = true
	}

	notifications, err := nh.notificationService.GetNotifications(userID, unreadOnly)
	if err != nil {
		RespondWithAppError(c, apperrors.Wrap(apperrors.ErrCodeInternal, "Failed to get notifications", err))
		return
	}

	RespondSuccess(c, http.StatusOK, notifications)
}

// MarkAsRead marks a notification as read
// POST /api/v1/notifications/:id/read
// BE-API-016: Implement notifications endpoints
func (nh *NotificationHandlers) MarkAsRead(c *gin.Context) {
	userID, ok := GetUserIDUUID(c)
	if !ok {
		return // Erreur déjà envoyée par GetUserIDUUID
	}

	notificationID, err := uuid.Parse(c.Param("id"))
	if err != nil {
		RespondWithAppError(c, apperrors.NewValidationError("invalid notification id"))
		return
	}

	err = nh.notificationService.MarkAsRead(userID, notificationID)
	if err != nil {
		RespondWithAppError(c, apperrors.Wrap(apperrors.ErrCodeInternal, "Failed to mark notification as read", err))
		return
	}

	RespondSuccess(c, http.StatusOK, gin.H{"message": "Notification marked as read"})
}

// MarkAllAsRead marks all notifications as read for the user
// POST /api/v1/notifications/read-all
// BE-API-016: Implement notifications endpoints
func (nh *NotificationHandlers) MarkAllAsRead(c *gin.Context) {
	userID, ok := GetUserIDUUID(c)
	if !ok {
		return // Erreur déjà envoyée par GetUserIDUUID
	}

	if err := nh.notificationService.MarkAllAsRead(userID); err != nil {
		RespondWithAppError(c, apperrors.Wrap(apperrors.ErrCodeInternal, "Failed to mark all notifications as read", err))
		return
	}

	RespondSuccess(c, http.StatusOK, gin.H{"message": "All notifications marked as read"})
}

// GetUnreadCount returns the count of unread notifications
func (nh *NotificationHandlers) GetUnreadCount(c *gin.Context) {
	userID, exists := c.Get("user_id")
	if !exists {
		c.JSON(http.StatusUnauthorized, gin.H{"error": "User not authenticated"})
		return
	}

	count, err := nh.notificationService.GetUnreadCount(userID.(uuid.UUID))
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	RespondSuccess(c, http.StatusOK, gin.H{"count": count})
}
