package client

import (
	"context"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	moneyv1 "github.com/tech/sendico/pkg/proto/common/money/v1"
	connectorv1 "github.com/tech/sendico/pkg/proto/connector/v1"
	ledgerv1 "github.com/tech/sendico/pkg/proto/ledger/v1"
	"google.golang.org/grpc"
)

type stubConnector struct {
	submitFn func(ctx context.Context, req *connectorv1.SubmitOperationRequest) (*connectorv1.SubmitOperationResponse, error)
}

func (s *stubConnector) OpenAccount(context.Context, *connectorv1.OpenAccountRequest, ...grpc.CallOption) (*connectorv1.OpenAccountResponse, error) {
	return nil, nil
}

func (s *stubConnector) GetAccount(context.Context, *connectorv1.GetAccountRequest, ...grpc.CallOption) (*connectorv1.GetAccountResponse, error) {
	return nil, nil
}

func (s *stubConnector) ListAccounts(context.Context, *connectorv1.ListAccountsRequest, ...grpc.CallOption) (*connectorv1.ListAccountsResponse, error) {
	return nil, nil
}

func (s *stubConnector) GetBalance(context.Context, *connectorv1.GetBalanceRequest, ...grpc.CallOption) (*connectorv1.GetBalanceResponse, error) {
	return nil, nil
}

func (s *stubConnector) UpdateAccountState(context.Context, *connectorv1.UpdateAccountStateRequest, ...grpc.CallOption) (*connectorv1.UpdateAccountStateResponse, error) {
	return nil, nil
}

func (s *stubConnector) SubmitOperation(ctx context.Context, req *connectorv1.SubmitOperationRequest, _ ...grpc.CallOption) (*connectorv1.SubmitOperationResponse, error) {
	if s.submitFn != nil {
		return s.submitFn(ctx, req)
	}
	return &connectorv1.SubmitOperationResponse{Receipt: &connectorv1.OperationReceipt{OperationId: "op-1"}}, nil
}

func (s *stubConnector) GetOperation(context.Context, *connectorv1.GetOperationRequest, ...grpc.CallOption) (*connectorv1.GetOperationResponse, error) {
	return nil, nil
}

func (s *stubConnector) ListOperations(context.Context, *connectorv1.ListOperationsRequest, ...grpc.CallOption) (*connectorv1.ListOperationsResponse, error) {
	return nil, nil
}

func TestTransferInternal_SubmitsTransferOperation(t *testing.T) {
	ctx := context.Background()

	var captured *connectorv1.Operation
	stub := &stubConnector{
		submitFn: func(ctx context.Context, req *connectorv1.SubmitOperationRequest) (*connectorv1.SubmitOperationResponse, error) {
			captured = req.GetOperation()
			return &connectorv1.SubmitOperationResponse{Receipt: &connectorv1.OperationReceipt{OperationId: "op-1"}}, nil
		},
	}

	client := NewWithClient(Config{}, stub)
	resp, err := client.TransferInternal(ctx, &ledgerv1.TransferRequest{
		IdempotencyKey:       "id-1",
		OrganizationRef:      "org-1",
		FromLedgerAccountRef: "acct-from",
		ToLedgerAccountRef:   "acct-to",
		Money:                &moneyv1.Money{Currency: "USD", Amount: "10"},
		Description:          "hold",
	})

	require.NoError(t, err)
	require.NotNil(t, resp)
	require.NotNil(t, captured)

	assert.Equal(t, connectorv1.OperationType_TRANSFER, captured.GetType())
	assert.Equal(t, "id-1", captured.GetIdempotencyKey())
	assert.Equal(t, "acct-from", captured.GetFrom().GetAccount().GetAccountId())
	assert.Equal(t, "acct-to", captured.GetTo().GetAccount().GetAccountId())
	assert.Equal(t, ledgerConnectorID, captured.GetFrom().GetAccount().GetConnectorId())
	assert.Equal(t, ledgerConnectorID, captured.GetTo().GetAccount().GetConnectorId())
	assert.Equal(t, "USD", captured.GetMoney().GetCurrency())
	assert.Equal(t, "10", captured.GetMoney().GetAmount())

	params := captured.GetParams().AsMap()
	assert.Equal(t, "org-1", params["organization_ref"])
	assert.Equal(t, "hold", params["description"])

	assert.Equal(t, "op-1", resp.GetJournalEntryRef())
	assert.Equal(t, ledgerv1.EntryType_ENTRY_TRANSFER, resp.GetEntryType())
}
