// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"registry-backend/ent/node"
	"registry-backend/ent/nodereview"
	"registry-backend/ent/user"

	"entgo.io/ent/dialect"
	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/google/uuid"
)

// NodeReviewCreate is the builder for creating a NodeReview entity.
type NodeReviewCreate struct {
	config
	mutation *NodeReviewMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

// SetNodeID sets the "node_id" field.
func (nrc *NodeReviewCreate) SetNodeID(s string) *NodeReviewCreate {
	nrc.mutation.SetNodeID(s)
	return nrc
}

// SetUserID sets the "user_id" field.
func (nrc *NodeReviewCreate) SetUserID(s string) *NodeReviewCreate {
	nrc.mutation.SetUserID(s)
	return nrc
}

// SetStar sets the "star" field.
func (nrc *NodeReviewCreate) SetStar(i int) *NodeReviewCreate {
	nrc.mutation.SetStar(i)
	return nrc
}

// SetNillableStar sets the "star" field if the given value is not nil.
func (nrc *NodeReviewCreate) SetNillableStar(i *int) *NodeReviewCreate {
	if i != nil {
		nrc.SetStar(*i)
	}
	return nrc
}

// SetID sets the "id" field.
func (nrc *NodeReviewCreate) SetID(u uuid.UUID) *NodeReviewCreate {
	nrc.mutation.SetID(u)
	return nrc
}

// SetNillableID sets the "id" field if the given value is not nil.
func (nrc *NodeReviewCreate) SetNillableID(u *uuid.UUID) *NodeReviewCreate {
	if u != nil {
		nrc.SetID(*u)
	}
	return nrc
}

// SetUser sets the "user" edge to the User entity.
func (nrc *NodeReviewCreate) SetUser(u *User) *NodeReviewCreate {
	return nrc.SetUserID(u.ID)
}

// SetNode sets the "node" edge to the Node entity.
func (nrc *NodeReviewCreate) SetNode(n *Node) *NodeReviewCreate {
	return nrc.SetNodeID(n.ID)
}

// Mutation returns the NodeReviewMutation object of the builder.
func (nrc *NodeReviewCreate) Mutation() *NodeReviewMutation {
	return nrc.mutation
}

// Save creates the NodeReview in the database.
func (nrc *NodeReviewCreate) Save(ctx context.Context) (*NodeReview, error) {
	nrc.defaults()
	return withHooks(ctx, nrc.sqlSave, nrc.mutation, nrc.hooks)
}

// SaveX calls Save and panics if Save returns an error.
func (nrc *NodeReviewCreate) SaveX(ctx context.Context) *NodeReview {
	v, err := nrc.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

// Exec executes the query.
func (nrc *NodeReviewCreate) Exec(ctx context.Context) error {
	_, err := nrc.Save(ctx)
	return err
}

// ExecX is like Exec, but panics if an error occurs.
func (nrc *NodeReviewCreate) ExecX(ctx context.Context) {
	if err := nrc.Exec(ctx); err != nil {
		panic(err)
	}
}

// defaults sets the default values of the builder before save.
func (nrc *NodeReviewCreate) defaults() {
	if _, ok := nrc.mutation.Star(); !ok {
		v := nodereview.DefaultStar
		nrc.mutation.SetStar(v)
	}
	if _, ok := nrc.mutation.ID(); !ok {
		v := nodereview.DefaultID()
		nrc.mutation.SetID(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (nrc *NodeReviewCreate) check() error {
	if _, ok := nrc.mutation.NodeID(); !ok {
		return &ValidationError{Name: "node_id", err: errors.New(`ent: missing required field "NodeReview.node_id"`)}
	}
	if _, ok := nrc.mutation.UserID(); !ok {
		return &ValidationError{Name: "user_id", err: errors.New(`ent: missing required field "NodeReview.user_id"`)}
	}
	if _, ok := nrc.mutation.Star(); !ok {
		return &ValidationError{Name: "star", err: errors.New(`ent: missing required field "NodeReview.star"`)}
	}
	if _, ok := nrc.mutation.UserID(); !ok {
		return &ValidationError{Name: "user", err: errors.New(`ent: missing required edge "NodeReview.user"`)}
	}
	if _, ok := nrc.mutation.NodeID(); !ok {
		return &ValidationError{Name: "node", err: errors.New(`ent: missing required edge "NodeReview.node"`)}
	}
	return nil
}

func (nrc *NodeReviewCreate) sqlSave(ctx context.Context) (*NodeReview, error) {
	if err := nrc.check(); err != nil {
		return nil, err
	}
	_node, _spec := nrc.createSpec()
	if err := sqlgraph.CreateNode(ctx, nrc.driver, _spec); err != nil {
		if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	if _spec.ID.Value != nil {
		if id, ok := _spec.ID.Value.(*uuid.UUID); ok {
			_node.ID = *id
		} else if err := _node.ID.Scan(_spec.ID.Value); err != nil {
			return nil, err
		}
	}
	nrc.mutation.id = &_node.ID
	nrc.mutation.done = true
	return _node, nil
}

func (nrc *NodeReviewCreate) createSpec() (*NodeReview, *sqlgraph.CreateSpec) {
	var (
		_node = &NodeReview{config: nrc.config}
		_spec = sqlgraph.NewCreateSpec(nodereview.Table, sqlgraph.NewFieldSpec(nodereview.FieldID, field.TypeUUID))
	)
	_spec.OnConflict = nrc.conflict
	if id, ok := nrc.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = &id
	}
	if value, ok := nrc.mutation.Star(); ok {
		_spec.SetField(nodereview.FieldStar, field.TypeInt, value)
		_node.Star = value
	}
	if nodes := nrc.mutation.UserIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   nodereview.UserTable,
			Columns: []string{nodereview.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeString),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.UserID = nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := nrc.mutation.NodeIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   nodereview.NodeTable,
			Columns: []string{nodereview.NodeColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(node.FieldID, field.TypeString),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.NodeID = nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	return _node, _spec
}

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.NodeReview.Create().
//		SetNodeID(v).
//		OnConflict(
//			// Update the row with the new values
//			// the was proposed for insertion.
//			sql.ResolveWithNewValues(),
//		).
//		// Override some of the fields with custom
//		// update values.
//		Update(func(u *ent.NodeReviewUpsert) {
//			SetNodeID(v+v).
//		}).
//		Exec(ctx)
func (nrc *NodeReviewCreate) OnConflict(opts ...sql.ConflictOption) *NodeReviewUpsertOne {
	nrc.conflict = opts
	return &NodeReviewUpsertOne{
		create: nrc,
	}
}

// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
//	client.NodeReview.Create().
//		OnConflict(sql.ConflictColumns(columns...)).
//		Exec(ctx)
func (nrc *NodeReviewCreate) OnConflictColumns(columns ...string) *NodeReviewUpsertOne {
	nrc.conflict = append(nrc.conflict, sql.ConflictColumns(columns...))
	return &NodeReviewUpsertOne{
		create: nrc,
	}
}

type (
	// NodeReviewUpsertOne is the builder for "upsert"-ing
	//  one NodeReview node.
	NodeReviewUpsertOne struct {
		create *NodeReviewCreate
	}

	// NodeReviewUpsert is the "OnConflict" setter.
	NodeReviewUpsert struct {
		*sql.UpdateSet
	}
)

// SetNodeID sets the "node_id" field.
func (u *NodeReviewUpsert) SetNodeID(v string) *NodeReviewUpsert {
	u.Set(nodereview.FieldNodeID, v)
	return u
}

// UpdateNodeID sets the "node_id" field to the value that was provided on create.
func (u *NodeReviewUpsert) UpdateNodeID() *NodeReviewUpsert {
	u.SetExcluded(nodereview.FieldNodeID)
	return u
}

// SetUserID sets the "user_id" field.
func (u *NodeReviewUpsert) SetUserID(v string) *NodeReviewUpsert {
	u.Set(nodereview.FieldUserID, v)
	return u
}

// UpdateUserID sets the "user_id" field to the value that was provided on create.
func (u *NodeReviewUpsert) UpdateUserID() *NodeReviewUpsert {
	u.SetExcluded(nodereview.FieldUserID)
	return u
}

// SetStar sets the "star" field.
func (u *NodeReviewUpsert) SetStar(v int) *NodeReviewUpsert {
	u.Set(nodereview.FieldStar, v)
	return u
}

// UpdateStar sets the "star" field to the value that was provided on create.
func (u *NodeReviewUpsert) UpdateStar() *NodeReviewUpsert {
	u.SetExcluded(nodereview.FieldStar)
	return u
}

// AddStar adds v to the "star" field.
func (u *NodeReviewUpsert) AddStar(v int) *NodeReviewUpsert {
	u.Add(nodereview.FieldStar, v)
	return u
}

// UpdateNewValues updates the mutable fields using the new values that were set on create except the ID field.
// Using this option is equivalent to using:
//
//	client.NodeReview.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(nodereview.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *NodeReviewUpsertOne) UpdateNewValues() *NodeReviewUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
		if _, exists := u.create.mutation.ID(); exists {
			s.SetIgnore(nodereview.FieldID)
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.NodeReview.Create().
//	    OnConflict(sql.ResolveWithIgnore()).
//	    Exec(ctx)
func (u *NodeReviewUpsertOne) Ignore() *NodeReviewUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
	return u
}

// DoNothing configures the conflict_action to `DO NOTHING`.
// Supported only by SQLite and PostgreSQL.
func (u *NodeReviewUpsertOne) DoNothing() *NodeReviewUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

// Update allows overriding fields `UPDATE` values. See the NodeReviewCreate.OnConflict
// documentation for more info.
func (u *NodeReviewUpsertOne) Update(set func(*NodeReviewUpsert)) *NodeReviewUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
		set(&NodeReviewUpsert{UpdateSet: update})
	}))
	return u
}

// SetNodeID sets the "node_id" field.
func (u *NodeReviewUpsertOne) SetNodeID(v string) *NodeReviewUpsertOne {
	return u.Update(func(s *NodeReviewUpsert) {
		s.SetNodeID(v)
	})
}

// UpdateNodeID sets the "node_id" field to the value that was provided on create.
func (u *NodeReviewUpsertOne) UpdateNodeID() *NodeReviewUpsertOne {
	return u.Update(func(s *NodeReviewUpsert) {
		s.UpdateNodeID()
	})
}

// SetUserID sets the "user_id" field.
func (u *NodeReviewUpsertOne) SetUserID(v string) *NodeReviewUpsertOne {
	return u.Update(func(s *NodeReviewUpsert) {
		s.SetUserID(v)
	})
}

// UpdateUserID sets the "user_id" field to the value that was provided on create.
func (u *NodeReviewUpsertOne) UpdateUserID() *NodeReviewUpsertOne {
	return u.Update(func(s *NodeReviewUpsert) {
		s.UpdateUserID()
	})
}

// SetStar sets the "star" field.
func (u *NodeReviewUpsertOne) SetStar(v int) *NodeReviewUpsertOne {
	return u.Update(func(s *NodeReviewUpsert) {
		s.SetStar(v)
	})
}

// AddStar adds v to the "star" field.
func (u *NodeReviewUpsertOne) AddStar(v int) *NodeReviewUpsertOne {
	return u.Update(func(s *NodeReviewUpsert) {
		s.AddStar(v)
	})
}

// UpdateStar sets the "star" field to the value that was provided on create.
func (u *NodeReviewUpsertOne) UpdateStar() *NodeReviewUpsertOne {
	return u.Update(func(s *NodeReviewUpsert) {
		s.UpdateStar()
	})
}

// Exec executes the query.
func (u *NodeReviewUpsertOne) Exec(ctx context.Context) error {
	if len(u.create.conflict) == 0 {
		return errors.New("ent: missing options for NodeReviewCreate.OnConflict")
	}
	return u.create.Exec(ctx)
}

// ExecX is like Exec, but panics if an error occurs.
func (u *NodeReviewUpsertOne) ExecX(ctx context.Context) {
	if err := u.create.Exec(ctx); err != nil {
		panic(err)
	}
}

// Exec executes the UPSERT query and returns the inserted/updated ID.
func (u *NodeReviewUpsertOne) ID(ctx context.Context) (id uuid.UUID, err error) {
	if u.create.driver.Dialect() == dialect.MySQL {
		// In case of "ON CONFLICT", there is no way to get back non-numeric ID
		// fields from the database since MySQL does not support the RETURNING clause.
		return id, errors.New("ent: NodeReviewUpsertOne.ID is not supported by MySQL driver. Use NodeReviewUpsertOne.Exec instead")
	}
	node, err := u.create.Save(ctx)
	if err != nil {
		return id, err
	}
	return node.ID, nil
}

// IDX is like ID, but panics if an error occurs.
func (u *NodeReviewUpsertOne) IDX(ctx context.Context) uuid.UUID {
	id, err := u.ID(ctx)
	if err != nil {
		panic(err)
	}
	return id
}

// NodeReviewCreateBulk is the builder for creating many NodeReview entities in bulk.
type NodeReviewCreateBulk struct {
	config
	err      error
	builders []*NodeReviewCreate
	conflict []sql.ConflictOption
}

// Save creates the NodeReview entities in the database.
func (nrcb *NodeReviewCreateBulk) Save(ctx context.Context) ([]*NodeReview, error) {
	if nrcb.err != nil {
		return nil, nrcb.err
	}
	specs := make([]*sqlgraph.CreateSpec, len(nrcb.builders))
	nodes := make([]*NodeReview, len(nrcb.builders))
	mutators := make([]Mutator, len(nrcb.builders))
	for i := range nrcb.builders {
		func(i int, root context.Context) {
			builder := nrcb.builders[i]
			builder.defaults()
			var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
				mutation, ok := m.(*NodeReviewMutation)
				if !ok {
					return nil, fmt.Errorf("unexpected mutation type %T", m)
				}
				if err := builder.check(); err != nil {
					return nil, err
				}
				builder.mutation = mutation
				var err error
				nodes[i], specs[i] = builder.createSpec()
				if i < len(mutators)-1 {
					_, err = mutators[i+1].Mutate(root, nrcb.builders[i+1].mutation)
				} else {
					spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
					spec.OnConflict = nrcb.conflict
					// Invoke the actual operation on the latest mutation in the chain.
					if err = sqlgraph.BatchCreate(ctx, nrcb.driver, spec); err != nil {
						if sqlgraph.IsConstraintError(err) {
							err = &ConstraintError{msg: err.Error(), wrap: err}
						}
					}
				}
				if err != nil {
					return nil, err
				}
				mutation.id = &nodes[i].ID
				mutation.done = true
				return nodes[i], nil
			})
			for i := len(builder.hooks) - 1; i >= 0; i-- {
				mut = builder.hooks[i](mut)
			}
			mutators[i] = mut
		}(i, ctx)
	}
	if len(mutators) > 0 {
		if _, err := mutators[0].Mutate(ctx, nrcb.builders[0].mutation); err != nil {
			return nil, err
		}
	}
	return nodes, nil
}

