// Copyright (c) Facebook, Inc. and its affiliates. 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.

// Code generated (@generated) by entc, DO NOT EDIT.

package ent

import (
	"context"
	"fmt"
	"strconv"
	"strings"

	"github.com/facebookincubator/ent/dialect"
	"github.com/facebookincubator/ent/dialect/gremlin"
	"github.com/facebookincubator/ent/dialect/gremlin/encoding/graphson"
	"github.com/facebookincubator/ent/dialect/gremlin/graph/dsl"
	"github.com/facebookincubator/ent/dialect/gremlin/graph/dsl/__"
	"github.com/facebookincubator/ent/dialect/sql"
)

// Order applies an ordering on either graph traversal or sql selector.
type Order func(interface{})

// OrderPerDialect construct the "order by" clause for graph traversals based on dialect type.
func OrderPerDialect(f0 func(*sql.Selector), f1 func(*dsl.Traversal)) Order {
	return Order(func(v interface{}) {
		switch v := v.(type) {
		case *sql.Selector:
			f0(v)
		case *dsl.Traversal:
			f1(v)
		default:
			panic(fmt.Sprintf("unknown type for order: %T", v))
		}
	})
}

// Asc applies the given fields in ASC order.
func Asc(fields ...string) Order {
	return OrderPerDialect(
		func(s *sql.Selector) {
			for _, f := range fields {
				s.OrderBy(sql.Asc(f))
			}
		},
		func(tr *dsl.Traversal) {
			for _, f := range fields {
				tr.By(f, dsl.Incr)
			}
		},
	)
}

// Desc applies the given fields in DESC order.
func Desc(fields ...string) Order {
	return OrderPerDialect(
		func(s *sql.Selector) {
			for _, f := range fields {
				s.OrderBy(sql.Desc(f))
			}
		},
		func(tr *dsl.Traversal) {
			for _, f := range fields {
				tr.By(f, dsl.Decr)
			}
		},
	)
}

// Aggregate applies an aggregation step on the group-by traversal/selector.
type Aggregate struct {
	// SQL the column wrapped with the aggregation function.
	SQL func(*sql.Selector) string
	// Gremlin gets two labels as parameters. The first used in the `As` step for the predicate,
	// and the second is an optional name for the next predicates (or for later usage).
	Gremlin func(string, string) (string, *dsl.Traversal)
}

// As is a pseudo aggregation function for renaming another other functions with custom names. For example:
//
//	GroupBy(field1, field2).
//	Aggregate(ent.As(ent.Sum(field1), "sum_field1"), (ent.As(ent.Sum(field2), "sum_field2")).
//	Scan(ctx, &v)
//
func As(fn Aggregate, end string) Aggregate {
	return Aggregate{
		SQL: func(s *sql.Selector) string {
			return sql.As(fn.SQL(s), end)
		},
		Gremlin: func(start, _ string) (string, *dsl.Traversal) {
			return fn.Gremlin(start, end)
		},
	}
}

// DefaultCountLabel is the default label name for the Count aggregation function.
// It should be used as the struct-tag for decoding, or a map key for interaction with the returned response.
// In order to "count" 2 or more fields and avoid conflicting, use the `ent.As(ent.Count(field), "custom_name")`
// function with custom name in order to override it.
const DefaultCountLabel = "count"

// Count applies the "count" aggregation function on each group.
func Count() Aggregate {
	return Aggregate{
		SQL: func(s *sql.Selector) string {
			return sql.Count("*")
		},
		Gremlin: func(start, end string) (string, *dsl.Traversal) {
			if end == "" {
				end = DefaultCountLabel
			}
			return end, __.As(start).Count(dsl.Local).As(end)
		},
	}
}

// DefaultMaxLabel is the default label name for the Max aggregation function.
// It should be used as the struct-tag for decoding, or a map key for interaction with the returned response.
// In order to "max" 2 or more fields and avoid conflicting, use the `ent.As(ent.Max(field), "custom_name")`
// function with custom name in order to override it.
const DefaultMaxLabel = "max"

