package runtimegrpc

import (
	"context"
	semantic "github.com/pingcap-inc/nexuscrm-contract/pkg/runtime/applicationnavigationv1"
	"github.com/pingcap-inc/nexuscrm-contract/wire-go/mapping"
)

// ReadApplicationNavigation uses the admitted caller identity and current Runtime state.
func (c *RuntimePlatformClient) ReadApplicationNavigation(ctx context.Context, bearer string, request semantic.ReadRequest) (semantic.ReadResponse, error) {
	callCtx, cancel, err := c.authorizedAttempt(ctx, bearer, semantic.CapabilityName)
	if err != nil {
		return semantic.ReadResponse{}, err
	}
	defer cancel()
	wireRequest, err := mapping.ToWireNavigationReadRequest(callCtx, request)
	if err != nil {
		return semantic.ReadResponse{}, err
	}
	response, err := c.navigation.Read(c.dataContext(callCtx, semantic.CapabilityName), wireRequest, platformCallOptions()...)
	if err != nil {
		return semantic.ReadResponse{}, mapping.FromPlatformStatusError(err, semantic.CapabilityName)
	}
	return mapping.FromWireNavigationReadResponse(request, response)
}

// ResolveApplicationNavigation uses the admitted caller identity and current Runtime state.
func (c *RuntimePlatformClient) ResolveApplicationNavigation(ctx context.Context, bearer string, request semantic.ResolveRequest) (semantic.ResolveResponse, error) {
	callCtx, cancel, err := c.authorizedAttempt(ctx, bearer, semantic.CapabilityName)
	if err != nil {
		return semantic.ResolveResponse{}, err
	}
	defer cancel()
	wireRequest, err := mapping.ToWireNavigationResolveRequest(callCtx, request)
	if err != nil {
		return semantic.ResolveResponse{}, err
	}
	response, err := c.navigation.Resolve(c.dataContext(callCtx, semantic.CapabilityName), wireRequest, platformCallOptions()...)
	if err != nil {
		return semantic.ResolveResponse{}, mapping.FromPlatformStatusError(err, semantic.CapabilityName)
	}
	return mapping.FromWireNavigationResolveResponse(request, response)
}

// SaveApplicationNavigation uses the admitted caller identity and current Runtime state.
func (c *RuntimePlatformClient) SaveApplicationNavigation(ctx context.Context, bearer string, request semantic.SaveRequest) (semantic.SaveResponse, error) {
	callCtx, cancel, err := c.authorizedAttempt(ctx, bearer, semantic.CapabilityName)
	if err != nil {
		return semantic.SaveResponse{}, err
	}
	defer cancel()
	wireRequest, err := mapping.ToWireNavigationSaveRequest(callCtx, request)
	if err != nil {
		return semantic.SaveResponse{}, err
	}
	response, err := c.navigation.Save(c.dataContext(callCtx, semantic.CapabilityName), wireRequest, platformCallOptions()...)
	if err != nil {
		return semantic.SaveResponse{}, mapping.FromPlatformStatusError(err, semantic.CapabilityName)
	}
	return mapping.FromWireNavigationSaveResponse(request, response)
}
