package client

import (
	"context"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"github.com/tech/sendico/pkg/discovery"
	"github.com/tech/sendico/pkg/payments/rail"
	accountrolev1 "github.com/tech/sendico/pkg/proto/common/account_role/v1"
	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 &connectorv1.OpenAccountResponse{}, nil
}

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

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

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

func (s *stubConnector) UpdateAccountState(context.Context, *connectorv1.UpdateAccountStateRequest, ...grpc.CallOption) (*connectorv1.UpdateAccountStateResponse, error) {
	return &connectorv1.UpdateAccountStateResponse{}, 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 &connectorv1.GetOperationResponse{}, nil
}

func (s *stubConnector) ListOperations(context.Context, *connectorv1.ListOperationsRequest, ...grpc.CallOption) (*connectorv1.ListOperationsResponse, error) {
	return &connectorv1.ListOperationsResponse{}, 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())
}

func TestPostExternalCreditWithCharges_SubmitsExternalOperation(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-ext-credit"}}, nil
		},
	}

	client := NewWithClient(Config{}, stub)
	resp, err := client.PostExternalCreditWithCharges(ctx, &ledgerv1.PostCreditRequest{
		IdempotencyKey:  "id-ext-credit",
		OrganizationRef: "org-1",
		Money:           &moneyv1.Money{Currency: "USDT", Amount: "1.0"},
		Role:            ledgerv1.AccountRole_ACCOUNT_ROLE_OPERATING,
	})

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

	assert.Equal(t, connectorv1.OperationType_CREDIT, captured.GetType())
	assert.Equal(t, "", captured.GetTo().GetAccount().GetAccountId())
	assert.Equal(t, accountrolev1.AccountRole_OPERATING, captured.GetToRole())
	assert.Equal(t, discovery.OperationExternalCredit, captured.GetParams().AsMap()["operation"])
	assert.Equal(t, "op-ext-credit", resp.GetJournalEntryRef())
	assert.Equal(t, ledgerv1.EntryType_ENTRY_CREDIT, resp.GetEntryType())
}

func TestPostExternalDebitWithCharges_SubmitsExternalOperation(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-ext-debit"}}, nil
		},
	}

	client := NewWithClient(Config{}, stub)
	resp, err := client.PostExternalDebitWithCharges(ctx, &ledgerv1.PostDebitRequest{
		IdempotencyKey:  "id-ext-debit",
		OrganizationRef: "org-1",
		Money:           &moneyv1.Money{Currency: "RUB", Amount: "77.14"},
		Role:            ledgerv1.AccountRole_ACCOUNT_ROLE_HOLD,
	})

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

	assert.Equal(t, connectorv1.OperationType_DEBIT, captured.GetType())
	assert.Equal(t, "", captured.GetFrom().GetAccount().GetAccountId())
	assert.Equal(t, accountrolev1.AccountRole_HOLD, captured.GetFromRole())
	assert.Equal(t, discovery.OperationExternalDebit, captured.GetParams().AsMap()["operation"])
	assert.Equal(t, "op-ext-debit", resp.GetJournalEntryRef())
	assert.Equal(t, ledgerv1.EntryType_ENTRY_DEBIT, resp.GetEntryType())
}

func TestCreateTransaction_NormalizesRailsForDirectionAndMetadata(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-tx"}}, nil
		},
	}

	client := NewWithClient(Config{}, stub)
	ref, err := client.CreateTransaction(ctx, rail.LedgerTx{
		IdempotencyKey:   "idem-tx",
		OrganizationRef:  "org-1",
		LedgerAccountRef: "acct-1",
		Currency:         "USD",
		Amount:           "10",
		FromRail:         "rail_ledger",
		ToRail:           "crypto",
	})

	require.NoError(t, err)
	require.Equal(t, "op-tx", ref)
	require.NotNil(t, captured)
	assert.Equal(t, connectorv1.OperationType_DEBIT, captured.GetType())

	metadata, ok := captured.GetParams().AsMap()["metadata"].(map[string]interface{})
	require.True(t, ok)
	assert.Equal(t, discovery.RailLedger, metadata["from_rail"])
	assert.Equal(t, discovery.RailCrypto, metadata["to_rail"])
}
