// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"fmt"
	"math"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/google/uuid"
	"github.com/hay-kot/homebox/backend/ent/group"
	"github.com/hay-kot/homebox/backend/ent/groupinvitationtoken"
	"github.com/hay-kot/homebox/backend/ent/predicate"
)

// GroupInvitationTokenQuery is the builder for querying GroupInvitationToken entities.
type GroupInvitationTokenQuery struct {
	config
	limit      *int
	offset     *int
	unique     *bool
	order      []OrderFunc
	fields     []string
	predicates []predicate.GroupInvitationToken
	withGroup  *GroupQuery
	withFKs    bool
	// intermediate query (i.e. traversal path).
	sql  *sql.Selector
	path func(context.Context) (*sql.Selector, error)
}

// Where adds a new predicate for the GroupInvitationTokenQuery builder.
func (gitq *GroupInvitationTokenQuery) Where(ps ...predicate.GroupInvitationToken) *GroupInvitationTokenQuery {
	gitq.predicates = append(gitq.predicates, ps...)
	return gitq
}

// Limit adds a limit step to the query.
func (gitq *GroupInvitationTokenQuery) Limit(limit int) *GroupInvitationTokenQuery {
	gitq.limit = &limit
	return gitq
}

// Offset adds an offset step to the query.
func (gitq *GroupInvitationTokenQuery) Offset(offset int) *GroupInvitationTokenQuery {
	gitq.offset = &offset
	return gitq
}

// Unique configures the query builder to filter duplicate records on query.
// By default, unique is set to true, and can be disabled using this method.
func (gitq *GroupInvitationTokenQuery) Unique(unique bool) *GroupInvitationTokenQuery {
	gitq.unique = &unique
	return gitq
}

// Order adds an order step to the query.
func (gitq *GroupInvitationTokenQuery) Order(o ...OrderFunc) *GroupInvitationTokenQuery {
	gitq.order = append(gitq.order, o...)
	return gitq
}

// QueryGroup chains the current query on the "group" edge.
func (gitq *GroupInvitationTokenQuery) QueryGroup() *GroupQuery {
	query := &GroupQuery{config: gitq.config}
	query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
		if err := gitq.prepareQuery(ctx); err != nil {
			return nil, err
		}
		selector := gitq.sqlQuery(ctx)
		if err := selector.Err(); err != nil {
			return nil, err
		}
		step := sqlgraph.NewStep(
			sqlgraph.From(groupinvitationtoken.Table, groupinvitationtoken.FieldID, selector),
			sqlgraph.To(group.Table, group.FieldID),
			sqlgraph.Edge(sqlgraph.M2O, true, groupinvitationtoken.GroupTable, groupinvitationtoken.GroupColumn),
		)
		fromU = sqlgraph.SetNeighbors(gitq.driver.Dialect(), step)
		return fromU, nil
	}
	return query
}

// First returns the first GroupInvitationToken entity from the query.
// Returns a *NotFoundError when no GroupInvitationToken was found.
func (gitq *GroupInvitationTokenQuery) First(ctx context.Context) (*GroupInvitationToken, error) {
	nodes, err := gitq.Limit(1).All(ctx)
	if err != nil {
		return nil, err
	}
	if len(nodes) == 0 {
		return nil, &NotFoundError{groupinvitationtoken.Label}
	}
	return nodes[0], nil
}

// FirstX is like First, but panics if an error occurs.
func (gitq *GroupInvitationTokenQuery) FirstX(ctx context.Context) *GroupInvitationToken {
	node, err := gitq.First(ctx)
	if err != nil && !IsNotFound(err) {
		panic(err)
	}
	return node
}

// FirstID returns the first GroupInvitationToken ID from the query.
// Returns a *NotFoundError when no GroupInvitationToken ID was found.
func (gitq *GroupInvitationTokenQuery) FirstID(ctx context.Context) (id uuid.UUID, err error) {
	var ids []uuid.UUID
	if ids, err = gitq.Limit(1).IDs(ctx); err != nil {
		return
	}
	if len(ids) == 0 {
		err = &NotFoundError{groupinvitationtoken.Label}
		return
	}
	return ids[0], nil
}

