package graph

// 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 version v0.17.45

import (
	"context"
	"log"

	"github.com/99designs/gqlgen/graphql"
	"github.com/Balun-courses/microservices_like_in_bigtech/lecture_4graphql/graph/model"
)

// Person is the resolver for the person field.
func (r *queryResolver) Person(ctx context.Context) ([]*model.Person, error) {
	requestedFields := graphql.CollectAllFields(ctx)

	var shouldSearchPets bool
	for i := range requestedFields {
		switch requestedFields[i] {
		case "pet":
			shouldSearchPets = true
		}
	}

	var result []*model.Person

	Adam := &model.Person{
		ID:   1,
		Name: "Adam",
	}
	if shouldSearchPets {
		log.Println("search pets")
		Adam.Pet = &model.Pet{
			ID:   1,
			Name: "Stew",
		}
	}

	result = append(result, Adam)
	return result, nil
}

// Pet is the resolver for the pet field.
func (r *queryResolver) Pet(ctx context.Context) ([]*model.Pet, error) {
	var result []*model.Pet

	Stew := &model.Pet{
		ID:   1,
		Name: "Stew",
	}

	result = append(result, Stew)
	return result, nil
}

// PersonByID is the resolver for the person_by_id field.
func (r *queryResolver) PersonByID(ctx context.Context, id int) (*model.Person, error) {
	requestedFields := graphql.CollectAllFields(ctx)

	var shouldSearchPets bool
	for i := range requestedFields {
		switch requestedFields[i] {
		case "pet":
			shouldSearchPets = true
		}
	}

	p := &model.Person{
		ID:   id,
		Name: "Adam",
	}

	if shouldSearchPets {
		log.Println("search pets")
		p.Pet = &model.Pet{
			ID:   1,
			Name: "Stew",
		}
	}

	return p, nil
}

// PetByID is the resolver for the pet_by_id field.
func (r *queryResolver) PetByID(ctx context.Context, id int) (*model.Pet, error) {
	return &model.Pet{
		ID:   id,
		Name: "Stew",
	}, nil
}

// Query returns QueryResolver implementation.
func (r *Resolver) Query() QueryResolver { return &queryResolver{r} }

type queryResolver struct{ *Resolver }
