package permissions

import (
	"context"
	"net/http"

	"fr.latosa-escrima/core"
)

func HandlePermissions(w http.ResponseWriter, r *http.Request) {
	var permissions core.Permissions
	count, err := core.DB.NewSelect().
		Model(&permissions).
		ScanAndCount(context.Background())
	if count == 0 {
		core.JSONSuccess{
			Status:  core.Success,
			Message: "No permissions 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: "Permissions found.",
		Data:    permissions,
	}.Respond(w, http.StatusOK)
}