// SaveX is like Save, but panics if an error occurs.
func (nrcb *NodeReviewCreateBulk) SaveX(ctx context.Context) []*NodeReview {
	v, err := nrcb.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

// Exec executes the query.
func (nrcb *NodeReviewCreateBulk) Exec(ctx context.Context) error {
	_, err := nrcb.Save(ctx)
	return err
}

// ExecX is like Exec, but panics if an error occurs.
func (nrcb *NodeReviewCreateBulk) ExecX(ctx context.Context) {
	if err := nrcb.Exec(ctx); err != nil {
		panic(err)
	}
}

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.NodeReview.CreateBulk(builders...).
//		OnConflict(
//			// Update the row with the new values
//			// the was proposed for insertion.
//			sql.ResolveWithNewValues(),
//		).
//		// Override some of the fields with custom
//		// update values.
//		Update(func(u *ent.NodeReviewUpsert) {
//			SetNodeID(v+v).
//		}).
//		Exec(ctx)
func (nrcb *NodeReviewCreateBulk) OnConflict(opts ...sql.ConflictOption) *NodeReviewUpsertBulk {
	nrcb.conflict = opts
	return &NodeReviewUpsertBulk{
		create: nrcb,
	}
}

// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
//	client.NodeReview.Create().
//		OnConflict(sql.ConflictColumns(columns...)).
//		Exec(ctx)
func (nrcb *NodeReviewCreateBulk) OnConflictColumns(columns ...string) *NodeReviewUpsertBulk {
	nrcb.conflict = append(nrcb.conflict, sql.ConflictColumns(columns...))
	return &NodeReviewUpsertBulk{
		create: nrcb,
	}
}

// NodeReviewUpsertBulk is the builder for "upsert"-ing
// a bulk of NodeReview nodes.
type NodeReviewUpsertBulk struct {
	create *NodeReviewCreateBulk
}

// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
//	client.NodeReview.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(nodereview.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *NodeReviewUpsertBulk) UpdateNewValues() *NodeReviewUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
		for _, b := range u.create.builders {
			if _, exists := b.mutation.ID(); exists {
				s.SetIgnore(nodereview.FieldID)
			}
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.NodeReview.Create().
//		OnConflict(sql.ResolveWithIgnore()).
//		Exec(ctx)
func (u *NodeReviewUpsertBulk) Ignore() *NodeReviewUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
	return u
}

// DoNothing configures the conflict_action to `DO NOTHING`.
// Supported only by SQLite and PostgreSQL.
func (u *NodeReviewUpsertBulk) DoNothing() *NodeReviewUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

// Update allows overriding fields `UPDATE` values. See the NodeReviewCreateBulk.OnConflict
// documentation for more info.
func (u *NodeReviewUpsertBulk) Update(set func(*NodeReviewUpsert)) *NodeReviewUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
		set(&NodeReviewUpsert{UpdateSet: update})
	}))
	return u
}