// FirstIDX is like FirstID, but panics if an error occurs.
func (gitq *GroupInvitationTokenQuery) FirstIDX(ctx context.Context) uuid.UUID {
	id, err := gitq.FirstID(ctx)
	if err != nil && !IsNotFound(err) {
		panic(err)
	}
	return id
}

// Only returns a single GroupInvitationToken entity found by the query, ensuring it only returns one.
// Returns a *NotSingularError when more than one GroupInvitationToken entity is found.
// Returns a *NotFoundError when no GroupInvitationToken entities are found.
func (gitq *GroupInvitationTokenQuery) Only(ctx context.Context) (*GroupInvitationToken, error) {
	nodes, err := gitq.Limit(2).All(ctx)
	if err != nil {
		return nil, err
	}
	switch len(nodes) {
	case 1:
		return nodes[0], nil
	case 0:
		return nil, &NotFoundError{groupinvitationtoken.Label}
	default:
		return nil, &NotSingularError{groupinvitationtoken.Label}
	}
}

// OnlyX is like Only, but panics if an error occurs.
func (gitq *GroupInvitationTokenQuery) OnlyX(ctx context.Context) *GroupInvitationToken {
	node, err := gitq.Only(ctx)
	if err != nil {
		panic(err)
	}
	return node
}

// OnlyID is like Only, but returns the only GroupInvitationToken ID in the query.
// Returns a *NotSingularError when more than one GroupInvitationToken ID is found.
// Returns a *NotFoundError when no entities are found.
func (gitq *GroupInvitationTokenQuery) OnlyID(ctx context.Context) (id uuid.UUID, err error) {
	var ids []uuid.UUID
	if ids, err = gitq.Limit(2).IDs(ctx); err != nil {
		return
	}
	switch len(ids) {
	case 1:
		id = ids[0]
	case 0:
		err = &NotFoundError{groupinvitationtoken.Label}
	default:
		err = &NotSingularError{groupinvitationtoken.Label}
	}
	return
}

// OnlyIDX is like OnlyID, but panics if an error occurs.
func (gitq *GroupInvitationTokenQuery) OnlyIDX(ctx context.Context) uuid.UUID {
	id, err := gitq.OnlyID(ctx)
	if err != nil {
		panic(err)
	}
	return id
}

// All executes the query and returns a list of GroupInvitationTokens.
func (gitq *GroupInvitationTokenQuery) All(ctx context.Context) ([]*GroupInvitationToken, error) {
	if err := gitq.prepareQuery(ctx); err != nil {
		return nil, err
	}
	return gitq.sqlAll(ctx)
}

// AllX is like All, but panics if an error occurs.
func (gitq *GroupInvitationTokenQuery) AllX(ctx context.Context) []*GroupInvitationToken {
	nodes, err := gitq.All(ctx)
	if err != nil {
		panic(err)
	}
	return nodes
}

// IDs executes the query and returns a list of GroupInvitationToken IDs.
func (gitq *GroupInvitationTokenQuery) IDs(ctx context.Context) ([]uuid.UUID, error) {
	var ids []uuid.UUID
	if err := gitq.Select(groupinvitationtoken.FieldID).Scan(ctx, &ids); err != nil {
		return nil, err
	}
	return ids, nil
}

// IDsX is like IDs, but panics if an error occurs.
func (gitq *GroupInvitationTokenQuery) IDsX(ctx context.Context) []uuid.UUID {
	ids, err := gitq.IDs(ctx)
	if err != nil {
		panic(err)
	}
	return ids
}

// Count returns the count of the given query.
func (gitq *GroupInvitationTokenQuery) Count(ctx context.Context) (int, error) {
	if err := gitq.prepareQuery(ctx); err != nil {
		return 0, err
	}
	return gitq.sqlCount(ctx)
}

// CountX is like Count, but panics if an error occurs.
func (gitq *GroupInvitationTokenQuery) CountX(ctx context.Context) int {
	count, err := gitq.Count(ctx)
	if err != nil {
		panic(err)
	}
	return count
}

// Exist returns true if the query has elements in the graph.
func (gitq *GroupInvitationTokenQuery) Exist(ctx context.Context) (bool, error) {
	if err := gitq.prepareQuery(ctx); err != nil {
		return false, err
	}
	return gitq.sqlExist(ctx)
}

