// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"time"
	"wechat-api/ent/agent"
	"wechat-api/ent/apikey"
	"wechat-api/ent/token"
	"wechat-api/ent/whatsapp"
	"wechat-api/ent/wx"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
)

// AgentCreate is the builder for creating a Agent entity.
type AgentCreate struct {
	config
	mutation *AgentMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

// SetCreatedAt sets the "created_at" field.
func (ac *AgentCreate) SetCreatedAt(t time.Time) *AgentCreate {
	ac.mutation.SetCreatedAt(t)
	return ac
}

// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (ac *AgentCreate) SetNillableCreatedAt(t *time.Time) *AgentCreate {
	if t != nil {
		ac.SetCreatedAt(*t)
	}
	return ac
}

// SetUpdatedAt sets the "updated_at" field.
func (ac *AgentCreate) SetUpdatedAt(t time.Time) *AgentCreate {
	ac.mutation.SetUpdatedAt(t)
	return ac
}

// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
func (ac *AgentCreate) SetNillableUpdatedAt(t *time.Time) *AgentCreate {
	if t != nil {
		ac.SetUpdatedAt(*t)
	}
	return ac
}

// SetDeletedAt sets the "deleted_at" field.
func (ac *AgentCreate) SetDeletedAt(t time.Time) *AgentCreate {
	ac.mutation.SetDeletedAt(t)
	return ac
}

// SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
func (ac *AgentCreate) SetNillableDeletedAt(t *time.Time) *AgentCreate {
	if t != nil {
		ac.SetDeletedAt(*t)
	}
	return ac
}

// SetName sets the "name" field.
func (ac *AgentCreate) SetName(s string) *AgentCreate {
	ac.mutation.SetName(s)
	return ac
}

// SetRole sets the "role" field.
func (ac *AgentCreate) SetRole(s string) *AgentCreate {
	ac.mutation.SetRole(s)
	return ac
}

