package orchestrator

import (
	"context"
	"net"
	"testing"

	"github.com/tech/sendico/payments/orchestrator/internal/service/orchestrationv2/psvc"
	"github.com/tech/sendico/pkg/api/routers"
	orchestrationv2 "github.com/tech/sendico/pkg/proto/payments/orchestration/v2"
	"google.golang.org/grpc"
)

func TestService_RegisterV2Only(t *testing.T) {
	svc := &Service{v2: fakeOrchestrationV2Service{}}
	router := newGRPCCaptureRouterV2()

	if err := svc.Register(router); err != nil {
		t.Fatalf("Register returned error: %v", err)
	}

	services := router.server.GetServiceInfo()
	if _, ok := services[orchestrationv2.PaymentOrchestratorService_ServiceDesc.ServiceName]; !ok {
		t.Fatalf("expected %q service to be registered", orchestrationv2.PaymentOrchestratorService_ServiceDesc.ServiceName)
	}
	if len(services) != 1 {
		t.Fatalf("expected exactly one registered service, got %d", len(services))
	}
}

type fakeOrchestrationV2Service struct{}

func (fakeOrchestrationV2Service) ExecutePayment(context.Context, *orchestrationv2.ExecutePaymentRequest) (*orchestrationv2.ExecutePaymentResponse, error) {
	return &orchestrationv2.ExecutePaymentResponse{}, nil
}

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

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

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

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

type grpcCaptureRouterV2 struct {
	server *grpc.Server
	done   chan error
}

func newGRPCCaptureRouterV2() *grpcCaptureRouterV2 {
	return &grpcCaptureRouterV2{
		server: grpc.NewServer(),
		done:   make(chan error),
	}
}

func (r *grpcCaptureRouterV2) Register(registration routers.GRPCServiceRegistration) error {
	registration(r.server)
	return nil
}

func (r *grpcCaptureRouterV2) Start(context.Context) error { return nil }

func (r *grpcCaptureRouterV2) Finish(context.Context) error { return nil }

func (r *grpcCaptureRouterV2) Addr() net.Addr { return nil }

func (r *grpcCaptureRouterV2) Done() <-chan error { return r.done }

var _ routers.GRPC = (*grpcCaptureRouterV2)(nil)