// ExistX is like Exist, but panics if an error occurs.
func (gitq *GroupInvitationTokenQuery) ExistX(ctx context.Context) bool {
	exist, err := gitq.Exist(ctx)
	if err != nil {
		panic(err)
	}
	return exist
}

// Clone returns a duplicate of the GroupInvitationTokenQuery builder, including all associated steps. It can be
// used to prepare common query builders and use them differently after the clone is made.
func (gitq *GroupInvitationTokenQuery) Clone() *GroupInvitationTokenQuery {
	if gitq == nil {
		return nil
	}
	return &GroupInvitationTokenQuery{
		config:     gitq.config,
		limit:      gitq.limit,
		offset:     gitq.offset,
		order:      append([]OrderFunc{}, gitq.order...),
		predicates: append([]predicate.GroupInvitationToken{}, gitq.predicates...),
		withGroup:  gitq.withGroup.Clone(),
		// clone intermediate query.
		sql:    gitq.sql.Clone(),
		path:   gitq.path,
		unique: gitq.unique,
	}
}

// WithGroup tells the query-builder to eager-load the nodes that are connected to
// the "group" edge. The optional arguments are used to configure the query builder of the edge.
func (gitq *GroupInvitationTokenQuery) WithGroup(opts ...func(*GroupQuery)) *GroupInvitationTokenQuery {
	query := &GroupQuery{config: gitq.config}
	for _, opt := range opts {
		opt(query)
	}
	gitq.withGroup = query
	return gitq
}

// GroupBy is used to group vertices by one or more fields/columns.
// It is often used with aggregate functions, like: count, max, mean, min, sum.
//
// Example:
//
//	var v []struct {
//		CreatedAt time.Time `json:"created_at,omitempty"`
//		Count int `json:"count,omitempty"`
//	}
//
//	client.GroupInvitationToken.Query().
//		GroupBy(groupinvitationtoken.FieldCreatedAt).
//		Aggregate(ent.Count()).
//		Scan(ctx, &v)
func (gitq *GroupInvitationTokenQuery) GroupBy(field string, fields ...string) *GroupInvitationTokenGroupBy {
	grbuild := &GroupInvitationTokenGroupBy{config: gitq.config}
	grbuild.fields = append([]string{field}, fields...)
	grbuild.path = func(ctx context.Context) (prev *sql.Selector, err error) {
		if err := gitq.prepareQuery(ctx); err != nil {
			return nil, err
		}
		return gitq.sqlQuery(ctx), nil
	}
	grbuild.label = groupinvitationtoken.Label
	grbuild.flds, grbuild.scan = &grbuild.fields, grbuild.Scan
	return grbuild
}

// Select allows the selection one or more fields/columns for the given query,
// instead of selecting all fields in the entity.
//
// Example:
//
//	var v []struct {
//		CreatedAt time.Time `json:"created_at,omitempty"`
//	}
//
//	client.GroupInvitationToken.Query().
//		Select(groupinvitationtoken.FieldCreatedAt).
//		Scan(ctx, &v)
func (gitq *GroupInvitationTokenQuery) Select(fields ...string) *GroupInvitationTokenSelect {
	gitq.fields = append(gitq.fields, fields...)
	selbuild := &GroupInvitationTokenSelect{GroupInvitationTokenQuery: gitq}
	selbuild.label = groupinvitationtoken.Label
	selbuild.flds, selbuild.scan = &gitq.fields, selbuild.Scan
	return selbuild
}

func (gitq *GroupInvitationTokenQuery) prepareQuery(ctx context.Context) error {
	for _, f := range gitq.fields {
		if !groupinvitationtoken.ValidColumn(f) {
			return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
		}
	}
	if gitq.path != nil {
		prev, err := gitq.path(ctx)
		if err != nil {
			return err
		}
		gitq.sql = prev
	}
	return nil
}