// SetStatus sets the "status" field.
func (ac *AgentCreate) SetStatus(i int) *AgentCreate {
	ac.mutation.SetStatus(i)
	return ac
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (ac *AgentCreate) SetNillableStatus(i *int) *AgentCreate {
	if i != nil {
		ac.SetStatus(*i)
	}
	return ac
}

// SetBackground sets the "background" field.
func (ac *AgentCreate) SetBackground(s string) *AgentCreate {
	ac.mutation.SetBackground(s)
	return ac
}

// SetNillableBackground sets the "background" field if the given value is not nil.
func (ac *AgentCreate) SetNillableBackground(s *string) *AgentCreate {
	if s != nil {
		ac.SetBackground(*s)
	}
	return ac
}

// SetExamples sets the "examples" field.
func (ac *AgentCreate) SetExamples(s string) *AgentCreate {
	ac.mutation.SetExamples(s)
	return ac
}

// SetNillableExamples sets the "examples" field if the given value is not nil.
func (ac *AgentCreate) SetNillableExamples(s *string) *AgentCreate {
	if s != nil {
		ac.SetExamples(*s)
	}
	return ac
}

// SetOrganizationID sets the "organization_id" field.
func (ac *AgentCreate) SetOrganizationID(u uint64) *AgentCreate {
	ac.mutation.SetOrganizationID(u)
	return ac
}

// SetDatasetID sets the "dataset_id" field.
func (ac *AgentCreate) SetDatasetID(s string) *AgentCreate {
	ac.mutation.SetDatasetID(s)
	return ac
}

// SetNillableDatasetID sets the "dataset_id" field if the given value is not nil.
func (ac *AgentCreate) SetNillableDatasetID(s *string) *AgentCreate {
	if s != nil {
		ac.SetDatasetID(*s)
	}
	return ac
}

// SetCollectionID sets the "collection_id" field.
func (ac *AgentCreate) SetCollectionID(s string) *AgentCreate {
	ac.mutation.SetCollectionID(s)
	return ac
}

// SetNillableCollectionID sets the "collection_id" field if the given value is not nil.
func (ac *AgentCreate) SetNillableCollectionID(s *string) *AgentCreate {
	if s != nil {
		ac.SetCollectionID(*s)
	}
	return ac
}

// SetID sets the "id" field.
func (ac *AgentCreate) SetID(u uint64) *AgentCreate {
	ac.mutation.SetID(u)
	return ac
}

// AddWxAgentIDs adds the "wx_agent" edge to the Wx entity by IDs.
func (ac *AgentCreate) AddWxAgentIDs(ids ...uint64) *AgentCreate {
	ac.mutation.AddWxAgentIDs(ids...)
	return ac
}

// AddWxAgent adds the "wx_agent" edges to the Wx entity.
func (ac *AgentCreate) AddWxAgent(w ...*Wx) *AgentCreate {
	ids := make([]uint64, len(w))
	for i := range w {
		ids[i] = w[i].ID
	}
	return ac.AddWxAgentIDs(ids...)
}

// AddTokenAgentIDs adds the "token_agent" edge to the Token entity by IDs.
func (ac *AgentCreate) AddTokenAgentIDs(ids ...uint64) *AgentCreate {
	ac.mutation.AddTokenAgentIDs(ids...)
	return ac
}

// AddTokenAgent adds the "token_agent" edges to the Token entity.
func (ac *AgentCreate) AddTokenAgent(t ...*Token) *AgentCreate {
	ids := make([]uint64, len(t))
	for i := range t {
		ids[i] = t[i].ID
	}
	return ac.AddTokenAgentIDs(ids...)
}

// AddWaAgentIDs adds the "wa_agent" edge to the Whatsapp entity by IDs.
func (ac *AgentCreate) AddWaAgentIDs(ids ...uint64) *AgentCreate {
	ac.mutation.AddWaAgentIDs(ids...)
	return ac
}

// AddWaAgent adds the "wa_agent" edges to the Whatsapp entity.
func (ac *AgentCreate) AddWaAgent(w ...*Whatsapp) *AgentCreate {
	ids := make([]uint64, len(w))
	for i := range w {
		ids[i] = w[i].ID
	}
	return ac.AddWaAgentIDs(ids...)
}

// AddKeyAgentIDs adds the "key_agent" edge to the ApiKey entity by IDs.
func (ac *AgentCreate) AddKeyAgentIDs(ids ...uint64) *AgentCreate {
	ac.mutation.AddKeyAgentIDs(ids...)
	return ac
}

// AddKeyAgent adds the "key_agent" edges to the ApiKey entity.
func (ac *AgentCreate) AddKeyAgent(a ...*ApiKey) *AgentCreate {
	ids := make([]uint64, len(a))
	for i := range a {
		ids[i] = a[i].ID
	}
	return ac.AddKeyAgentIDs(ids...)
}

// Mutation returns the AgentMutation object of the builder.
func (ac *AgentCreate) Mutation() *AgentMutation {
	return ac.mutation
}

// Save creates the Agent in the database.
func (ac *AgentCreate) Save(ctx context.Context) (*Agent, error) {
	if err := ac.defaults(); err != nil {
		return nil, err
	}
	return withHooks(ctx, ac.sqlSave, ac.mutation, ac.hooks)
}

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

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

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

// defaults sets the default values of the builder before save.
func (ac *AgentCreate) defaults() error {
	if _, ok := ac.mutation.CreatedAt(); !ok {
		if agent.DefaultCreatedAt == nil {
			return fmt.Errorf("ent: uninitialized agent.DefaultCreatedAt (forgotten import ent/runtime?)")
		}
		v := agent.DefaultCreatedAt()
		ac.mutation.SetCreatedAt(v)
	}
	if _, ok := ac.mutation.UpdatedAt(); !ok {
		if agent.DefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized agent.DefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := agent.DefaultUpdatedAt()
		ac.mutation.SetUpdatedAt(v)
	}
	if _, ok := ac.mutation.Status(); !ok {
		v := agent.DefaultStatus
		ac.mutation.SetStatus(v)
	}
	if _, ok := ac.mutation.Background(); !ok {
		v := agent.DefaultBackground
		ac.mutation.SetBackground(v)
	}
	if _, ok := ac.mutation.Examples(); !ok {
		v := agent.DefaultExamples
		ac.mutation.SetExamples(v)
	}
	if _, ok := ac.mutation.DatasetID(); !ok {
		v := agent.DefaultDatasetID
		ac.mutation.SetDatasetID(v)
	}
	if _, ok := ac.mutation.CollectionID(); !ok {
		v := agent.DefaultCollectionID
		ac.mutation.SetCollectionID(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (ac *AgentCreate) check() error {
	if _, ok := ac.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Agent.created_at"`)}
	}
	if _, ok := ac.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "Agent.updated_at"`)}
	}
	if _, ok := ac.mutation.Name(); !ok {
		return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "Agent.name"`)}
	}
	if v, ok := ac.mutation.Name(); ok {
		if err := agent.NameValidator(v); err != nil {
			return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "Agent.name": %w`, err)}
		}
	}
	if _, ok := ac.mutation.Role(); !ok {
		return &ValidationError{Name: "role", err: errors.New(`ent: missing required field "Agent.role"`)}
	}
	if v, ok := ac.mutation.Status(); ok {
		if err := agent.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "Agent.status": %w`, err)}
		}
	}
	if _, ok := ac.mutation.OrganizationID(); !ok {
		return &ValidationError{Name: "organization_id", err: errors.New(`ent: missing required field "Agent.organization_id"`)}
	}
	if v, ok := ac.mutation.OrganizationID(); ok {
		if err := agent.OrganizationIDValidator(v); err != nil {
			return &ValidationError{Name: "organization_id", err: fmt.Errorf(`ent: validator failed for field "Agent.organization_id": %w`, err)}
		}
	}
	if _, ok := ac.mutation.DatasetID(); !ok {
		return &ValidationError{Name: "dataset_id", err: errors.New(`ent: missing required field "Agent.dataset_id"`)}
	}
	if v, ok := ac.mutation.DatasetID(); ok {
		if err := agent.DatasetIDValidator(v); err != nil {
			return &ValidationError{Name: "dataset_id", err: fmt.Errorf(`ent: validator failed for field "Agent.dataset_id": %w`, err)}
		}
	}
	if _, ok := ac.mutation.CollectionID(); !ok {
		return &ValidationError{Name: "collection_id", err: errors.New(`ent: missing required field "Agent.collection_id"`)}
	}
	if v, ok := ac.mutation.CollectionID(); ok {
		if err := agent.CollectionIDValidator(v); err != nil {
			return &ValidationError{Name: "collection_id", err: fmt.Errorf(`ent: validator failed for field "Agent.collection_id": %w`, err)}
		}
	}
	return nil
}

