package sresponse

import (
	"net/http"

	"github.com/tech/sendico/pkg/api/http/response"
	"github.com/tech/sendico/pkg/mlogger"
	"github.com/tech/sendico/pkg/model"
)

type permissionsDescription struct {
	Roles    []model.RoleDescription   `json:"roles"`
	Policies []model.PolicyDescription `json:"policies"`
}

type permissionsData struct {
	Roles       []model.Role       `json:"roles"`
	Policies    []model.RolePolicy `json:"policies"`
	Permissions []model.Permission `json:"permissions"`
}

type permissionsResponse struct {
	authResponse `json:",inline"`
	Descriptions permissionsDescription `json:"descriptions"`
	Permissions  permissionsData        `json:"permissions"`
}

func Permisssions(logger mlogger.Logger,
	rolesDescs []model.RoleDescription, policiesDescs []model.PolicyDescription,
	roles []model.Role, policies []model.RolePolicy, permissions []model.Permission,
	accessToken *TokenData,
) http.HandlerFunc {
	return response.Ok(logger, permissionsResponse{
		Descriptions: permissionsDescription{
			Roles:    rolesDescs,
			Policies: policiesDescs,
		},
		Permissions: permissionsData{
			Roles:       roles,
			Policies:    policies,
			Permissions: permissions,
		},
		authResponse: authResponse{AccessToken: *accessToken},
	})
}
