package resolver

// This file will be automatically regenerated based on the schema, any resolver implementations
// will be copied through when generating and any unknown code will be moved to the end.
// Code generated by github.com/99designs/gqlgen

import (
	"context"

	"github.com/99designs/gqlgen/graphql"
	"github.com/cloudoperators/heureka/internal/api/graphql/graph"
	"github.com/cloudoperators/heureka/internal/api/graphql/graph/baseResolver"
	"github.com/cloudoperators/heureka/internal/api/graphql/graph/model"
	"github.com/samber/lo"
)

// SPDX-FileCopyrightText: 2025 SAP SE or an SAP affiliate company and Greenhouse contributors
// SPDX-License-Identifier: Apache-2.0

func (r *imageResolver) Versions(ctx context.Context, obj *model.Image, first *int, after *string) (*model.ComponentVersionConnection, error) {
	rootCtx := baseResolver.GetRoot(graphql.GetFieldContext(ctx))
	serviceFilter := rootCtx.Args["filter"].(*model.ImageFilter)
	filter := &model.ComponentVersionFilter{
		ServiceCcrn: serviceFilter.Service,
	}
	orderBy := []*model.ComponentVersionOrderBy{
		{
			By:        lo.ToPtr(model.ComponentVersionOrderByFieldSeverity),
			Direction: lo.ToPtr(model.OrderDirectionDesc),
		},
		{
			By:        lo.ToPtr(model.ComponentVersionOrderByFieldRepository),
			Direction: lo.ToPtr(model.OrderDirectionAsc),
		},
	}
	return baseResolver.ComponentVersionBaseResolver(r.App, ctx, filter, first, after, orderBy,
		&model.NodeParent{
			Parent:     obj,
			ParentName: model.ImageNodeName,
		})
}

func (r *imageResolver) VulnerabilityCounts(ctx context.Context, obj *model.Image) (*model.SeverityCounts, error) {
	rootCtx := baseResolver.GetRoot(graphql.GetFieldContext(ctx))
	serviceFilter := rootCtx.Args["filter"].(*model.ImageFilter)
	filter := &model.ComponentFilter{
		ServiceCcrn: serviceFilter.Service,
	}
	return baseResolver.ComponentIssueCountsBaseResolver(r.App, ctx, filter, &model.NodeParent{
		Parent:     obj,
		ParentName: model.ImageNodeName,
	})
}

func (r *imageResolver) Vulnerabilities(ctx context.Context, obj *model.Image, first *int, after *string) (*model.VulnerabilityConnection, error) {
	rootCtx := baseResolver.GetRoot(graphql.GetFieldContext(ctx))
	serviceFilter := rootCtx.Args["filter"].(*model.ImageFilter)
	vulnerabilityFilter := &model.VulnerabilityFilter{
		Service: serviceFilter.Service,
	}
	return baseResolver.VulnerabilityBaseResolver(r.App, ctx, vulnerabilityFilter, first, after, &model.NodeParent{
		Parent:     obj,
		ParentName: model.ImageNodeName,
	})
}

func (r *Resolver) Image() graph.ImageResolver { return &imageResolver{r} }

type imageResolver struct{ *Resolver }
