package accntcli

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

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

func (cli *Client) CreateAccount(ctx context.Context, host, user, pass string) (string, error) {
	var err error
	var res string
	params := accoper.CreateAccountParams{
		Username: user,
		Password: pass,
	}
	reqdata, err := json.Marshal(params)
	if err != nil {
		return res, err
	}
	respdata, err := cli.doHTTPCall(ctx, host, "account", "create", reqdata)
	if err != nil {
		return res, err
	}
	response := handler.NewResponse[accoper.CreateAccountResult]()
	err = json.Unmarshal(respdata, response)
	if err != nil {
		return res, err
	}
	if response.Error {
		err = errors.New(response.Message)
		return res, err
	}
	res = response.Result.AccountID
	return res, err
}
