package organization

import (
	"git.ex.umbach.dev/LMS/libcore/models"
	"github.com/gofiber/fiber/v2"
	"lms.de/backend/modules/database"
	"lms.de/backend/modules/structs"
	"lms.de/backend/modules/utils"
)

func GetRoles(c *fiber.Ctx) error {
	// swagger:operation GET /organization/roles organization getRoles
	// ---
	// summary: Get roles
	// produces:
	// - application/json
	// responses:
	//   '200':
	//     description: Roles
	//     schema:
	//       "$ref": "#/definitions/RolesResponse"
	//   '500':
	//     description: Failed to get roles

	/*
		var roles []models.Role

		if err := database.DB.Model(&models.Role{}).Where("organization_id = ?", c.Locals("organizationId").(string)).Find(&roles).Error; err != nil {
			return c.SendStatus(fiber.StatusInternalServerError)
		}

		if len(roles) == 0 {
			return c.SendStatus(fiber.StatusInternalServerError)
		}

		for _, role := range roles {
			var rolePermissions []uint16

			if err := database.DB.Model(&models.RolePermission{}).Where("role_id = ?", role.Id).Pluck("permission", &rolePermissions).Error; err != nil {
				return c.SendStatus(fiber.StatusInternalServerError)
			}

			var users []structs.HelperRoleUser

			if err := database.DB.Model(&models.User{}).Where("role_id = ?", role.Id).Where("organization_id = ?", c.Locals("organizationId").(string)).Find(&users).Error; err != nil {
				return c.SendStatus(fiber.StatusInternalServerError)
			}

			rolesResponse.Roles = append(rolesResponse.Roles, structs.HelperRole{
				Id:          role.Id,
				Name:        role.Name,
				Master:      role.Master,
				Permissions: rolePermissions,
				Users:       users,
			})
		} */

	var rolesResponse structs.RolesResponse

	// order is random so we need to define the order
	keys := []string{utils.RoleAdminId, utils.RoleModeratorId, utils.RoleUserId}

	for _, key := range keys {
		role := utils.Roles[key]

		var users []structs.HelperRoleUser

		if err := database.DB.Model(&models.User{}).Where("role_id = ?", key).Where("organization_id = ?", c.Locals("organizationId").(string)).Find(&users).Error; err != nil {
			return c.SendStatus(fiber.StatusInternalServerError)
		}

		rolesResponse.Roles = append(rolesResponse.Roles, structs.HelperRole{
			Id:          key,
			Permissions: role,
			Users:       users,
		})
	}

	return c.JSON(rolesResponse)
}

/*
func CreateRole(c *fiber.Ctx) error {
	// swagger:operation POST /organization/roles organization createRole
	// ---
	// summary: Create role
	// produces:
	// - application/json
	// parameters:
	// - name: name
	//   in: body
	//   description: Role name
	//   required: true
	//   schema:
	//     "$ref": "#/definitions/CreateRoleRequest"
	// responses:
	//   '200':
	//     description: Role created
	//   '400':
	//     description: Invalid input
	//   '500':
	//     description: Failed to create role

	var body structs.CreateRoleRequest

	if err := c.BodyParser(&body); err != nil {
		fmt.Print(err)
		return c.SendStatus(fiber.StatusBadRequest)
	}

	role := models.Role{
		Id:             uuid.New().String(),
		OrganizationId: c.Locals("organizationId").(string),
		Name:           body.Name,
	}

	if err := database.DB.Create(&role).Error; err != nil {
		return c.SendStatus(fiber.StatusInternalServerError)
	}

	return c.SendStatus(fiber.StatusOK)
}
*/
