package permissions

import (
	"context"
	"net/http"

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

func HandlePermission(w http.ResponseWriter, r *http.Request) {
	resource := r.PathValue("resource")
	action := r.PathValue("action")
	var permissions models.Permission
	err := core.DB.NewSelect().
		Model(&permissions).
		Where("resource = ? AND action = ?", resource, action).
		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 found.",
		Data:    permissions,
	}.Respond(w, http.StatusOK)
}