// SetNodeID sets the "node_id" field.
func (u *NodeReviewUpsertBulk) SetNodeID(v string) *NodeReviewUpsertBulk {
	return u.Update(func(s *NodeReviewUpsert) {
		s.SetNodeID(v)
	})
}

// UpdateNodeID sets the "node_id" field to the value that was provided on create.
func (u *NodeReviewUpsertBulk) UpdateNodeID() *NodeReviewUpsertBulk {
	return u.Update(func(s *NodeReviewUpsert) {
		s.UpdateNodeID()
	})
}

// SetUserID sets the "user_id" field.
func (u *NodeReviewUpsertBulk) SetUserID(v string) *NodeReviewUpsertBulk {
	return u.Update(func(s *NodeReviewUpsert) {
		s.SetUserID(v)
	})
}

// UpdateUserID sets the "user_id" field to the value that was provided on create.
func (u *NodeReviewUpsertBulk) UpdateUserID() *NodeReviewUpsertBulk {
	return u.Update(func(s *NodeReviewUpsert) {
		s.UpdateUserID()
	})
}

// SetStar sets the "star" field.
func (u *NodeReviewUpsertBulk) SetStar(v int) *NodeReviewUpsertBulk {
	return u.Update(func(s *NodeReviewUpsert) {
		s.SetStar(v)
	})
}

