package handlers

import (
	"net/http"
	"strconv"

	"github.com/flatrender/notification-svc/internal/db"
	"github.com/flatrender/notification-svc/internal/middleware"
	"github.com/flatrender/notification-svc/internal/models"
	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
)

type NotificationHandler struct {
	store *db.Store
}

func NewNotificationHandler(store *db.Store) *NotificationHandler {
	return &NotificationHandler{store: store}
}

// GET /v1/notifications
func (h *NotificationHandler) List(c *gin.Context) {
	userID := middleware.GetUserID(c)
	onlyUnread := c.Query("unread") == "true"
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
	if page < 1 {
		page = 1
	}
	if pageSize < 1 || pageSize > 100 {
		pageSize = 20
	}

	notifs, total, err := h.store.ListNotifications(c.Request.Context(), userID, onlyUnread, page, pageSize)
	if err != nil {
		c.JSON(http.StatusInternalServerError, models.APIError{Code: "internal_error", Message: err.Error()})
		return
	}
	if notifs == nil {
		notifs = []*models.Notification{}
	}
	c.JSON(http.StatusOK, models.PagedResponse[*models.Notification]{
		Data: notifs,
		Meta: models.PaginationMeta{
			Page:     page,
			PageSize: pageSize,
			Total:    total,
			HasMore:  int64(page*pageSize) < total,
		},
	})
}

// GET /v1/notifications/unread-count
func (h *NotificationHandler) UnreadCount(c *gin.Context) {
	userID := middleware.GetUserID(c)
	count, err := h.store.CountUnread(c.Request.Context(), userID)
	if err != nil {
		c.JSON(http.StatusInternalServerError, models.APIError{Code: "internal_error", Message: err.Error()})
		return
	}
	c.JSON(http.StatusOK, gin.H{"count": count})
}

// GET /v1/notifications/:id
func (h *NotificationHandler) Get(c *gin.Context) {
	userID := middleware.GetUserID(c)
	id, err := uuid.Parse(c.Param("id"))
	if err != nil {
		c.JSON(http.StatusBadRequest, models.APIError{Code: "bad_request", Message: "invalid id"})
		return
	}
	n, err := h.store.GetNotificationByID(c.Request.Context(), id, userID)
	if err != nil {
		c.JSON(http.StatusNotFound, models.APIError{Code: "not_found", Message: err.Error()})
		return
	}
	c.JSON(http.StatusOK, n)
}

// POST /v1/notifications/:id/seen
func (h *NotificationHandler) MarkSeen(c *gin.Context) {
	userID := middleware.GetUserID(c)
	id, err := uuid.Parse(c.Param("id"))
	if err != nil {
		c.JSON(http.StatusBadRequest, models.APIError{Code: "bad_request", Message: "invalid id"})
		return
	}
	if err := h.store.MarkSeen(c.Request.Context(), id, userID); err != nil {
		c.JSON(http.StatusInternalServerError, models.APIError{Code: "internal_error", Message: err.Error()})
		return
	}
	c.Status(http.StatusNoContent)
}

// POST /v1/notifications/seen-all
func (h *NotificationHandler) MarkAllSeen(c *gin.Context) {
	userID := middleware.GetUserID(c)
	count, err := h.store.MarkAllSeen(c.Request.Context(), userID)
	if err != nil {
		c.JSON(http.StatusInternalServerError, models.APIError{Code: "internal_error", Message: err.Error()})
		return
	}
	c.JSON(http.StatusOK, gin.H{"marked_seen": count})
}

// POST /v1/notifications/:id/click
func (h *NotificationHandler) MarkClicked(c *gin.Context) {
	userID := middleware.GetUserID(c)
	id, err := uuid.Parse(c.Param("id"))
	if err != nil {
		c.JSON(http.StatusBadRequest, models.APIError{Code: "bad_request", Message: "invalid id"})
		return
	}
	if err := h.store.MarkClicked(c.Request.Context(), id, userID); err != nil {
		c.JSON(http.StatusInternalServerError, models.APIError{Code: "internal_error", Message: err.Error()})
		return
	}
	c.Status(http.StatusNoContent)
}

