// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"database/sql/driver"
	"fmt"
	"math"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/cloudreve/Cloudreve/v4/ent/oauthclient"
	"github.com/cloudreve/Cloudreve/v4/ent/oauthgrant"
	"github.com/cloudreve/Cloudreve/v4/ent/predicate"
)

// OAuthClientQuery is the builder for querying OAuthClient entities.
type OAuthClientQuery struct {
	config
	ctx        *QueryContext
	order      []oauthclient.OrderOption
	inters     []Interceptor
	predicates []predicate.OAuthClient
	withGrants *OAuthGrantQuery
	// intermediate query (i.e. traversal path).
	sql  *sql.Selector
	path func(context.Context) (*sql.Selector, error)
}

// Where adds a new predicate for the OAuthClientQuery builder.
func (ocq *OAuthClientQuery) Where(ps ...predicate.OAuthClient) *OAuthClientQuery {
	ocq.predicates = append(ocq.predicates, ps...)
	return ocq
}

// Limit the number of records to be returned by this query.
func (ocq *OAuthClientQuery) Limit(limit int) *OAuthClientQuery {
	ocq.ctx.Limit = &limit
	return ocq
}

// Offset to start from.
func (ocq *OAuthClientQuery) Offset(offset int) *OAuthClientQuery {
	ocq.ctx.Offset = &offset
	return ocq
}

// 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 (ocq *OAuthClientQuery) Unique(unique bool) *OAuthClientQuery {
	ocq.ctx.Unique = &unique
	return ocq
}

// Order specifies how the records should be ordered.
func (ocq *OAuthClientQuery) Order(o ...oauthclient.OrderOption) *OAuthClientQuery {
	ocq.order = append(ocq.order, o...)
	return ocq
}

// QueryGrants chains the current query on the "grants" edge.
func (ocq *OAuthClientQuery) QueryGrants() *OAuthGrantQuery {
	query := (&OAuthGrantClient{config: ocq.config}).Query()
	query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
		if err := ocq.prepareQuery(ctx); err != nil {
			return nil, err
		}
		selector := ocq.sqlQuery(ctx)
		if err := selector.Err(); err != nil {
			return nil, err
		}
		step := sqlgraph.NewStep(
			sqlgraph.From(oauthclient.Table, oauthclient.FieldID, selector),
			sqlgraph.To(oauthgrant.Table, oauthgrant.FieldID),
			sqlgraph.Edge(sqlgraph.O2M, false, oauthclient.GrantsTable, oauthclient.GrantsColumn),
		)
		fromU = sqlgraph.SetNeighbors(ocq.driver.Dialect(), step)
		return fromU, nil
	}
	return query
}

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

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

// FirstID returns the first OAuthClient ID from the query.
// Returns a *NotFoundError when no OAuthClient ID was found.
func (ocq *OAuthClientQuery) FirstID(ctx context.Context) (id int, err error) {
	var ids []int
	if ids, err = ocq.Limit(1).IDs(setContextOp(ctx, ocq.ctx, "FirstID")); err != nil {
		return
	}
	if len(ids) == 0 {
		err = &NotFoundError{oauthclient.Label}
		return
	}
	return ids[0], nil
}

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

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

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

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

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

// All executes the query and returns a list of OAuthClients.
func (ocq *OAuthClientQuery) All(ctx context.Context) ([]*OAuthClient, error) {
	ctx = setContextOp(ctx, ocq.ctx, "All")
	if err := ocq.prepareQuery(ctx); err != nil {
		return nil, err
	}
	qr := querierAll[[]*OAuthClient, *OAuthClientQuery]()
	return withInterceptors[[]*OAuthClient](ctx, ocq, qr, ocq.inters)
}

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