func (ac *AgentCreate) sqlSave(ctx context.Context) (*Agent, error) {
	if err := ac.check(); err != nil {
		return nil, err
	}
	_node, _spec := ac.createSpec()
	if err := sqlgraph.CreateNode(ctx, ac.driver, _spec); err != nil {
		if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	if _spec.ID.Value != _node.ID {
		id := _spec.ID.Value.(int64)
		_node.ID = uint64(id)
	}
	ac.mutation.id = &_node.ID
	ac.mutation.done = true
	return _node, nil
}

func (ac *AgentCreate) createSpec() (*Agent, *sqlgraph.CreateSpec) {
	var (
		_node = &Agent{config: ac.config}
		_spec = sqlgraph.NewCreateSpec(agent.Table, sqlgraph.NewFieldSpec(agent.FieldID, field.TypeUint64))
	)
	_spec.OnConflict = ac.conflict
	if id, ok := ac.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := ac.mutation.CreatedAt(); ok {
		_spec.SetField(agent.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := ac.mutation.UpdatedAt(); ok {
		_spec.SetField(agent.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	if value, ok := ac.mutation.DeletedAt(); ok {
		_spec.SetField(agent.FieldDeletedAt, field.TypeTime, value)
		_node.DeletedAt = value
	}
	if value, ok := ac.mutation.Name(); ok {
		_spec.SetField(agent.FieldName, field.TypeString, value)
		_node.Name = value
	}
	if value, ok := ac.mutation.Role(); ok {
		_spec.SetField(agent.FieldRole, field.TypeString, value)
		_node.Role = value
	}
	if value, ok := ac.mutation.Status(); ok {
		_spec.SetField(agent.FieldStatus, field.TypeInt, value)
		_node.Status = value
	}
	if value, ok := ac.mutation.Background(); ok {
		_spec.SetField(agent.FieldBackground, field.TypeString, value)
		_node.Background = value
	}
	if value, ok := ac.mutation.Examples(); ok {
		_spec.SetField(agent.FieldExamples, field.TypeString, value)
		_node.Examples = value
	}
	if value, ok := ac.mutation.OrganizationID(); ok {
		_spec.SetField(agent.FieldOrganizationID, field.TypeUint64, value)
		_node.OrganizationID = value
	}
	if value, ok := ac.mutation.DatasetID(); ok {
		_spec.SetField(agent.FieldDatasetID, field.TypeString, value)
		_node.DatasetID = value
	}
	if value, ok := ac.mutation.CollectionID(); ok {
		_spec.SetField(agent.FieldCollectionID, field.TypeString, value)
		_node.CollectionID = value
	}
	if nodes := ac.mutation.WxAgentIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   agent.WxAgentTable,
			Columns: []string{agent.WxAgentColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(wx.FieldID, field.TypeUint64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := ac.mutation.TokenAgentIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   agent.TokenAgentTable,
			Columns: []string{agent.TokenAgentColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(token.FieldID, field.TypeUint64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := ac.mutation.WaAgentIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   agent.WaAgentTable,
			Columns: []string{agent.WaAgentColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(whatsapp.FieldID, field.TypeUint64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := ac.mutation.KeyAgentIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   agent.KeyAgentTable,
			Columns: []string{agent.KeyAgentColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(apikey.FieldID, field.TypeUint64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_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.Agent.Create().
//		SetCreatedAt(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.AgentUpsert) {
//			SetCreatedAt(v+v).
//		}).
//		Exec(ctx)
func (ac *AgentCreate) OnConflict(opts ...sql.ConflictOption) *AgentUpsertOne {
	ac.conflict = opts
	return &AgentUpsertOne{
		create: ac,
	}
}

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

type (
	// AgentUpsertOne is the builder for "upsert"-ing
	//  one Agent node.
	AgentUpsertOne struct {
		create *AgentCreate
	}

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

// SetUpdatedAt sets the "updated_at" field.
func (u *AgentUpsert) SetUpdatedAt(v time.Time) *AgentUpsert {
	u.Set(agent.FieldUpdatedAt, v)
	return u
}

// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *AgentUpsert) UpdateUpdatedAt() *AgentUpsert {
	u.SetExcluded(agent.FieldUpdatedAt)
	return u
}

// SetDeletedAt sets the "deleted_at" field.
func (u *AgentUpsert) SetDeletedAt(v time.Time) *AgentUpsert {
	u.Set(agent.FieldDeletedAt, v)
	return u
}

// UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create.
func (u *AgentUpsert) UpdateDeletedAt() *AgentUpsert {
	u.SetExcluded(agent.FieldDeletedAt)
	return u
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (u *AgentUpsert) ClearDeletedAt() *AgentUpsert {
	u.SetNull(agent.FieldDeletedAt)
	return u
}

// SetName sets the "name" field.
func (u *AgentUpsert) SetName(v string) *AgentUpsert {
	u.Set(agent.FieldName, v)
	return u
}

// UpdateName sets the "name" field to the value that was provided on create.
func (u *AgentUpsert) UpdateName() *AgentUpsert {
	u.SetExcluded(agent.FieldName)
	return u
}

// SetRole sets the "role" field.
func (u *AgentUpsert) SetRole(v string) *AgentUpsert {
	u.Set(agent.FieldRole, v)
	return u
}

// UpdateRole sets the "role" field to the value that was provided on create.
func (u *AgentUpsert) UpdateRole() *AgentUpsert {
	u.SetExcluded(agent.FieldRole)
	return u
}

// SetStatus sets the "status" field.
func (u *AgentUpsert) SetStatus(v int) *AgentUpsert {
	u.Set(agent.FieldStatus, v)
	return u
}

// UpdateStatus sets the "status" field to the value that was provided on create.
func (u *AgentUpsert) UpdateStatus() *AgentUpsert {
	u.SetExcluded(agent.FieldStatus)
	return u
}

// AddStatus adds v to the "status" field.
func (u *AgentUpsert) AddStatus(v int) *AgentUpsert {
	u.Add(agent.FieldStatus, v)
	return u
}

// ClearStatus clears the value of the "status" field.
func (u *AgentUpsert) ClearStatus() *AgentUpsert {
	u.SetNull(agent.FieldStatus)
	return u
}

// SetBackground sets the "background" field.
func (u *AgentUpsert) SetBackground(v string) *AgentUpsert {
	u.Set(agent.FieldBackground, v)
	return u
}

// UpdateBackground sets the "background" field to the value that was provided on create.
func (u *AgentUpsert) UpdateBackground() *AgentUpsert {
	u.SetExcluded(agent.FieldBackground)
	return u
}

// ClearBackground clears the value of the "background" field.
func (u *AgentUpsert) ClearBackground() *AgentUpsert {
	u.SetNull(agent.FieldBackground)
	return u
}

// SetExamples sets the "examples" field.
func (u *AgentUpsert) SetExamples(v string) *AgentUpsert {
	u.Set(agent.FieldExamples, v)
	return u
}

// UpdateExamples sets the "examples" field to the value that was provided on create.
func (u *AgentUpsert) UpdateExamples() *AgentUpsert {
	u.SetExcluded(agent.FieldExamples)
	return u
}

// ClearExamples clears the value of the "examples" field.
func (u *AgentUpsert) ClearExamples() *AgentUpsert {
	u.SetNull(agent.FieldExamples)
	return u
}

// SetOrganizationID sets the "organization_id" field.
func (u *AgentUpsert) SetOrganizationID(v uint64) *AgentUpsert {
	u.Set(agent.FieldOrganizationID, v)
	return u
}

// UpdateOrganizationID sets the "organization_id" field to the value that was provided on create.
func (u *AgentUpsert) UpdateOrganizationID() *AgentUpsert {
	u.SetExcluded(agent.FieldOrganizationID)
	return u
}

// AddOrganizationID adds v to the "organization_id" field.
func (u *AgentUpsert) AddOrganizationID(v uint64) *AgentUpsert {
	u.Add(agent.FieldOrganizationID, v)
	return u
}

// SetDatasetID sets the "dataset_id" field.
func (u *AgentUpsert) SetDatasetID(v string) *AgentUpsert {
	u.Set(agent.FieldDatasetID, v)
	return u
}

// UpdateDatasetID sets the "dataset_id" field to the value that was provided on create.
func (u *AgentUpsert) UpdateDatasetID() *AgentUpsert {
	u.SetExcluded(agent.FieldDatasetID)
	return u
}

// SetCollectionID sets the "collection_id" field.
func (u *AgentUpsert) SetCollectionID(v string) *AgentUpsert {
	u.Set(agent.FieldCollectionID, v)
	return u
}

// UpdateCollectionID sets the "collection_id" field to the value that was provided on create.
func (u *AgentUpsert) UpdateCollectionID() *AgentUpsert {
	u.SetExcluded(agent.FieldCollectionID)
	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.Agent.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(agent.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *AgentUpsertOne) UpdateNewValues() *AgentUpsertOne {
	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(agent.FieldID)
		}
		if _, exists := u.create.mutation.CreatedAt(); exists {
			s.SetIgnore(agent.FieldCreatedAt)
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.Agent.Create().
//	    OnConflict(sql.ResolveWithIgnore()).
//	    Exec(ctx)
func (u *AgentUpsertOne) Ignore() *AgentUpsertOne {
	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 *AgentUpsertOne) DoNothing() *AgentUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

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

// SetUpdatedAt sets the "updated_at" field.
func (u *AgentUpsertOne) SetUpdatedAt(v time.Time) *AgentUpsertOne {
	return u.Update(func(s *AgentUpsert) {
		s.SetUpdatedAt(v)
	})
}

// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *AgentUpsertOne) UpdateUpdatedAt() *AgentUpsertOne {
	return u.Update(func(s *AgentUpsert) {
		s.UpdateUpdatedAt()
	})
}

// SetDeletedAt sets the "deleted_at" field.
func (u *AgentUpsertOne) SetDeletedAt(v time.Time) *AgentUpsertOne {
	return u.Update(func(s *AgentUpsert) {
		s.SetDeletedAt(v)
	})
}

// UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create.
func (u *AgentUpsertOne) UpdateDeletedAt() *AgentUpsertOne {
	return u.Update(func(s *AgentUpsert) {
		s.UpdateDeletedAt()
	})
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (u *AgentUpsertOne) ClearDeletedAt() *AgentUpsertOne {
	return u.Update(func(s *AgentUpsert) {
		s.ClearDeletedAt()
	})
}

// SetName sets the "name" field.
func (u *AgentUpsertOne) SetName(v string) *AgentUpsertOne {
	return u.Update(func(s *AgentUpsert) {
		s.SetName(v)
	})
}

// UpdateName sets the "name" field to the value that was provided on create.
func (u *AgentUpsertOne) UpdateName() *AgentUpsertOne {
	return u.Update(func(s *AgentUpsert) {
		s.UpdateName()
	})
}

// SetRole sets the "role" field.
func (u *AgentUpsertOne) SetRole(v string) *AgentUpsertOne {
	return u.Update(func(s *AgentUpsert) {
		s.SetRole(v)
	})
}

// UpdateRole sets the "role" field to the value that was provided on create.
func (u *AgentUpsertOne) UpdateRole() *AgentUpsertOne {
	return u.Update(func(s *AgentUpsert) {
		s.UpdateRole()
	})
}

// SetStatus sets the "status" field.
func (u *AgentUpsertOne) SetStatus(v int) *AgentUpsertOne {
	return u.Update(func(s *AgentUpsert) {
		s.SetStatus(v)
	})
}

// AddStatus adds v to the "status" field.
func (u *AgentUpsertOne) AddStatus(v int) *AgentUpsertOne {
	return u.Update(func(s *AgentUpsert) {
		s.AddStatus(v)
	})
}

// UpdateStatus sets the "status" field to the value that was provided on create.
func (u *AgentUpsertOne) UpdateStatus() *AgentUpsertOne {
	return u.Update(func(s *AgentUpsert) {
		s.UpdateStatus()
	})
}

// ClearStatus clears the value of the "status" field.
func (u *AgentUpsertOne) ClearStatus() *AgentUpsertOne {
	return u.Update(func(s *AgentUpsert) {
		s.ClearStatus()
	})
}

// SetBackground sets the "background" field.
func (u *AgentUpsertOne) SetBackground(v string) *AgentUpsertOne {
	return u.Update(func(s *AgentUpsert) {
		s.SetBackground(v)
	})
}

// UpdateBackground sets the "background" field to the value that was provided on create.
func (u *AgentUpsertOne) UpdateBackground() *AgentUpsertOne {
	return u.Update(func(s *AgentUpsert) {
		s.UpdateBackground()
	})
}

// ClearBackground clears the value of the "background" field.
func (u *AgentUpsertOne) ClearBackground() *AgentUpsertOne {
	return u.Update(func(s *AgentUpsert) {
		s.ClearBackground()
	})
}

// SetExamples sets the "examples" field.
func (u *AgentUpsertOne) SetExamples(v string) *AgentUpsertOne {
	return u.Update(func(s *AgentUpsert) {
		s.SetExamples(v)
	})
}

// UpdateExamples sets the "examples" field to the value that was provided on create.
func (u *AgentUpsertOne) UpdateExamples() *AgentUpsertOne {
	return u.Update(func(s *AgentUpsert) {
		s.UpdateExamples()
	})
}

// ClearExamples clears the value of the "examples" field.
func (u *AgentUpsertOne) ClearExamples() *AgentUpsertOne {
	return u.Update(func(s *AgentUpsert) {
		s.ClearExamples()
	})
}

// SetOrganizationID sets the "organization_id" field.
func (u *AgentUpsertOne) SetOrganizationID(v uint64) *AgentUpsertOne {
	return u.Update(func(s *AgentUpsert) {
		s.SetOrganizationID(v)
	})
}

// AddOrganizationID adds v to the "organization_id" field.
func (u *AgentUpsertOne) AddOrganizationID(v uint64) *AgentUpsertOne {
	return u.Update(func(s *AgentUpsert) {
		s.AddOrganizationID(v)
	})
}

// UpdateOrganizationID sets the "organization_id" field to the value that was provided on create.
func (u *AgentUpsertOne) UpdateOrganizationID() *AgentUpsertOne {
	return u.Update(func(s *AgentUpsert) {
		s.UpdateOrganizationID()
	})
}

// SetDatasetID sets the "dataset_id" field.
func (u *AgentUpsertOne) SetDatasetID(v string) *AgentUpsertOne {
	return u.Update(func(s *AgentUpsert) {
		s.SetDatasetID(v)
	})
}

// UpdateDatasetID sets the "dataset_id" field to the value that was provided on create.
func (u *AgentUpsertOne) UpdateDatasetID() *AgentUpsertOne {
	return u.Update(func(s *AgentUpsert) {
		s.UpdateDatasetID()
	})
}

// SetCollectionID sets the "collection_id" field.
func (u *AgentUpsertOne) SetCollectionID(v string) *AgentUpsertOne {
	return u.Update(func(s *AgentUpsert) {
		s.SetCollectionID(v)
	})
}

// UpdateCollectionID sets the "collection_id" field to the value that was provided on create.
func (u *AgentUpsertOne) UpdateCollectionID() *AgentUpsertOne {
	return u.Update(func(s *AgentUpsert) {
		s.UpdateCollectionID()
	})
}

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

// ExecX is like Exec, but panics if an error occurs.
func (u *AgentUpsertOne) 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 *AgentUpsertOne) ID(ctx context.Context) (id uint64, err error) {
	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 *AgentUpsertOne) IDX(ctx context.Context) uint64 {
	id, err := u.ID(ctx)
	if err != nil {
		panic(err)
	}
	return id
}

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

// Save creates the Agent entities in the database.
func (acb *AgentCreateBulk) Save(ctx context.Context) ([]*Agent, error) {
	if acb.err != nil {
		return nil, acb.err
	}
	specs := make([]*sqlgraph.CreateSpec, len(acb.builders))
	nodes := make([]*Agent, len(acb.builders))
	mutators := make([]Mutator, len(acb.builders))
	for i := range acb.builders {
		func(i int, root context.Context) {
			builder := acb.builders[i]
			builder.defaults()
			var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
				mutation, ok := m.(*AgentMutation)
				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, acb.builders[i+1].mutation)
				} else {
					spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
					spec.OnConflict = acb.conflict
					// Invoke the actual operation on the latest mutation in the chain.
					if err = sqlgraph.BatchCreate(ctx, acb.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
				if specs[i].ID.Value != nil && nodes[i].ID == 0 {
					id := specs[i].ID.Value.(int64)
					nodes[i].ID = uint64(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, acb.builders[0].mutation); err != nil {
			return nil, err
		}
	}
	return nodes, nil
}

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

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

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

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.Agent.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.AgentUpsert) {
//			SetCreatedAt(v+v).
//		}).
//		Exec(ctx)
func (acb *AgentCreateBulk) OnConflict(opts ...sql.ConflictOption) *AgentUpsertBulk {
	acb.conflict = opts
	return &AgentUpsertBulk{
		create: acb,
	}
}

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

// AgentUpsertBulk is the builder for "upsert"-ing
// a bulk of Agent nodes.
type AgentUpsertBulk struct {
	create *AgentCreateBulk
}

// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
//	client.Agent.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(agent.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *AgentUpsertBulk) UpdateNewValues() *AgentUpsertBulk {
	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(agent.FieldID)
			}
			if _, exists := b.mutation.CreatedAt(); exists {
				s.SetIgnore(agent.FieldCreatedAt)
			}
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.Agent.Create().
//		OnConflict(sql.ResolveWithIgnore()).
//		Exec(ctx)
func (u *AgentUpsertBulk) Ignore() *AgentUpsertBulk {
	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 *AgentUpsertBulk) DoNothing() *AgentUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

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

// SetUpdatedAt sets the "updated_at" field.
func (u *AgentUpsertBulk) SetUpdatedAt(v time.Time) *AgentUpsertBulk {
	return u.Update(func(s *AgentUpsert) {
		s.SetUpdatedAt(v)
	})
}

// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *AgentUpsertBulk) UpdateUpdatedAt() *AgentUpsertBulk {
	return u.Update(func(s *AgentUpsert) {
		s.UpdateUpdatedAt()
	})
}

// SetDeletedAt sets the "deleted_at" field.
func (u *AgentUpsertBulk) SetDeletedAt(v time.Time) *AgentUpsertBulk {
	return u.Update(func(s *AgentUpsert) {
		s.SetDeletedAt(v)
	})
}

// UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create.
func (u *AgentUpsertBulk) UpdateDeletedAt() *AgentUpsertBulk {
	return u.Update(func(s *AgentUpsert) {
		s.UpdateDeletedAt()
	})
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (u *AgentUpsertBulk) ClearDeletedAt() *AgentUpsertBulk {
	return u.Update(func(s *AgentUpsert) {
		s.ClearDeletedAt()
	})
}

// SetName sets the "name" field.
func (u *AgentUpsertBulk) SetName(v string) *AgentUpsertBulk {
	return u.Update(func(s *AgentUpsert) {
		s.SetName(v)
	})
}

// UpdateName sets the "name" field to the value that was provided on create.
func (u *AgentUpsertBulk) UpdateName() *AgentUpsertBulk {
	return u.Update(func(s *AgentUpsert) {
		s.UpdateName()
	})
}

// SetRole sets the "role" field.
func (u *AgentUpsertBulk) SetRole(v string) *AgentUpsertBulk {
	return u.Update(func(s *AgentUpsert) {
		s.SetRole(v)
	})
}

// UpdateRole sets the "role" field to the value that was provided on create.
func (u *AgentUpsertBulk) UpdateRole() *AgentUpsertBulk {
	return u.Update(func(s *AgentUpsert) {
		s.UpdateRole()
	})
}

// SetStatus sets the "status" field.
func (u *AgentUpsertBulk) SetStatus(v int) *AgentUpsertBulk {
	return u.Update(func(s *AgentUpsert) {
		s.SetStatus(v)
	})
}

// AddStatus adds v to the "status" field.
func (u *AgentUpsertBulk) AddStatus(v int) *AgentUpsertBulk {
	return u.Update(func(s *AgentUpsert) {
		s.AddStatus(v)
	})
}

// UpdateStatus sets the "status" field to the value that was provided on create.
func (u *AgentUpsertBulk) UpdateStatus() *AgentUpsertBulk {
	return u.Update(func(s *AgentUpsert) {
		s.UpdateStatus()
	})
}

// ClearStatus clears the value of the "status" field.
func (u *AgentUpsertBulk) ClearStatus() *AgentUpsertBulk {
	return u.Update(func(s *AgentUpsert) {
		s.ClearStatus()
	})
}

// SetBackground sets the "background" field.
func (u *AgentUpsertBulk) SetBackground(v string) *AgentUpsertBulk {
	return u.Update(func(s *AgentUpsert) {
		s.SetBackground(v)
	})
}

// UpdateBackground sets the "background" field to the value that was provided on create.
func (u *AgentUpsertBulk) UpdateBackground() *AgentUpsertBulk {
	return u.Update(func(s *AgentUpsert) {
		s.UpdateBackground()
	})
}

// ClearBackground clears the value of the "background" field.
func (u *AgentUpsertBulk) ClearBackground() *AgentUpsertBulk {
	return u.Update(func(s *AgentUpsert) {
		s.ClearBackground()
	})
}

// SetExamples sets the "examples" field.
func (u *AgentUpsertBulk) SetExamples(v string) *AgentUpsertBulk {
	return u.Update(func(s *AgentUpsert) {
		s.SetExamples(v)
	})
}

// UpdateExamples sets the "examples" field to the value that was provided on create.
func (u *AgentUpsertBulk) UpdateExamples() *AgentUpsertBulk {
	return u.Update(func(s *AgentUpsert) {
		s.UpdateExamples()
	})
}

// ClearExamples clears the value of the "examples" field.
func (u *AgentUpsertBulk) ClearExamples() *AgentUpsertBulk {
	return u.Update(func(s *AgentUpsert) {
		s.ClearExamples()
	})
}

// SetOrganizationID sets the "organization_id" field.
func (u *AgentUpsertBulk) SetOrganizationID(v uint64) *AgentUpsertBulk {
	return u.Update(func(s *AgentUpsert) {
		s.SetOrganizationID(v)
	})
}

// AddOrganizationID adds v to the "organization_id" field.
func (u *AgentUpsertBulk) AddOrganizationID(v uint64) *AgentUpsertBulk {
	return u.Update(func(s *AgentUpsert) {
		s.AddOrganizationID(v)
	})
}

// UpdateOrganizationID sets the "organization_id" field to the value that was provided on create.
func (u *AgentUpsertBulk) UpdateOrganizationID() *AgentUpsertBulk {
	return u.Update(func(s *AgentUpsert) {
		s.UpdateOrganizationID()
	})
}

// SetDatasetID sets the "dataset_id" field.
func (u *AgentUpsertBulk) SetDatasetID(v string) *AgentUpsertBulk {
	return u.Update(func(s *AgentUpsert) {
		s.SetDatasetID(v)
	})
}

// UpdateDatasetID sets the "dataset_id" field to the value that was provided on create.
func (u *AgentUpsertBulk) UpdateDatasetID() *AgentUpsertBulk {
	return u.Update(func(s *AgentUpsert) {
		s.UpdateDatasetID()
	})
}

// SetCollectionID sets the "collection_id" field.
func (u *AgentUpsertBulk) SetCollectionID(v string) *AgentUpsertBulk {
	return u.Update(func(s *AgentUpsert) {
		s.SetCollectionID(v)
	})
}

// UpdateCollectionID sets the "collection_id" field to the value that was provided on create.
func (u *AgentUpsertBulk) UpdateCollectionID() *AgentUpsertBulk {
	return u.Update(func(s *AgentUpsert) {
		s.UpdateCollectionID()
	})
}

// Exec executes the query.
func (u *AgentUpsertBulk) 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 AgentCreateBulk instead", i)
		}
	}
	if len(u.create.conflict) == 0 {
		return errors.New("ent: missing options for AgentCreateBulk.OnConflict")
	}
	return u.create.Exec(ctx)
}

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