package roles

import (
	"context"
	"net/http"

	"fr.latosa-escrima/core"
	"fr.latosa-escrima/core/models"
)

func HandleRolePermissions(w http.ResponseWriter, r *http.Request) {
	uuid := r.PathValue("role_uuid")

	var role models.Role
	err := core.DB.NewSelect().
		Model(&role).
		Where("id = ?", uuid).
		Relation("Permissions").
		Limit(1).
		Scan(context.Background())
	if err != nil {
		core.JSONError{
			Status:  core.Error,
			Message: err.Error(),
		}.Respond(w, http.StatusInternalServerError)
		return
	}

	core.JSONSuccess{
		Status:  core.Success,
		Message: "Permissions.",
		Data:    role.Permissions,
	}.Respond(w, http.StatusOK)

}
