package queries

import (
	"context"

	"git.aiterp.net/rpdata/api/graphql/resolver/types"
	"git.aiterp.net/rpdata/api/model/character"
)

// CharactersArgs is args for characters query
type CharactersArgs struct {
	Filter *struct {
		IDs    *[]string
		Nicks  *[]string
		Names  *[]string
		Author *string
		Search *string
		Logged *bool
	}
}

// Characters resolves the characters query
func (r *QueryResolver) Characters(ctx context.Context, args *CharactersArgs) ([]*types.CharacterResolver, error) {
	var characters []character.Character
	var err error

	filter := args.Filter
	if filter != nil {
		var ids []string
		var nicks []string
		var names []string

		if filter.IDs != nil {
			ids = *filter.IDs
		}
		if filter.Nicks != nil {
			nicks = *filter.Nicks
		}
		if filter.Names != nil {
			names = *filter.Names
		}

		characters, err = character.ListFilter(ids, nicks, names, filter.Author, filter.Search, filter.Logged)
		if err != nil {
			return nil, err
		}
	} else {
		characters, err = character.List()
		if err != nil {
			return nil, err
		}
	}

	resolvers := make([]*types.CharacterResolver, 0, len(characters))
	for _, character := range characters {
		resolvers = append(resolvers, &types.CharacterResolver{C: character})
	}

	return resolvers, nil
}