package client

import (
	"context"
	"testing"

	"github.com/stretchr/testify/require"
	connectorv1 "github.com/tech/sendico/pkg/proto/connector/v1"
	chainv1 "github.com/tech/sendico/pkg/proto/gateway/chain/v1"
	"google.golang.org/grpc"
)

type stubConnectorClient struct {
	listReq *connectorv1.ListAccountsRequest
}

func (s *stubConnectorClient) GetCapabilities(ctx context.Context, in *connectorv1.GetCapabilitiesRequest, opts ...grpc.CallOption) (*connectorv1.GetCapabilitiesResponse, error) {
	return &connectorv1.GetCapabilitiesResponse{}, nil
}

func (s *stubConnectorClient) OpenAccount(ctx context.Context, in *connectorv1.OpenAccountRequest, opts ...grpc.CallOption) (*connectorv1.OpenAccountResponse, error) {
	return &connectorv1.OpenAccountResponse{}, nil
}

func (s *stubConnectorClient) GetAccount(ctx context.Context, in *connectorv1.GetAccountRequest, opts ...grpc.CallOption) (*connectorv1.GetAccountResponse, error) {
	return &connectorv1.GetAccountResponse{}, nil
}

func (s *stubConnectorClient) ListAccounts(ctx context.Context, in *connectorv1.ListAccountsRequest, opts ...grpc.CallOption) (*connectorv1.ListAccountsResponse, error) {
	s.listReq = in
	return &connectorv1.ListAccountsResponse{}, nil
}

func (s *stubConnectorClient) GetBalance(ctx context.Context, in *connectorv1.GetBalanceRequest, opts ...grpc.CallOption) (*connectorv1.GetBalanceResponse, error) {
	return &connectorv1.GetBalanceResponse{}, nil
}

func (s *stubConnectorClient) SubmitOperation(ctx context.Context, in *connectorv1.SubmitOperationRequest, opts ...grpc.CallOption) (*connectorv1.SubmitOperationResponse, error) {
	return &connectorv1.SubmitOperationResponse{}, nil
}

func (s *stubConnectorClient) GetOperation(ctx context.Context, in *connectorv1.GetOperationRequest, opts ...grpc.CallOption) (*connectorv1.GetOperationResponse, error) {
	return &connectorv1.GetOperationResponse{}, nil
}

func (s *stubConnectorClient) ListOperations(ctx context.Context, in *connectorv1.ListOperationsRequest, opts ...grpc.CallOption) (*connectorv1.ListOperationsResponse, error) {
	return &connectorv1.ListOperationsResponse{}, nil
}

func TestListManagedWallets_ForwardsOrganizationRef(t *testing.T) {
	stub := &stubConnectorClient{}
	client := NewWithClient(Config{}, stub)

	_, err := client.ListManagedWallets(context.Background(), &chainv1.ListManagedWalletsRequest{
		OrganizationRef: "org-1",
		OwnerRef:        "owner-1",
		Asset: &chainv1.Asset{
			Chain:       chainv1.ChainNetwork_CHAIN_NETWORK_ETHEREUM_MAINNET,
			TokenSymbol: "USDC",
		},
	})
	require.NoError(t, err)
	require.NotNil(t, stub.listReq)
	require.Equal(t, "org-1", stub.listReq.GetOrganizationRef())
	require.Equal(t, "owner-1", stub.listReq.GetOwnerRef())
	require.Equal(t, connectorv1.AccountKind_CHAIN_MANAGED_WALLET, stub.listReq.GetKind())
}
