{{/*
Copyright 2019-present Facebook Inc. All rights reserved.
This source code is licensed under the Apache 2.0 license found
in the LICENSE file in the root directory of this source tree.
*/}}

{{ define "query" }}
{{ $pkg := base $.Config.Package }}

{{ template "header" $ }}

{{ template "import" $ }}

{{ $builder := print (pascal $.Name) "Query" }}
{{ $receiver := receiver $builder }}

// {{ $builder }} is the builder for querying {{ pascal $.Name }} entities.
type {{ $builder }} struct {
	config
	limit		*int
	offset		*int
	order		[]Order
	unique		[]string
	predicates 	[]predicate.{{ $.Name }}
	// intermediate queries.
	{{- range $_, $storage := $.Storage }}
		{{ $storage }} {{ $storage.Builder}}
	{{- end }}
}

// Where adds a new predicate for the builder.
func ({{ $receiver }} *{{ $builder }}) Where(ps ...predicate.{{ $.Name }}) *{{ $builder }} {
	{{ $receiver}}.predicates = append({{ $receiver }}.predicates, ps...)
	return {{ $receiver }}
}

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

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

// Order adds an order step to the query.
func ({{ $receiver }} *{{ $builder }}) Order(o ...Order) *{{ $builder }} {
	{{ $receiver }}.order = append({{ $receiver }}.order, o...)
	return {{ $receiver }}
}

{{/* this code has similarity with edge queries in client.tmpl */}}
{{ range $_, $e := $.Edges }}
	{{ $edge_builder := print (pascal $e.Type.Name) "Query" }}
	// Query{{ pascal $e.Name }} chains the current query on the {{ $e.Name }} edge.
	func ({{ $receiver }} *{{ $builder }}) Query{{ pascal $e.Name }}() *{{ $edge_builder }} {
		query := &{{ $edge_builder }}{config: {{ $receiver }}.config}
		{{- if $.MultiStorage }}
			switch {{ $receiver }}.driver.Dialect() {
			{{- range $_, $storage := $.Storage -}}
			case {{ join $storage.Dialects ", " }}:
				{{- with extend $ "Receiver" $receiver "Edge" $e -}}
					{{ $tmpl := printf "dialect/%s/query/path" $storage }}
					{{- xtemplate $tmpl . -}}
				{{ end -}}
			{{ end -}}
			}
		{{ else }}
			{{- with extend $ "Receiver" $receiver "Edge" $e -}}
				{{ $tmpl := printf "dialect/%s/query/path" (index $.Storage 0) }}
				{{- xtemplate $tmpl . }}
			{{- end -}}
		{{- end -}}
		return query
	}
{{ end }}

// First returns the first {{ $.Name }} entity in the query. Returns *ErrNotFound when no {{ lower $.Name }} was found.
func ({{ $receiver }} *{{ $builder }}) First(ctx context.Context) (*{{ $.Name }}, error) {
	{{ plural $.Receiver }}, err := {{ $receiver }}.Limit(1).All(ctx)
	if err != nil {
		return nil, err
	}
	if len({{ plural $.Receiver }}) == 0 {
		return nil, &ErrNotFound{ {{ $.Package }}.Label}
	}
	return {{ plural $.Receiver }}[0], nil
}

// FirstX is like First, but panics if an error occurs.
func ({{ $receiver }} *{{ $builder }}) FirstX(ctx context.Context) *{{ $.Name }} {
	{{ $.Receiver }}, err := {{ $receiver }}.First(ctx)
	if err != nil && !IsNotFound(err) {
		panic(err)
	}
	return {{ $.Receiver }}
}

// FirstID returns the first {{ $.Name }} id in the query. Returns *ErrNotFound when no id was found.
func ({{ $receiver }} *{{ $builder }}) FirstID(ctx context.Context) (id {{ $.ID.Type }}, err error) {
	var ids []{{ $.ID.Type }}
	if ids, err = {{ $receiver }}.Limit(1).IDs(ctx); err != nil {
		return
	}
	if len(ids) == 0 {
		err = &ErrNotFound{ {{ $.Package }}.Label}
		return
	}
	return ids[0], nil
}

// FirstXID is like FirstID, but panics if an error occurs.
func ({{ $receiver }} *{{ $builder }}) FirstXID(ctx context.Context) {{ $.ID.Type }} {
	id, err := {{ $receiver }}.FirstID(ctx)
	if err != nil && !IsNotFound(err) {
		panic(err)
	}
	return id
}

