// Code generated by candi v1.8.18.

package graphqlhandler

import (
	"context"

	"monorepo/services/master-service/internal/modules/apps/domain"
	shareddomain "monorepo/services/master-service/pkg/shared/domain"

	"pkg.agungdp.dev/candi/candishared"
	"pkg.agungdp.dev/candi/tracer"
)

type queryResolver struct {
	root *GraphQLHandler
}

// GetAll resolver
func (q *queryResolver) GetAll(ctx context.Context, input struct{ Filter *CommonFilter }) (results AppListResolver, err error) {
	trace := tracer.StartTrace(ctx, "AppsDeliveryGraphQL:GetAll")
	defer trace.Finish()
	ctx = trace.Context()

	if input.Filter == nil {
		input.Filter = new(CommonFilter)
	}
	filter := input.Filter.ToSharedFilter()
	data, meta, err := q.root.uc.FindAll(ctx, filter)
	if err != nil {
		return results, err
	}

	return AppListResolver{
		Meta: meta, Data: data,
	}, nil
}

// GetMePermissions resolver
func (q *queryResolver) GetMePermissions(ctx context.Context, input struct{ AppsCode string }) (results []shareddomain.Permission, err error) {
	trace := tracer.StartTrace(ctx, "AppsDeliveryGraphQL:GetMePermissions")
	defer trace.Finish()
	ctx = trace.Context()

	tokenClaim := candishared.ParseTokenClaimFromContext(ctx)
	return q.root.uc.GetAllUserPermissions(ctx, input.AppsCode, tokenClaim.Subject)
}

// GetAllMeApps resolver
func (q *queryResolver) GetAllMeApps(ctx context.Context) (data []domain.UserApps, err error) {
	trace := tracer.StartTrace(ctx, "AppsDeliveryGraphQL:GetAllMeApps")
	defer trace.Finish()
	ctx = trace.Context()

	tokenClaim := candishared.ParseTokenClaimFromContext(ctx)
	return q.root.uc.GetUserApps(ctx, tokenClaim.Subject)
}

// GetDetail resolver
func (q *queryResolver) GetDetail(ctx context.Context, input struct{ Code string }) (data domain.AppDetail, err error) {
	trace := tracer.StartTrace(ctx, "AppsDeliveryGraphQL:GetDetail")
	defer trace.Finish()
	ctx = trace.Context()

	return q.root.uc.GetDetailApp(ctx, input.Code)
}
