package orchestrator

import (
	"context"
	"strings"
	"testing"

	"github.com/tech/sendico/payments/orchestrator/internal/service/orchestrationv2/psvc"
	"github.com/tech/sendico/pkg/merrors"
	orchestrationv2 "github.com/tech/sendico/pkg/proto/payments/orchestration/v2"
	"go.uber.org/zap"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
)

func TestV2GRPCServerExecutePayment_MapsInvalidArgument(t *testing.T) {
	srv := newV2GRPCServer(fakeV2Service{
		executeErr: merrors.InvalidArgument("batch quotation is not supported for ExecutePayment; use ExecuteBatchPayment"),
	}, zap.NewNop())

	_, err := srv.ExecutePayment(context.Background(), &orchestrationv2.ExecutePaymentRequest{})
	if err == nil {
		t.Fatal("expected error")
	}
	if got, want := status.Code(err), codes.InvalidArgument; got != want {
		t.Fatalf("unexpected grpc status code: got=%s want=%s", got, want)
	}
	if got := status.Convert(err).Message(); !strings.Contains(got, "batch quotation is not supported for ExecutePayment; use ExecuteBatchPayment") {
		t.Fatalf("unexpected grpc status message: %q", got)
	}
}

type fakeV2Service struct {
	executeErr error
}

func (f fakeV2Service) ExecutePayment(context.Context, *orchestrationv2.ExecutePaymentRequest) (*orchestrationv2.ExecutePaymentResponse, error) {
	return nil, f.executeErr
}

func (fakeV2Service) GetPayment(context.Context, *orchestrationv2.GetPaymentRequest) (*orchestrationv2.GetPaymentResponse, error) {
	return &orchestrationv2.GetPaymentResponse{}, nil
}

func (fakeV2Service) ListPayments(context.Context, *orchestrationv2.ListPaymentsRequest) (*orchestrationv2.ListPaymentsResponse, error) {
	return &orchestrationv2.ListPaymentsResponse{}, nil
}

func (fakeV2Service) ReconcileExternal(context.Context, psvc.ReconcileExternalInput) (*psvc.ReconcileExternalOutput, error) {
	return &psvc.ReconcileExternalOutput{}, nil
}

func (fakeV2Service) ExecuteBatchPayment(ctx context.Context, req *orchestrationv2.ExecuteBatchPaymentRequest) (*orchestrationv2.ExecuteBatchPaymentResponse, error) {
	return &orchestrationv2.ExecuteBatchPaymentResponse{}, nil
}