// Only returns the only {{ $.Name }} entity in the query, returns an error if not exactly one entity was returned.
func ({{ $receiver }} *{{ $builder }}) Only(ctx context.Context) (*{{ $.Name }}, error) {
	{{ plural $.Receiver }}, err := {{ $receiver }}.Limit(2).All(ctx)
	if err != nil {
		return nil, err
	}
	switch len({{ plural $.Receiver }}) {
	case 1:
		return {{ plural $.Receiver }}[0], nil
	case 0:
		return nil, &ErrNotFound{ {{ $.Package }}.Label}
	default:
		return nil, &ErrNotSingular{ {{ $.Package }}.Label}
	}
}

// OnlyX is like Only, but panics if an error occurs.
func ({{ $receiver }} *{{ $builder }}) OnlyX(ctx context.Context) *{{ $.Name }} {
	{{ $.Receiver }}, err := {{ $receiver }}.Only(ctx)
	if err != nil {
		panic(err)
	}
	return {{ $.Receiver }}
}

// OnlyID returns the only {{ $.Name }} id in the query, returns an error if not exactly one id was returned.
func ({{ $receiver }} *{{ $builder }}) OnlyID(ctx context.Context) (id {{ $.ID.Type }}, err error) {
	var ids []{{ $.ID.Type }}
	if ids, err = {{ $receiver }}.Limit(2).IDs(ctx); err != nil {
		return
	}
	switch len(ids) {
	case 1:
		id = ids[0]
	case 0:
		err = &ErrNotFound{ {{ $.Package }}.Label}
	default:
		err = &ErrNotSingular{ {{ $.Package }}.Label}
	}
	return
}

// OnlyXID is like OnlyID, but panics if an error occurs.
func ({{ $receiver }} *{{ $builder }}) OnlyXID(ctx context.Context) {{ $.ID.Type }} {
	id, err := {{ $receiver }}.OnlyID(ctx)
	if err != nil {
		panic(err)
	}
	return id
}

// All executes the query and returns a list of {{ plural $.Name }}.
func ({{ $receiver }} *{{ $builder }}) All(ctx context.Context) ([]*{{ $.Name }}, error) {
	{{- if $.MultiStorage }}
		switch {{ $receiver }}.driver.Dialect() {
		{{- range $_, $storage := $.Storage }}
		case {{ join $storage.Dialects ", " }}:
			return {{ $receiver }}.{{ $storage }}All(ctx)
		{{- end }}
		default:
			return nil, errors.New("{{ $pkg }}: unsupported dialect")
		}
	{{- else }}
		return {{ $receiver }}.{{ index $.Storage 0 }}All(ctx)
	{{- end }}
}

// AllX is like All, but panics if an error occurs.
func ({{ $receiver }} *{{ $builder }}) AllX(ctx context.Context) []*{{ $.Name }} {
	{{ plural $.Receiver }}, err := {{ $receiver }}.All(ctx)
	if err != nil {
		panic(err)
	}
	return {{ plural $.Receiver }}
}

// IDs executes the query and returns a list of {{ $.Name }} ids.
func ({{ $receiver }} *{{ $builder }}) IDs(ctx context.Context) ([]{{ $.ID.Type }}, error) {
	var ids []{{ $.ID.Type }}
	if err := {{ $receiver }}.Select({{ $.Package }}.FieldID).Scan(ctx, &ids); err != nil {
		return nil, err
	}
	return ids, nil
}

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

// Count returns the count of the given query.
func ({{ $receiver }} *{{ $builder }}) Count(ctx context.Context) (int, error) {
	{{- if $.MultiStorage }}
		switch {{ $receiver }}.driver.Dialect() {
		{{- range $_, $storage := $.Storage }}
		case {{ join $storage.Dialects ", " }}:
			return {{ $receiver }}.{{ $storage }}Count(ctx)
		{{- end }}
		default:
			return 0, errors.New("{{ $pkg }}: unsupported dialect")
		}
	{{- else }}
		return {{ $receiver }}.{{ index $.Storage 0 }}Count(ctx)
	{{- end }}
}

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

// Exist returns true if the query has elements in the graph.
func ({{ $receiver }} *{{ $builder }}) Exist(ctx context.Context) (bool, error) {
	{{- if $.MultiStorage }}
		switch {{ $receiver }}.driver.Dialect() {
		{{- range $_, $storage := $.Storage }}
		case {{ join $storage.Dialects ", " }}:
			return {{ $receiver }}.{{ $storage }}Exist(ctx)
		{{- end }}
		default:
			return false, errors.New("{{ $pkg }}: unsupported dialect")
		}
	{{- else }}
		return {{ $receiver }}.{{ index $.Storage 0 }}Exist(ctx)
	{{- end }}
}

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

