package adminarea

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

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

func GetRoles(c *fiber.Ctx) error {
	// swagger:operation GET /adminarea/roles adminarea adminareaGetRoles
	// ---
	// summary: Get all roles
	// 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: All roles
	//     schema:
	//       "$ref": "#/definitions/AdminAreaRolesResponse"
	//   '401':
	//     description: No permissions
	//   '500':
	//     description: Failed to get roles

	if !socketclients.HasOnePermission(c.Locals("userId").(string),
		[]string{utils.PermissionAdminAreaCreateNewRole,
			utils.PermissionAdminAreaDeleteRole,
			utils.PermissionAdminAreaMoveRoleUpDown,
			utils.PermissionAdminAreaUpdateRole}) {
		return c.SendStatus(fiber.StatusUnauthorized)
	}

	var users []structs.AdminAreaUserInfo

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

	return c.JSON(structs.AdminAreaRolesResponse{
		Roles:            socketclients.GetAllRoles(),
		RolesPermissions: socketclients.GetAdminAreaRolesPermissions(),
		Users:            users,
	})
}
