package roles

import (
	"context"
	"net/http"

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

func HandleRoles(w http.ResponseWriter, r *http.Request) {
	var roles []models.Role
	count, err := core.DB.NewSelect().
		Model(&roles).
		Relation("Permissions").
		ScanAndCount(context.Background())

	if count == 0 {
		core.JSONError{
			Status:  core.Error,
			Message: "No role found.",
		}.Respond(w, http.StatusNotFound)
		return
	}

	if err != nil {
		core.JSONError{
			Status:  core.Error,
			Message: err.Error(),
		}.Respond(w, http.StatusInternalServerError)
		return
	}

	core.JSONSuccess{
		Status:  core.Success,
		Message: "Roles found.",
		Data:    roles,
	}.Respond(w, http.StatusOK)
}
