package accoper

import (
	"context"
	"fmt"

	"mstore/pkg/descr"
)

// DeleteGrant
type DeleteGrantParams struct {
	GrantID string `json:"grantId"`
}
type DeleteGrantResult struct{}

func (oper *Operator) DeleteGrant(ctx context.Context, operatorID string, params *DeleteGrantParams) (*DeleteGrantResult, error) {
	var err error
	res := &DeleteGrantResult{}

	if params.GrantID == "" {
		err := fmt.Errorf("Empty grantId parameter")
		return res, err
	}

	var grantDescr *descr.Grant
	var grantExists bool

	grantExists, grantDescr, err = oper.mdb.GetGrantByID(ctx, params.GrantID)
	if err != nil {
		return res, err
	}
	if !grantExists {
		err := fmt.Errorf("Grant with ID %s dont exists", params.GrantID)
		return res, err
	}
	err = oper.mdb.DeleteGrantByID(ctx, grantDescr.ID)
	if err != nil {
		return res, err
	}
	return res, err
}