// Max applies the "max" aggregation function on the given field of each group.
func Max(field string) Aggregate {
	return Aggregate{
		SQL: func(s *sql.Selector) string {
			return sql.Max(s.C(field))
		},
		Gremlin: func(start, end string) (string, *dsl.Traversal) {
			if end == "" {
				end = DefaultMaxLabel
			}
			return end, __.As(start).Unfold().Values(field).Max().As(end)
		},
	}
}

// DefaultMeanLabel is the default label name for the Mean aggregation function.
// It should be used as the struct-tag for decoding, or a map key for interaction with the returned response.
// In order to "mean" 2 or more fields and avoid conflicting, use the `ent.As(ent.Mean(field), "custom_name")`
// function with custom name in order to override it.
const DefaultMeanLabel = "mean"

// Mean applies the "mean" aggregation function on the given field of each group.
func Mean(field string) Aggregate {
	return Aggregate{
		SQL: func(s *sql.Selector) string {
			return sql.Avg(s.C(field))
		},
		Gremlin: func(start, end string) (string, *dsl.Traversal) {
			if end == "" {
				end = DefaultMeanLabel
			}
			return end, __.As(start).Unfold().Values(field).Mean().As(end)
		},
	}
}

// DefaultMinLabel is the default label name for the Min aggregation function.
// It should be used as the struct-tag for decoding, or a map key for interaction with the returned response.
// In order to "min" 2 or more fields and avoid conflicting, use the `ent.As(ent.Min(field), "custom_name")`
// function with custom name in order to override it.
const DefaultMinLabel = "min"

// Min applies the "min" aggregation function on the given field of each group.
func Min(field string) Aggregate {
	return Aggregate{
		SQL: func(s *sql.Selector) string {
			return sql.Min(s.C(field))
		},
		Gremlin: func(start, end string) (string, *dsl.Traversal) {
			if end == "" {
				end = DefaultMinLabel
			}
			return end, __.As(start).Unfold().Values(field).Min().As(end)
		},
	}
}

// DefaultSumLabel is the default label name for the Sum aggregation function.
// It should be used as the struct-tag for decoding, or a map key for interaction with the returned response.
// In order to "sum" 2 or more fields and avoid conflicting, use the `ent.As(ent.Sum(field), "custom_name")`
// function with custom name in order to override it.
const DefaultSumLabel = "sum"

// Sum applies the "sum" aggregation function on the given field of each group.
func Sum(field string) Aggregate {
	return Aggregate{
		SQL: func(s *sql.Selector) string {
			return sql.Sum(s.C(field))
		},
		Gremlin: func(start, end string) (string, *dsl.Traversal) {
			if end == "" {
				end = DefaultSumLabel
			}
			return end, __.As(start).Unfold().Values(field).Sum().As(end)
		},
	}
}

// ErrNotFound returns when trying to fetch a specific entity and it was not found in the database.
type ErrNotFound struct {
	label string
}

// Error implements the error interface.
func (e *ErrNotFound) Error() string {
	return fmt.Sprintf("ent: %s not found", e.label)
}

// IsNotFound returns a boolean indicating whether the error is a not found error.
func IsNotFound(err error) bool {
	_, ok := err.(*ErrNotFound)
	return ok
}

// MaskNotFound masks nor found error.
func MaskNotFound(err error) error {
	if IsNotFound(err) {
		return nil
	}
	return err
}

// ErrNotSingular returns when trying to fetch a singular entity and more then one was found in the database.
type ErrNotSingular struct {
	label string
}

// Error implements the error interface.
func (e *ErrNotSingular) Error() string {
	return fmt.Sprintf("ent: %s not singular", e.label)
}

// IsNotSingular returns a boolean indicating whether the error is a not singular error.
func IsNotSingular(err error) bool {
	_, ok := err.(*ErrNotSingular)
	return ok
}

// ErrConstraintFailed returns when trying to create/update one or more entities and
// one or more of their constraints failed. For example, violation of edge or field uniqueness.
type ErrConstraintFailed struct {
	msg  string
	wrap error
}

// Error implements the error interface.
func (e ErrConstraintFailed) Error() string {
	return fmt.Sprintf("ent: unique constraint failed: %s", e.msg)
}