// IDs executes the query and returns a list of OAuthClient IDs.
func (ocq *OAuthClientQuery) IDs(ctx context.Context) (ids []int, err error) {
	if ocq.ctx.Unique == nil && ocq.path != nil {
		ocq.Unique(true)
	}
	ctx = setContextOp(ctx, ocq.ctx, "IDs")
	if err = ocq.Select(oauthclient.FieldID).Scan(ctx, &ids); err != nil {
		return nil, err
	}
	return ids, nil
}

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

// Count returns the count of the given query.
func (ocq *OAuthClientQuery) Count(ctx context.Context) (int, error) {
	ctx = setContextOp(ctx, ocq.ctx, "Count")
	if err := ocq.prepareQuery(ctx); err != nil {
		return 0, err
	}
	return withInterceptors[int](ctx, ocq, querierCount[*OAuthClientQuery](), ocq.inters)
}

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

// Exist returns true if the query has elements in the graph.
func (ocq *OAuthClientQuery) Exist(ctx context.Context) (bool, error) {
	ctx = setContextOp(ctx, ocq.ctx, "Exist")
	switch _, err := ocq.FirstID(ctx); {
	case IsNotFound(err):
		return false, nil
	case err != nil:
		return false, fmt.Errorf("ent: check existence: %w", err)
	default:
		return true, nil
	}
}

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

// Clone returns a duplicate of the OAuthClientQuery builder, including all associated steps. It can be
// used to prepare common query builders and use them differently after the clone is made.
func (ocq *OAuthClientQuery) Clone() *OAuthClientQuery {
	if ocq == nil {
		return nil
	}
	return &OAuthClientQuery{
		config:     ocq.config,
		ctx:        ocq.ctx.Clone(),
		order:      append([]oauthclient.OrderOption{}, ocq.order...),
		inters:     append([]Interceptor{}, ocq.inters...),
		predicates: append([]predicate.OAuthClient{}, ocq.predicates...),
		withGrants: ocq.withGrants.Clone(),
		// clone intermediate query.
		sql:  ocq.sql.Clone(),
		path: ocq.path,
	}
}

// WithGrants tells the query-builder to eager-load the nodes that are connected to
// the "grants" edge. The optional arguments are used to configure the query builder of the edge.
func (ocq *OAuthClientQuery) WithGrants(opts ...func(*OAuthGrantQuery)) *OAuthClientQuery {
	query := (&OAuthGrantClient{config: ocq.config}).Query()
	for _, opt := range opts {
		opt(query)
	}
	ocq.withGrants = query
	return ocq
}