// DELETE /v1/notifications/:id
func (h *NotificationHandler) Delete(c *gin.Context) {
	userID := middleware.GetUserID(c)
	id, err := uuid.Parse(c.Param("id"))
	if err != nil {
		c.JSON(http.StatusBadRequest, models.APIError{Code: "bad_request", Message: "invalid id"})
		return
	}
	if err := h.store.SoftDeleteNotification(c.Request.Context(), id, userID); err != nil {
		c.JSON(http.StatusNotFound, models.APIError{Code: "not_found", Message: err.Error()})
		return
	}
	c.Status(http.StatusNoContent)
}

// POST /v1/internal/notifications  — called by other services to create notifications
func (h *NotificationHandler) CreateInternal(c *gin.Context) {
	var req models.CreateNotificationRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, models.APIError{Code: "bad_request", Message: err.Error()})
		return
	}
	n, err := h.store.CreateNotification(c.Request.Context(), &req)
	if err != nil {
		c.JSON(http.StatusInternalServerError, models.APIError{Code: "internal_error", Message: err.Error()})
		return
	}
	c.JSON(http.StatusCreated, n)
}

// ── Preferences ───────────────────────────────────────────────────────────────

type PreferenceHandler struct {
	store *db.Store
}

func NewPreferenceHandler(store *db.Store) *PreferenceHandler {
	return &PreferenceHandler{store: store}
}

// GET /v1/notifications/preferences
func (h *PreferenceHandler) List(c *gin.Context) {
	userID := middleware.GetUserID(c)
	prefs, err := h.store.ListPreferences(c.Request.Context(), userID)
	if err != nil {
		c.JSON(http.StatusInternalServerError, models.APIError{Code: "internal_error", Message: err.Error()})
		return
	}
	if prefs == nil {
		prefs = []*models.NotificationPreference{}
	}
	c.JSON(http.StatusOK, gin.H{"data": prefs})
}

// PUT /v1/notifications/preferences
func (h *PreferenceHandler) Upsert(c *gin.Context) {
	userID := middleware.GetUserID(c)
	var req models.UpdatePreferenceRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, models.APIError{Code: "bad_request", Message: err.Error()})
		return
	}
	if err := h.store.UpsertPreference(c.Request.Context(), userID, &req); err != nil {
		c.JSON(http.StatusInternalServerError, models.APIError{Code: "internal_error", Message: err.Error()})
		return
	}
	c.Status(http.StatusNoContent)
}

// ── Templates (admin) ─────────────────────────────────────────────────────────

type TemplateHandler struct {
	store *db.Store
}

func NewTemplateHandler(store *db.Store) *TemplateHandler {
	return &TemplateHandler{store: store}
}

// GET /v1/notification-templates
func (h *TemplateHandler) List(c *gin.Context) {
	tpls, err := h.store.ListTemplates(c.Request.Context(), nil)
	if err != nil {
		c.JSON(http.StatusInternalServerError, models.APIError{Code: "internal_error", Message: err.Error()})
		return
	}
	if tpls == nil {
		tpls = []*models.NotificationTemplate{}
	}
	c.JSON(http.StatusOK, gin.H{"data": tpls})
}

// PUT /v1/notification-templates
func (h *TemplateHandler) Upsert(c *gin.Context) {
	var req models.TemplateUpsertRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, models.APIError{Code: "bad_request", Message: err.Error()})
		return
	}
	tpl, err := h.store.UpsertTemplate(c.Request.Context(), nil, &req)
	if err != nil {
		c.JSON(http.StatusInternalServerError, models.APIError{Code: "internal_error", Message: err.Error()})
		return
	}
	c.JSON(http.StatusOK, tpl)
}