// Unwrap implements the errors.Wrapper interface.
func (e *ErrConstraintFailed) Unwrap() error {
	return e.wrap
}

// IsConstraintFailure returns a boolean indicating whether the error is a constraint failure.
func IsConstraintFailure(err error) bool {
	_, ok := err.(*ErrConstraintFailed)
	return ok
}

func isSQLConstraintError(err error) (*ErrConstraintFailed, bool) {
	var (
		msg = err.Error()
		// error format per dialect.
		errors = [...]string{
			"Error 1062",               // MySQL 1062 error (ER_DUP_ENTRY).
			"UNIQUE constraint failed", // SQLite.
			"duplicate key value violates unique constraint", // PostgreSQL.
		}
	)
	for i := range errors {
		if strings.Contains(msg, errors[i]) {
			return &ErrConstraintFailed{msg, err}, true
		}
	}
	return nil, false
}

// rollback calls to tx.Rollback and wraps the given error with the rollback error if occurred.
func rollback(tx dialect.Tx, err error) error {
	if rerr := tx.Rollback(); rerr != nil {
		err = fmt.Errorf("%s: %v", err.Error(), rerr)
	}
	if err, ok := isSQLConstraintError(err); ok {
		return err
	}
	return err
}

// insertLastID invokes the insert query on the transaction and returns the LastInsertID.
func insertLastID(ctx context.Context, tx dialect.Tx, insert *sql.InsertBuilder) (int64, error) {
	query, args := insert.Query()
	// PostgreSQL does not support the LastInsertId() method of sql.Result
	// on Exec, and should be extracted manually using the `RETURNING` clause.
	if insert.Dialect() == dialect.Postgres {
		rows := &sql.Rows{}
		if err := tx.Query(ctx, query, args, rows); err != nil {
			return 0, err
		}
		defer rows.Close()
		if !rows.Next() {
			return 0, fmt.Errorf("no rows found for query: %v", query)
		}
		var id int64
		if err := rows.Scan(&id); err != nil {
			return 0, err
		}
		return id, nil
	}
	// MySQL, SQLite, etc.
	var res sql.Result
	if err := tx.Exec(ctx, query, args, &res); err != nil {
		return 0, err
	}
	id, err := res.LastInsertId()
	if err != nil {
		return 0, err
	}
	return id, nil
}

// Code implements the dsl.Node interface.
func (e ErrConstraintFailed) Code() (string, []interface{}) {
	return strconv.Quote(e.prefix() + e.msg), nil
}

func (e *ErrConstraintFailed) UnmarshalGraphson(b []byte) error {
	var v [1]*string
	if err := graphson.Unmarshal(b, &v); err != nil {
		return err
	}
	if v[0] == nil {
		return fmt.Errorf("ent: missing string value")
	}
	if !strings.HasPrefix(*v[0], e.prefix()) {
		return fmt.Errorf("ent: invalid string for error: %s", *v[0])
	}
	e.msg = strings.TrimPrefix(*v[0], e.prefix())
	return nil
}

// prefix returns the prefix used for gremlin constants.
func (ErrConstraintFailed) prefix() string { return "Error: " }

// NewErrUniqueField creates a constraint error for unique fields.
func NewErrUniqueField(label, field string, v interface{}) *ErrConstraintFailed {
	return &ErrConstraintFailed{msg: fmt.Sprintf("field %s.%s with value: %#v", label, field, v)}
}

// NewErrUniqueEdge creates a constraint error for unique edges.
func NewErrUniqueEdge(label, edge, id string) *ErrConstraintFailed {
	return &ErrConstraintFailed{msg: fmt.Sprintf("edge %s.%s with id: %#v", label, edge, id)}
}

// isConstantError indicates if the given response holds a gremlin constant containing an error.
func isConstantError(r *gremlin.Response) (*ErrConstraintFailed, bool) {
	e := &ErrConstraintFailed{}
	if err := graphson.Unmarshal(r.Result.Data, e); err != nil {
		return nil, false
	}
	return e, true
}

// keys returns the keys/ids from the edge map.
func keys(m map[string]struct{}) []string {
	s := make([]string, 0, len(m))
	for id := range m {
		s = append(s, id)
	}
	return s
}
