package accntcli

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

	"mstore/app/accoper"
	"mstore/app/handler"
	"mstore/pkg/descr"
)

func (cli *Client) ListAccounts(ctx context.Context, host string) ([]descr.AccountShort, error) {
	var err error
	res := make([]descr.AccountShort, 0)

	params := accoper.ListAccountsParams{}
	reqdata, err := json.Marshal(params)
	if err != nil {
		return res, err
	}
	resdata, err := cli.doHTTPCall(ctx, host, "accounts", "list", reqdata)
	if err != nil {
		return res, err
	}
	response := handler.NewResponse[accoper.ListAccountsResult]()
	err = json.Unmarshal(resdata, response)
	if err != nil {
		return res, err
	}
	if response.Error {
		err = errors.New(response.Message)
		return res, err
	}
	res = response.Result.Accounts
	return res, err
}