func (gitq *GroupInvitationTokenQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*GroupInvitationToken, error) {
	var (
		nodes       = []*GroupInvitationToken{}
		withFKs     = gitq.withFKs
		_spec       = gitq.querySpec()
		loadedTypes = [1]bool{
			gitq.withGroup != nil,
		}
	)
	if gitq.withGroup != nil {
		withFKs = true
	}
	if withFKs {
		_spec.Node.Columns = append(_spec.Node.Columns, groupinvitationtoken.ForeignKeys...)
	}
	_spec.ScanValues = func(columns []string) ([]any, error) {
		return (*GroupInvitationToken).scanValues(nil, columns)
	}
	_spec.Assign = func(columns []string, values []any) error {
		node := &GroupInvitationToken{config: gitq.config}
		nodes = append(nodes, node)
		node.Edges.loadedTypes = loadedTypes
		return node.assignValues(columns, values)
	}
	for i := range hooks {
		hooks[i](ctx, _spec)
	}
	if err := sqlgraph.QueryNodes(ctx, gitq.driver, _spec); err != nil {
		return nil, err
	}
	if len(nodes) == 0 {
		return nodes, nil
	}
	if query := gitq.withGroup; query != nil {
		if err := gitq.loadGroup(ctx, query, nodes, nil,
			func(n *GroupInvitationToken, e *Group) { n.Edges.Group = e }); err != nil {
			return nil, err
		}
	}
	return nodes, nil
}

func (gitq *GroupInvitationTokenQuery) loadGroup(ctx context.Context, query *GroupQuery, nodes []*GroupInvitationToken, init func(*GroupInvitationToken), assign func(*GroupInvitationToken, *Group)) error {
	ids := make([]uuid.UUID, 0, len(nodes))
	nodeids := make(map[uuid.UUID][]*GroupInvitationToken)
	for i := range nodes {
		if nodes[i].group_invitation_tokens == nil {
			continue
		}
		fk := *nodes[i].group_invitation_tokens
		if _, ok := nodeids[fk]; !ok {
			ids = append(ids, fk)
		}
		nodeids[fk] = append(nodeids[fk], nodes[i])
	}
	query.Where(group.IDIn(ids...))
	neighbors, err := query.All(ctx)
	if err != nil {
		return err
	}
	for _, n := range neighbors {
		nodes, ok := nodeids[n.ID]
		if !ok {
			return fmt.Errorf(`unexpected foreign-key "group_invitation_tokens" returned %v`, n.ID)
		}
		for i := range nodes {
			assign(nodes[i], n)
		}
	}
	return nil
}

func (gitq *GroupInvitationTokenQuery) sqlCount(ctx context.Context) (int, error) {
	_spec := gitq.querySpec()
	_spec.Node.Columns = gitq.fields
	if len(gitq.fields) > 0 {
		_spec.Unique = gitq.unique != nil && *gitq.unique
	}
	return sqlgraph.CountNodes(ctx, gitq.driver, _spec)
}

func (gitq *GroupInvitationTokenQuery) sqlExist(ctx context.Context) (bool, error) {
	switch _, err := gitq.FirstID(ctx); {
	case IsNotFound(err):
		return false, nil
	case err != nil:
		return false, fmt.Errorf("ent: check existence: %w", err)
	default:
		return true, nil
	}
}

func (gitq *GroupInvitationTokenQuery) querySpec() *sqlgraph.QuerySpec {
	_spec := &sqlgraph.QuerySpec{
		Node: &sqlgraph.NodeSpec{
			Table:   groupinvitationtoken.Table,
			Columns: groupinvitationtoken.Columns,
			ID: &sqlgraph.FieldSpec{
				Type:   field.TypeUUID,
				Column: groupinvitationtoken.FieldID,
			},
		},
		From:   gitq.sql,
		Unique: true,
	}
	if unique := gitq.unique; unique != nil {
		_spec.Unique = *unique
	}
	if fields := gitq.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, groupinvitationtoken.FieldID)
		for i := range fields {
			if fields[i] != groupinvitationtoken.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, fields[i])
			}
		}
	}
	if ps := gitq.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if limit := gitq.limit; limit != nil {
		_spec.Limit = *limit
	}
	if offset := gitq.offset; offset != nil {
		_spec.Offset = *offset
	}
	if ps := gitq.order; len(ps) > 0 {
		_spec.Order = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	return _spec
}