// Clone returns a duplicate of the query builder, including all associated steps. It can be
// used to prepare common query builders and use them differently after the clone is made.
func ({{ $receiver }} *{{ $builder }}) Clone() *{{ $builder }} {
	return &{{ $builder }}{
		config: 	{{ $receiver }}.config,
		limit: 		{{ $receiver }}.limit,
		offset: 	{{ $receiver }}.offset,
		order: 		append([]Order{}, {{ $receiver }}.order...),
		unique: 	append([]string{}, {{ $receiver }}.unique...),
		predicates: append([]predicate.{{ $.Name }}{}, {{ $receiver }}.predicates...),
		// clone intermediate queries.
		{{- range $_, $storage := $.Storage }}
			{{ $storage }}: {{ $receiver }}.{{ $storage }}.Clone(),
		{{- end }}
	}
}

{{ $groupBuilder := pascal $.Name | printf "%sGroupBy" }}

// GroupBy used to group vertices by one or more fields/columns.
// It is often used with aggregate functions, like: {{ join (keys aggregate) ", " }}.
{{- with len $.Fields }}
{{- $f := index $.Fields 0 }}
//
// Example:
//
//	var v []struct {
//		{{ $f.StructField }} {{ $f.Type }} `{{ $f.StructTag }}`
//		Count int `json:"count,omitempty"`
//	}
//
//	client.{{ pascal $.Name }}.Query().
//		GroupBy({{ $.Package }}.{{ $f.Constant }}).
//		Aggregate({{ $pkg }}.Count()).
//		Scan(ctx, &v)
//
{{- end }}
func ({{ $receiver }} *{{ $builder }}) GroupBy(field string, fields ...string) *{{ $groupBuilder }} {
	group := &{{ $groupBuilder }}{config: {{ $receiver }}.config}
	group.fields = append([]string{field}, fields...)
	{{- if $.MultiStorage }}
		switch {{ $receiver }}.driver.Dialect() {
		{{- range $_, $storage := $.Storage }}
		case {{ join $storage.Dialects ", " }}:
			group.{{ $storage }} = {{ $receiver }}.{{ $storage }}Query()
		{{- end }}
		}
	{{- else }}
		group.{{ index $.Storage 0 }} = {{ $receiver }}.{{ index $.Storage 0 }}Query()
	{{- end }}
	return group
}

{{ $selectBuilder := pascal $.Name | printf "%sSelect" }}

// Select one or more fields from the given query.
{{- with len $.Fields }}
{{- $f := index $.Fields 0 }}
//
// Example:
//
//	var v []struct {
//		{{ $f.StructField }} {{ $f.Type }} `{{ $f.StructTag }}`
//	}
//
//	client.{{ pascal $.Name }}.Query().
//		Select({{ $.Package }}.{{ $f.Constant }}).
//		Scan(ctx, &v)
//
{{- end }}
func ({{ $receiver }} *{{ $builder }}) Select(field string, fields ...string) *{{ $selectBuilder }} {
	selector := &{{ $selectBuilder }}{config: {{ $receiver }}.config}
	selector.fields = append([]string{field}, fields...)
	{{- if $.MultiStorage }}
		switch {{ $receiver }}.driver.Dialect() {
		{{- range $_, $storage := $.Storage }}
		case {{ join $storage.Dialects ", " }}:
			selector.{{ $storage }} = {{ $receiver }}.{{ $storage }}Query()
		{{- end }}
		}
	{{- else }}
		selector.{{ index $.Storage 0 }} = {{ $receiver }}.{{ index $.Storage 0 }}Query()
	{{- end }}
	return selector
}

{{- range $_, $storage := $.Storage }}
	{{ with extend $ "Builder" $builder "Package" $pkg }}
		{{ $tmpl := printf "dialect/%s/query" $storage }}
		{{ xtemplate $tmpl . }}
	{{ end }}
{{ end }}

{{/* groupby builder */}}

{{ $groupReceiver := receiver $groupBuilder }}

// {{ $groupBuilder }} is the builder for group-by {{ pascal $.Name }} entities.
type {{ $groupBuilder }} struct {
	config
	fields []string
	fns    []Aggregate
	// intermediate queries.
	{{- range $_, $storage := $.Storage }}
		{{ $storage }} {{ $storage.Builder}}
	{{- end }}
}

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

// Scan applies the group-by query and scan the result into the given value.
func ({{ $groupReceiver }} *{{ $groupBuilder }}) Scan(ctx context.Context, v interface{}) error {
	{{- if $.MultiStorage }}
		switch {{ $groupReceiver }}.driver.Dialect() {
		{{- range $_, $storage := $.Storage }}
		case {{ join $storage.Dialects ", " }}:
			return {{ $groupReceiver }}.{{ $storage }}Scan(ctx, v)
		{{- end }}
		default:
			return errors.New("{{ $groupReceiver }}: unsupported dialect")
		}
	{{- else }}
		return {{ $groupReceiver }}.{{ index $.Storage 0 }}Scan(ctx, v)
	{{- end }}
}