// AddStar adds v to the "star" field.
func (u *NodeReviewUpsertBulk) AddStar(v int) *NodeReviewUpsertBulk {
	return u.Update(func(s *NodeReviewUpsert) {
		s.AddStar(v)
	})
}

// UpdateStar sets the "star" field to the value that was provided on create.
func (u *NodeReviewUpsertBulk) UpdateStar() *NodeReviewUpsertBulk {
	return u.Update(func(s *NodeReviewUpsert) {
		s.UpdateStar()
	})
}

// Exec executes the query.
func (u *NodeReviewUpsertBulk) Exec(ctx context.Context) error {
	if u.create.err != nil {
		return u.create.err
	}
	for i, b := range u.create.builders {
		if len(b.conflict) != 0 {
			return fmt.Errorf("ent: OnConflict was set for builder %d. Set it on the NodeReviewCreateBulk instead", i)
		}
	}
	if len(u.create.conflict) == 0 {
		return errors.New("ent: missing options for NodeReviewCreateBulk.OnConflict")
	}
	return u.create.Exec(ctx)
}

// ExecX is like Exec, but panics if an error occurs.
func (u *NodeReviewUpsertBulk) ExecX(ctx context.Context) {
	if err := u.create.Exec(ctx); err != nil {
		panic(err)
	}
}
