package notification

import (
	"jannex/admin-dashboard-backend/modules/database"
	"jannex/admin-dashboard-backend/modules/notification"
	"jannex/admin-dashboard-backend/modules/structs"
	"jannex/admin-dashboard-backend/modules/utils"

	"git.ex.umbach.dev/Alex/roese-utils/rspagination"
	"git.ex.umbach.dev/Alex/roese-utils/rsutils"
	"github.com/gofiber/fiber/v2"
)

func AddNotification(c *fiber.Ctx) error {
	// swagger:operation POST /notifications notifications notificationsAddNotification
	// ---
	// summary: Add a new notification
	// consumes:
	// - application/json
	// produces:
	// - application/json
	// parameters:
	// - name: X-Api-Key
	//   in: header
	//   description: You can create a new api key in your user profile
	// - name: body
	//   in: body
	//   schema:
	//     "$ref": "#/definitions/AddNotificationRequest"
	// responses:
	//   '200':
	//     description: New notification added successfully
	//   '400':
	//     description: Invalid request body
	//   '401':
	//     description: No permissions
	//   '422':
	//     description: No users found
	//   '500':
	//     description: Failed to add notification

	var body structs.AddNotificationRequest

	if err := rsutils.BodyParserHelper(c, &body); err != nil {
		return c.SendStatus(fiber.StatusBadRequest)
	}

	return notification.AddNotification(c, body)
}

func GetNotifications(c *fiber.Ctx) error {
	// swagger:operation GET /notifications notifications notificationsGetNotifications
	// ---
	// summary: Get notifications
	// consumes:
	// - application/json
	// produces:
	// - application/json
	// parameters:
	// - name: X-Api-Key
	//   in: header
	//   description: You can create a new api key in your user profile
	// - name: page
	//   in: query
	//   description: Page number
	// responses:
	//   '200':
	//     description: Notifications
	//     schema:
	//       "$ref": "#/definitions/NotificationsResponse"
	//   '401':
	//     description: No permissions
	//   '500':
	//     description: Failed to get notifications

	var query rspagination.PageQuery

	if err := rsutils.QueryParserHelper(c, &query); err != nil {
		return c.SendStatus(fiber.StatusBadRequest)
	}

	userId := c.Locals("userId").(string)

	return c.JSON(structs.NotificationsResponse{
		Notifications: notification.GetNotifications(query, userId),
		TotalPages: rspagination.GetTotalPages(database.DB, utils.NotificationsPaginationLimit,
			[]structs.Notification{},
			"user_id = ?",
			userId),
	})
}
