package compatibility_test

import (
	"context"
	"testing"
	"time"

	semantic "github.com/pingcap-inc/nexuscrm-contract/pkg/runtime/layoutauthoringv1"
	runtimev1 "github.com/pingcap-inc/nexuscrm-contract/wire-go/gen/runtime/v1"
	"google.golang.org/grpc"
)

func TestGeneratedLayoutAuthoringClientUsesFourUnaryRPCs(t *testing.T) {
	service := &layoutAuthoringTransportService{}
	connection, cleanup := startBufconnServer(t, func(server *grpc.Server) {
		runtimev1.RegisterLayoutAuthoringServiceServer(server, service)
	})
	defer cleanup()
	client := runtimev1.NewLayoutAuthoringServiceClient(connection)
	ctx, cancel := context.WithTimeout(withCapability(context.Background(), semantic.CapabilityName), time.Second)
	defer cancel()

	submit, err := client.Submit(ctx, &runtimev1.LayoutAuthoringServiceSubmitRequest{})
	if err != nil || !submit.GetDuplicate() {
		t.Fatalf("submit = %#v, %v", submit, err)
	}
	status, err := client.GetCommand(ctx, &runtimev1.LayoutAuthoringServiceGetCommandRequest{})
	if err != nil || status.GetState() != runtimev1.LayoutAuthoringCommandState_LAYOUT_AUTHORING_COMMAND_STATE_ACCEPTED {
		t.Fatalf("status = %#v, %v", status, err)
	}
	workspace, err := client.ReadWorkspace(ctx, &runtimev1.LayoutAuthoringServiceReadWorkspaceRequest{})
	if err != nil || workspace.GetObjectId() != "Account" {
		t.Fatalf("workspace = %#v, %v", workspace, err)
	}
	stored, err := client.ReadStoredLayout(ctx, &runtimev1.LayoutAuthoringServiceReadStoredLayoutRequest{})
	if err != nil || stored.GetDocument().GetLayoutId() != "layout-main" {
		t.Fatalf("stored = %#v, %v", stored, err)
	}
	if service.submitCalls != 1 || service.statusCalls != 1 || service.workspaceCalls != 1 || service.storedCalls != 1 {
		t.Fatalf("calls submit/status/workspace/stored = %d/%d/%d/%d", service.submitCalls, service.statusCalls, service.workspaceCalls, service.storedCalls)
	}
}

type layoutAuthoringTransportService struct {
	runtimev1.UnimplementedLayoutAuthoringServiceServer
	submitCalls    int
	statusCalls    int
	workspaceCalls int
	storedCalls    int
}

func (service *layoutAuthoringTransportService) Submit(context.Context, *runtimev1.LayoutAuthoringServiceSubmitRequest) (*runtimev1.LayoutAuthoringServiceSubmitResponse, error) {
	service.submitCalls++
	return &runtimev1.LayoutAuthoringServiceSubmitResponse{Duplicate: true}, nil
}

func (service *layoutAuthoringTransportService) GetCommand(context.Context, *runtimev1.LayoutAuthoringServiceGetCommandRequest) (*runtimev1.LayoutAuthoringServiceGetCommandResponse, error) {
	service.statusCalls++
	return &runtimev1.LayoutAuthoringServiceGetCommandResponse{State: runtimev1.LayoutAuthoringCommandState_LAYOUT_AUTHORING_COMMAND_STATE_ACCEPTED}, nil
}

func (service *layoutAuthoringTransportService) ReadWorkspace(context.Context, *runtimev1.LayoutAuthoringServiceReadWorkspaceRequest) (*runtimev1.LayoutAuthoringServiceReadWorkspaceResponse, error) {
	service.workspaceCalls++
	return &runtimev1.LayoutAuthoringServiceReadWorkspaceResponse{ObjectId: "Account"}, nil
}

func (service *layoutAuthoringTransportService) ReadStoredLayout(context.Context, *runtimev1.LayoutAuthoringServiceReadStoredLayoutRequest) (*runtimev1.LayoutAuthoringServiceReadStoredLayoutResponse, error) {
	service.storedCalls++
	return &runtimev1.LayoutAuthoringServiceReadStoredLayoutResponse{Document: &runtimev1.LayoutAuthoringDocument{LayoutId: "layout-main"}}, nil
}
