package user

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

	"github.com/gofiber/fiber/v2"
)

func UserInfo(c *fiber.Ctx) error {
	// swagger:operation GET /user/info user userInfo
	// ---
	// summary: Get user info
	// 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
	// responses:
	//   '200':
	//     description: User info
	//     schema:
	//       "$ref": "#/definitions/UserShortInfoResponse"
	//   '401':
	//     description: No permissions
	//   '500':
	//     description: Failed to get user info

	userId := c.Locals("userId").(string)
	var user structs.User

	database.DB.Select("username, avatar", "role_id").First(&user, "id = ?", userId)

	categories := socketclients.GetAvailableCategories(userId)

	var users []structs.UserShortInfoResponse

	database.DB.Model(&structs.User{}).Find(&users)

	return c.JSON(structs.UserInfoResponse{
		UserId:              userId,
		Username:            user.Username,
		Avatar:              user.Avatar,
		Permissions:         socketclients.GetPermissionsByRoleId(user.RoleId),
		AvailableCategories: categories,
		Users:               users,
		TotalNotifications:  notification.GetTotalNotifications(userId),
	})
}
