package accntcli

import (
	"context"
	"encoding/json"
	"errors"

	"mstore/app/accoper"
	"mstore/app/handler"
)

func (cli *Client) DeleteAccountByName(ctx context.Context, host, username string) error {
	var err error

	params := accoper.DeleteAccountParams{
		Username: username,
	}
	reqdata, err := json.Marshal(params)
	if err != nil {
		return err
	}
	resdata, err := cli.doHTTPCall(ctx, host, "account", "delete", reqdata)
	if err != nil {
		return err
	}
	response := handler.NewResponse[accoper.DeleteAccountResult]()
	err = json.Unmarshal(resdata, response)
	if err != nil {
		return err
	}
	if response.Error {
		err = errors.New(response.Message)
		return err
	}
	return err
}

func (cli *Client) DeleteAccountByID(ctx context.Context, host, accountID string) error {
	var err error

	params := accoper.DeleteAccountParams{
		AccountID: accountID,
	}
	reqdata, err := json.Marshal(params)
	if err != nil {
		return err
	}
	resdata, err := cli.doHTTPCall(ctx, host, "account", "delete", reqdata)
	if err != nil {
		return err
	}
	response := handler.NewResponse[accoper.DeleteAccountResult]()
	err = json.Unmarshal(resdata, response)
	if err != nil {
		return err
	}
	if response.Error {
		err = errors.New(response.Message)
		return err
	}
	return err
}