func (gitq *GroupInvitationTokenQuery) sqlQuery(ctx context.Context) *sql.Selector {
	builder := sql.Dialect(gitq.driver.Dialect())
	t1 := builder.Table(groupinvitationtoken.Table)
	columns := gitq.fields
	if len(columns) == 0 {
		columns = groupinvitationtoken.Columns
	}
	selector := builder.Select(t1.Columns(columns...)...).From(t1)
	if gitq.sql != nil {
		selector = gitq.sql
		selector.Select(selector.Columns(columns...)...)
	}
	if gitq.unique != nil && *gitq.unique {
		selector.Distinct()
	}
	for _, p := range gitq.predicates {
		p(selector)
	}
	for _, p := range gitq.order {
		p(selector)
	}
	if offset := gitq.offset; offset != nil {
		// limit is mandatory for offset clause. We start
		// with default value, and override it below if needed.
		selector.Offset(*offset).Limit(math.MaxInt32)
	}
	if limit := gitq.limit; limit != nil {
		selector.Limit(*limit)
	}
	return selector
}

// GroupInvitationTokenGroupBy is the group-by builder for GroupInvitationToken entities.
type GroupInvitationTokenGroupBy struct {
	config
	selector
	fields []string
	fns    []AggregateFunc
	// intermediate query (i.e. traversal path).
	sql  *sql.Selector
	path func(context.Context) (*sql.Selector, error)
}

// Aggregate adds the given aggregation functions to the group-by query.
func (gitgb *GroupInvitationTokenGroupBy) Aggregate(fns ...AggregateFunc) *GroupInvitationTokenGroupBy {
	gitgb.fns = append(gitgb.fns, fns...)
	return gitgb
}

// Scan applies the group-by query and scans the result into the given value.
func (gitgb *GroupInvitationTokenGroupBy) Scan(ctx context.Context, v any) error {
	query, err := gitgb.path(ctx)
	if err != nil {
		return err
	}
	gitgb.sql = query
	return gitgb.sqlScan(ctx, v)
}

func (gitgb *GroupInvitationTokenGroupBy) sqlScan(ctx context.Context, v any) error {
	for _, f := range gitgb.fields {
		if !groupinvitationtoken.ValidColumn(f) {
			return &ValidationError{Name: f, err: fmt.Errorf("invalid field %q for group-by", f)}
		}
	}
	selector := gitgb.sqlQuery()
	if err := selector.Err(); err != nil {
		return err
	}
	rows := &sql.Rows{}
	query, args := selector.Query()
	if err := gitgb.driver.Query(ctx, query, args, rows); err != nil {
		return err
	}
	defer rows.Close()
	return sql.ScanSlice(rows, v)
}

func (gitgb *GroupInvitationTokenGroupBy) sqlQuery() *sql.Selector {
	selector := gitgb.sql.Select()
	aggregation := make([]string, 0, len(gitgb.fns))
	for _, fn := range gitgb.fns {
		aggregation = append(aggregation, fn(selector))
	}
	// If no columns were selected in a custom aggregation function, the default
	// selection is the fields used for "group-by", and the aggregation functions.
	if len(selector.SelectedColumns()) == 0 {
		columns := make([]string, 0, len(gitgb.fields)+len(gitgb.fns))
		for _, f := range gitgb.fields {
			columns = append(columns, selector.C(f))
		}
		columns = append(columns, aggregation...)
		selector.Select(columns...)
	}
	return selector.GroupBy(selector.Columns(gitgb.fields...)...)
}

// GroupInvitationTokenSelect is the builder for selecting fields of GroupInvitationToken entities.
type GroupInvitationTokenSelect struct {
	*GroupInvitationTokenQuery
	selector
	// intermediate query (i.e. traversal path).
	sql *sql.Selector
}

// Scan applies the selector query and scans the result into the given value.
func (gits *GroupInvitationTokenSelect) Scan(ctx context.Context, v any) error {
	if err := gits.prepareQuery(ctx); err != nil {
		return err
	}
	gits.sql = gits.GroupInvitationTokenQuery.sqlQuery(ctx)
	return gits.sqlScan(ctx, v)
}

func (gits *GroupInvitationTokenSelect) sqlScan(ctx context.Context, v any) error {
	rows := &sql.Rows{}
	query, args := gits.sql.Query()
	if err := gits.driver.Query(ctx, query, args, rows); err != nil {
		return err
	}
	defer rows.Close()
	return sql.ScanSlice(rows, v)
}