/*
 * Copyright 2026 Oleg Borodin <onborodin@gmail.com>
 */
package accountcmd

import (
	"context"
	"strings"
	"time"

	"github.com/spf13/cobra"

	"mstore/pkg/accntcli"
)

// DeleteGrant
type DeleteGrantParams struct {
	GrantID string
}

type DeleteGrantResult struct{}

func (util *GrantUtil) DeleteGrant(cmd *cobra.Command, args []string) {
	util.commonGrantParams.Hostname = args[0]
	util.deleteGrantParams.GrantID = args[1]
	res, err := util.deleteGrant(&util.commonGrantParams, &util.deleteGrantParams)
	printResponse(res, err)
}
func (util *GrantUtil) deleteGrant(common *CommonGrantParams, params *DeleteGrantParams) (*DeleteGrantResult, error) {
	var err error
	res := &DeleteGrantResult{}

	timeout := time.Duration(common.Timeout) * time.Second
	ctx, _ := context.WithTimeout(context.Background(), timeout)
	ref, err := accntcli.ParseHostinfo(common.Hostname)
	if err != nil {
		return res, err
	}
	ref.SetUserinfo(common.Username, common.Password)
	mw := accntcli.NewBasicAuthMiddleware(ref.Userinfo())
	cli := accntcli.NewClient(nil, mw)

	id := strings.ToLower(params.GrantID)
	err = cli.DeleteGrant(ctx, ref.Host(), id)
	if err != nil {
		return res, err
	}
	return res, err
}