// ScanX is like Scan, but panics if an error occurs.
func ({{ $groupReceiver }} *{{ $groupBuilder }}) ScanX(ctx context.Context, v interface{}) {
	if err := {{ $groupReceiver }}.Scan(ctx, v); err != nil {
		panic(err)
	}
}

{{ range $_, $t := primitives }}
	{{ $f := pascal $t | plural }}
	// {{ $f }} returns list of {{ plural $t }} from group-by. It is only allowed when querying group-by with one field.
	func ({{ $groupReceiver }} *{{ $groupBuilder }}) {{ $f }}(ctx context.Context) ([]{{ $t }}, error) {
		if len({{ $groupReceiver }}.fields) > 1 {
			return nil, errors.New("{{ $pkg }}: {{ $groupBuilder }}.{{ $f }} is not achievable when grouping more than 1 field")
		}
		var v []{{ $t }}
		if err := {{ $groupReceiver }}.Scan(ctx, &v); err != nil {
			return nil, err
		}
		return v, nil
	}

	// {{ $f }}X is like {{ $f }}, but panics if an error occurs.
	func ({{ $groupReceiver }} *{{ $groupBuilder }}) {{ $f }}X(ctx context.Context) []{{ $t }} {
		v, err := {{ $groupReceiver }}.{{ $f }}(ctx)
		if err != nil {
			panic(err)
		}
		return v
	}
{{ end }}

{{- range $_, $storage := $.Storage }}
	{{ with extend $ "Builder" $groupBuilder }}
		{{ $tmpl := printf "dialect/%s/group" $storage }}
		{{ xtemplate $tmpl . }}
	{{ end }}
{{ end }}

{{/* select builder */}}

{{ $selectReceiver := receiver $selectBuilder }}

// {{ $selectBuilder }} is the builder for select fields of {{ pascal $.Name }} entities.
type {{ $selectBuilder }} struct {
	config
	fields []string
	// intermediate queries.
	{{- range $_, $storage := $.Storage }}
		{{ $storage }} {{ $storage.Builder}}
	{{- end }}
}


// Scan applies the selector query and scan the result into the given value.
func ({{ $selectReceiver }} *{{ $selectBuilder }}) Scan(ctx context.Context, v interface{}) error {
	{{- if $.MultiStorage }}
		switch {{ $selectReceiver }}.driver.Dialect() {
		{{- range $_, $storage := $.Storage }}
		case {{ join $storage.Dialects ", " }}:
			return {{ $selectReceiver }}.{{ $storage }}Scan(ctx, v)
		{{- end }}
		default:
			return errors.New("{{ $selectBuilder }}: unsupported dialect")
		}
	{{- else }}
		return {{ $selectReceiver }}.{{ index $.Storage 0 }}Scan(ctx, v)
	{{- end }}
}

// ScanX is like Scan, but panics if an error occurs.
func ({{ $selectReceiver }} *{{ $selectBuilder }}) ScanX(ctx context.Context, v interface{}) {
	if err := {{ $selectReceiver }}.Scan(ctx, v); err != nil {
		panic(err)
	}
}

{{ range $_, $t := primitives }}
	{{ $f := pascal $t | plural }}
	// {{ $f }} returns list of {{ plural $t }} from selector. It is only allowed when selecting one field.
	func ({{ $selectReceiver }} *{{ $selectBuilder }}) {{ $f }}(ctx context.Context) ([]{{ $t }}, error) {
		if len({{ $selectReceiver }}.fields) > 1 {
			return nil, errors.New("{{ $pkg }}: {{ $selectBuilder }}.{{ $f }} is not achievable when selecting more than 1 field")
		}
		var v []{{ $t }}
		if err := {{ $selectReceiver }}.Scan(ctx, &v); err != nil {
			return nil, err
		}
		return v, nil
	}

	// {{ $f }}X is like {{ $f }}, but panics if an error occurs.
	func ({{ $selectReceiver }} *{{ $selectBuilder }}) {{ $f }}X(ctx context.Context) []{{ $t }} {
		v, err := {{ $selectReceiver }}.{{ $f }}(ctx)
		if err != nil {
			panic(err)
		}
		return v
	}
{{ end }}

{{- range $_, $storage := $.Storage }}
	{{ with extend $ "Builder" $selectBuilder }}
		{{ $tmpl := printf "dialect/%s/select" $storage }}
		{{ xtemplate $tmpl . }}
	{{ end }}
{{ end }}


{{ end }}