// 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.OAuthClient.Query().
//		GroupBy(oauthclient.FieldCreatedAt).
//		Aggregate(ent.Count()).
//		Scan(ctx, &v)
func (ocq *OAuthClientQuery) GroupBy(field string, fields ...string) *OAuthClientGroupBy {
	ocq.ctx.Fields = append([]string{field}, fields...)
	grbuild := &OAuthClientGroupBy{build: ocq}
	grbuild.flds = &ocq.ctx.Fields
	grbuild.label = oauthclient.Label
	grbuild.scan = 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.OAuthClient.Query().
//		Select(oauthclient.FieldCreatedAt).
//		Scan(ctx, &v)
func (ocq *OAuthClientQuery) Select(fields ...string) *OAuthClientSelect {
	ocq.ctx.Fields = append(ocq.ctx.Fields, fields...)
	sbuild := &OAuthClientSelect{OAuthClientQuery: ocq}
	sbuild.label = oauthclient.Label
	sbuild.flds, sbuild.scan = &ocq.ctx.Fields, sbuild.Scan
	return sbuild
}

// Aggregate returns a OAuthClientSelect configured with the given aggregations.
func (ocq *OAuthClientQuery) Aggregate(fns ...AggregateFunc) *OAuthClientSelect {
	return ocq.Select().Aggregate(fns...)
}

func (ocq *OAuthClientQuery) prepareQuery(ctx context.Context) error {
	for _, inter := range ocq.inters {
		if inter == nil {
			return fmt.Errorf("ent: uninitialized interceptor (forgotten import ent/runtime?)")
		}
		if trv, ok := inter.(Traverser); ok {
			if err := trv.Traverse(ctx, ocq); err != nil {
				return err
			}
		}
	}
	for _, f := range ocq.ctx.Fields {
		if !oauthclient.ValidColumn(f) {
			return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
		}
	}
	if ocq.path != nil {
		prev, err := ocq.path(ctx)
		if err != nil {
			return err
		}
		ocq.sql = prev
	}
	return nil
}

func (ocq *OAuthClientQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*OAuthClient, error) {
	var (
		nodes       = []*OAuthClient{}
		_spec       = ocq.querySpec()
		loadedTypes = [1]bool{
			ocq.withGrants != nil,
		}
	)
	_spec.ScanValues = func(columns []string) ([]any, error) {
		return (*OAuthClient).scanValues(nil, columns)
	}
	_spec.Assign = func(columns []string, values []any) error {
		node := &OAuthClient{config: ocq.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, ocq.driver, _spec); err != nil {
		return nil, err
	}
	if len(nodes) == 0 {
		return nodes, nil
	}
	if query := ocq.withGrants; query != nil {
		if err := ocq.loadGrants(ctx, query, nodes,
			func(n *OAuthClient) { n.Edges.Grants = []*OAuthGrant{} },
			func(n *OAuthClient, e *OAuthGrant) { n.Edges.Grants = append(n.Edges.Grants, e) }); err != nil {
			return nil, err
		}
	}
	return nodes, nil
}

func (ocq *OAuthClientQuery) loadGrants(ctx context.Context, query *OAuthGrantQuery, nodes []*OAuthClient, init func(*OAuthClient), assign func(*OAuthClient, *OAuthGrant)) error {
	fks := make([]driver.Value, 0, len(nodes))
	nodeids := make(map[int]*OAuthClient)
	for i := range nodes {
		fks = append(fks, nodes[i].ID)
		nodeids[nodes[i].ID] = nodes[i]
		if init != nil {
			init(nodes[i])
		}
	}
	if len(query.ctx.Fields) > 0 {
		query.ctx.AppendFieldOnce(oauthgrant.FieldClientID)
	}
	query.Where(predicate.OAuthGrant(func(s *sql.Selector) {
		s.Where(sql.InValues(s.C(oauthclient.GrantsColumn), fks...))
	}))
	neighbors, err := query.All(ctx)
	if err != nil {
		return err
	}
	for _, n := range neighbors {
		fk := n.ClientID
		node, ok := nodeids[fk]
		if !ok {
			return fmt.Errorf(`unexpected referenced foreign-key "client_id" returned %v for node %v`, fk, n.ID)
		}
		assign(node, n)
	}
	return nil
}

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

func (ocq *OAuthClientQuery) querySpec() *sqlgraph.QuerySpec {
	_spec := sqlgraph.NewQuerySpec(oauthclient.Table, oauthclient.Columns, sqlgraph.NewFieldSpec(oauthclient.FieldID, field.TypeInt))
	_spec.From = ocq.sql
	if unique := ocq.ctx.Unique; unique != nil {
		_spec.Unique = *unique
	} else if ocq.path != nil {
		_spec.Unique = true
	}
	if fields := ocq.ctx.Fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, oauthclient.FieldID)
		for i := range fields {
			if fields[i] != oauthclient.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, fields[i])
			}
		}
	}
	if ps := ocq.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if limit := ocq.ctx.Limit; limit != nil {
		_spec.Limit = *limit
	}
	if offset := ocq.ctx.Offset; offset != nil {
		_spec.Offset = *offset
	}
	if ps := ocq.order; len(ps) > 0 {
		_spec.Order = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	return _spec
}

func (ocq *OAuthClientQuery) sqlQuery(ctx context.Context) *sql.Selector {
	builder := sql.Dialect(ocq.driver.Dialect())
	t1 := builder.Table(oauthclient.Table)
	columns := ocq.ctx.Fields
	if len(columns) == 0 {
		columns = oauthclient.Columns
	}
	selector := builder.Select(t1.Columns(columns...)...).From(t1)
	if ocq.sql != nil {
		selector = ocq.sql
		selector.Select(selector.Columns(columns...)...)
	}
	if ocq.ctx.Unique != nil && *ocq.ctx.Unique {
		selector.Distinct()
	}
	for _, p := range ocq.predicates {
		p(selector)
	}
	for _, p := range ocq.order {
		p(selector)
	}
	if offset := ocq.ctx.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 := ocq.ctx.Limit; limit != nil {
		selector.Limit(*limit)
	}
	return selector
}

// OAuthClientGroupBy is the group-by builder for OAuthClient entities.
type OAuthClientGroupBy struct {
	selector
	build *OAuthClientQuery
}

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

// Scan applies the selector query and scans the result into the given value.
func (ocgb *OAuthClientGroupBy) Scan(ctx context.Context, v any) error {
	ctx = setContextOp(ctx, ocgb.build.ctx, "GroupBy")
	if err := ocgb.build.prepareQuery(ctx); err != nil {
		return err
	}
	return scanWithInterceptors[*OAuthClientQuery, *OAuthClientGroupBy](ctx, ocgb.build, ocgb, ocgb.build.inters, v)
}

func (ocgb *OAuthClientGroupBy) sqlScan(ctx context.Context, root *OAuthClientQuery, v any) error {
	selector := root.sqlQuery(ctx).Select()
	aggregation := make([]string, 0, len(ocgb.fns))
	for _, fn := range ocgb.fns {
		aggregation = append(aggregation, fn(selector))
	}
	if len(selector.SelectedColumns()) == 0 {
		columns := make([]string, 0, len(*ocgb.flds)+len(ocgb.fns))
		for _, f := range *ocgb.flds {
			columns = append(columns, selector.C(f))
		}
		columns = append(columns, aggregation...)
		selector.Select(columns...)
	}
	selector.GroupBy(selector.Columns(*ocgb.flds...)...)
	if err := selector.Err(); err != nil {
		return err
	}
	rows := &sql.Rows{}
	query, args := selector.Query()
	if err := ocgb.build.driver.Query(ctx, query, args, rows); err != nil {
		return err
	}
	defer rows.Close()
	return sql.ScanSlice(rows, v)
}

// OAuthClientSelect is the builder for selecting fields of OAuthClient entities.
type OAuthClientSelect struct {
	*OAuthClientQuery
	selector
}

// Aggregate adds the given aggregation functions to the selector query.
func (ocs *OAuthClientSelect) Aggregate(fns ...AggregateFunc) *OAuthClientSelect {
	ocs.fns = append(ocs.fns, fns...)
	return ocs
}

// Scan applies the selector query and scans the result into the given value.
func (ocs *OAuthClientSelect) Scan(ctx context.Context, v any) error {
	ctx = setContextOp(ctx, ocs.ctx, "Select")
	if err := ocs.prepareQuery(ctx); err != nil {
		return err
	}
	return scanWithInterceptors[*OAuthClientQuery, *OAuthClientSelect](ctx, ocs.OAuthClientQuery, ocs, ocs.inters, v)
}

func (ocs *OAuthClientSelect) sqlScan(ctx context.Context, root *OAuthClientQuery, v any) error {
	selector := root.sqlQuery(ctx)
	aggregation := make([]string, 0, len(ocs.fns))
	for _, fn := range ocs.fns {
		aggregation = append(aggregation, fn(selector))
	}
	switch n := len(*ocs.selector.flds); {
	case n == 0 && len(aggregation) > 0:
		selector.Select(aggregation...)
	case n != 0 && len(aggregation) > 0:
		selector.AppendSelect(aggregation...)
	}
	rows := &sql.Rows{}
	query, args := selector.Query()
	if err := ocs.driver.Query(ctx, query, args, rows); err != nil {
		return err
	}
	defer rows.Close()
	return sql.ScanSlice(rows, v)
}
