// Code generated by ent, DO NOT EDIT.

package ent

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

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

// ApiKeyCreate is the builder for creating a ApiKey entity.
type ApiKeyCreate struct {
	config
	mutation *ApiKeyMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

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

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

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

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

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

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

// SetTitle sets the "title" field.
func (akc *ApiKeyCreate) SetTitle(s string) *ApiKeyCreate {
	akc.mutation.SetTitle(s)
	return akc
}

// SetNillableTitle sets the "title" field if the given value is not nil.
func (akc *ApiKeyCreate) SetNillableTitle(s *string) *ApiKeyCreate {
	if s != nil {
		akc.SetTitle(*s)
	}
	return akc
}

// SetKey sets the "key" field.
func (akc *ApiKeyCreate) SetKey(s string) *ApiKeyCreate {
	akc.mutation.SetKey(s)
	return akc
}

// SetNillableKey sets the "key" field if the given value is not nil.
func (akc *ApiKeyCreate) SetNillableKey(s *string) *ApiKeyCreate {
	if s != nil {
		akc.SetKey(*s)
	}
	return akc
}

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

// SetNillableOrganizationID sets the "organization_id" field if the given value is not nil.
func (akc *ApiKeyCreate) SetNillableOrganizationID(u *uint64) *ApiKeyCreate {
	if u != nil {
		akc.SetOrganizationID(*u)
	}
	return akc
}

// SetAgentID sets the "agent_id" field.
func (akc *ApiKeyCreate) SetAgentID(u uint64) *ApiKeyCreate {
	akc.mutation.SetAgentID(u)
	return akc
}

// SetNillableAgentID sets the "agent_id" field if the given value is not nil.
func (akc *ApiKeyCreate) SetNillableAgentID(u *uint64) *ApiKeyCreate {
	if u != nil {
		akc.SetAgentID(*u)
	}
	return akc
}

// SetCustomAgentBase sets the "custom_agent_base" field.
func (akc *ApiKeyCreate) SetCustomAgentBase(s string) *ApiKeyCreate {
	akc.mutation.SetCustomAgentBase(s)
	return akc
}

// SetNillableCustomAgentBase sets the "custom_agent_base" field if the given value is not nil.
func (akc *ApiKeyCreate) SetNillableCustomAgentBase(s *string) *ApiKeyCreate {
	if s != nil {
		akc.SetCustomAgentBase(*s)
	}
	return akc
}

// SetCustomAgentKey sets the "custom_agent_key" field.
func (akc *ApiKeyCreate) SetCustomAgentKey(s string) *ApiKeyCreate {
	akc.mutation.SetCustomAgentKey(s)
	return akc
}

// SetNillableCustomAgentKey sets the "custom_agent_key" field if the given value is not nil.
func (akc *ApiKeyCreate) SetNillableCustomAgentKey(s *string) *ApiKeyCreate {
	if s != nil {
		akc.SetCustomAgentKey(*s)
	}
	return akc
}

// SetOpenaiBase sets the "openai_base" field.
func (akc *ApiKeyCreate) SetOpenaiBase(s string) *ApiKeyCreate {
	akc.mutation.SetOpenaiBase(s)
	return akc
}

// SetNillableOpenaiBase sets the "openai_base" field if the given value is not nil.
func (akc *ApiKeyCreate) SetNillableOpenaiBase(s *string) *ApiKeyCreate {
	if s != nil {
		akc.SetOpenaiBase(*s)
	}
	return akc
}

// SetOpenaiKey sets the "openai_key" field.
func (akc *ApiKeyCreate) SetOpenaiKey(s string) *ApiKeyCreate {
	akc.mutation.SetOpenaiKey(s)
	return akc
}

// SetNillableOpenaiKey sets the "openai_key" field if the given value is not nil.
func (akc *ApiKeyCreate) SetNillableOpenaiKey(s *string) *ApiKeyCreate {
	if s != nil {
		akc.SetOpenaiKey(*s)
	}
	return akc
}

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

// SetAgent sets the "agent" edge to the Agent entity.
func (akc *ApiKeyCreate) SetAgent(a *Agent) *ApiKeyCreate {
	return akc.SetAgentID(a.ID)
}

// Mutation returns the ApiKeyMutation object of the builder.
func (akc *ApiKeyCreate) Mutation() *ApiKeyMutation {
	return akc.mutation
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (akc *ApiKeyCreate) defaults() error {
	if _, ok := akc.mutation.CreatedAt(); !ok {
		if apikey.DefaultCreatedAt == nil {
			return fmt.Errorf("ent: uninitialized apikey.DefaultCreatedAt (forgotten import ent/runtime?)")
		}
		v := apikey.DefaultCreatedAt()
		akc.mutation.SetCreatedAt(v)
	}
	if _, ok := akc.mutation.UpdatedAt(); !ok {
		if apikey.DefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized apikey.DefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := apikey.DefaultUpdatedAt()
		akc.mutation.SetUpdatedAt(v)
	}
	if _, ok := akc.mutation.Title(); !ok {
		v := apikey.DefaultTitle
		akc.mutation.SetTitle(v)
	}
	if _, ok := akc.mutation.Key(); !ok {
		v := apikey.DefaultKey
		akc.mutation.SetKey(v)
	}
	if _, ok := akc.mutation.OrganizationID(); !ok {
		v := apikey.DefaultOrganizationID
		akc.mutation.SetOrganizationID(v)
	}
	if _, ok := akc.mutation.AgentID(); !ok {
		v := apikey.DefaultAgentID
		akc.mutation.SetAgentID(v)
	}
	if _, ok := akc.mutation.CustomAgentBase(); !ok {
		v := apikey.DefaultCustomAgentBase
		akc.mutation.SetCustomAgentBase(v)
	}
	if _, ok := akc.mutation.CustomAgentKey(); !ok {
		v := apikey.DefaultCustomAgentKey
		akc.mutation.SetCustomAgentKey(v)
	}
	if _, ok := akc.mutation.OpenaiBase(); !ok {
		v := apikey.DefaultOpenaiBase
		akc.mutation.SetOpenaiBase(v)
	}
	if _, ok := akc.mutation.OpenaiKey(); !ok {
		v := apikey.DefaultOpenaiKey
		akc.mutation.SetOpenaiKey(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (akc *ApiKeyCreate) check() error {
	if _, ok := akc.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "ApiKey.created_at"`)}
	}
	if _, ok := akc.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "ApiKey.updated_at"`)}
	}
	if _, ok := akc.mutation.OrganizationID(); !ok {
		return &ValidationError{Name: "organization_id", err: errors.New(`ent: missing required field "ApiKey.organization_id"`)}
	}
	if _, ok := akc.mutation.AgentID(); !ok {
		return &ValidationError{Name: "agent_id", err: errors.New(`ent: missing required field "ApiKey.agent_id"`)}
	}
	if _, ok := akc.mutation.AgentID(); !ok {
		return &ValidationError{Name: "agent", err: errors.New(`ent: missing required edge "ApiKey.agent"`)}
	}
	return nil
}

func (akc *ApiKeyCreate) sqlSave(ctx context.Context) (*ApiKey, error) {
	if err := akc.check(); err != nil {
		return nil, err
	}
	_node, _spec := akc.createSpec()
	if err := sqlgraph.CreateNode(ctx, akc.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)
	}
	akc.mutation.id = &_node.ID
	akc.mutation.done = true
	return _node, nil
}

func (akc *ApiKeyCreate) createSpec() (*ApiKey, *sqlgraph.CreateSpec) {
	var (
		_node = &ApiKey{config: akc.config}
		_spec = sqlgraph.NewCreateSpec(apikey.Table, sqlgraph.NewFieldSpec(apikey.FieldID, field.TypeUint64))
	)
	_spec.OnConflict = akc.conflict
	if id, ok := akc.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := akc.mutation.CreatedAt(); ok {
		_spec.SetField(apikey.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := akc.mutation.UpdatedAt(); ok {
		_spec.SetField(apikey.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	if value, ok := akc.mutation.DeletedAt(); ok {
		_spec.SetField(apikey.FieldDeletedAt, field.TypeTime, value)
		_node.DeletedAt = value
	}
	if value, ok := akc.mutation.Title(); ok {
		_spec.SetField(apikey.FieldTitle, field.TypeString, value)
		_node.Title = value
	}
	if value, ok := akc.mutation.Key(); ok {
		_spec.SetField(apikey.FieldKey, field.TypeString, value)
		_node.Key = value
	}
	if value, ok := akc.mutation.OrganizationID(); ok {
		_spec.SetField(apikey.FieldOrganizationID, field.TypeUint64, value)
		_node.OrganizationID = value
	}
	if value, ok := akc.mutation.CustomAgentBase(); ok {
		_spec.SetField(apikey.FieldCustomAgentBase, field.TypeString, value)
		_node.CustomAgentBase = value
	}
	if value, ok := akc.mutation.CustomAgentKey(); ok {
		_spec.SetField(apikey.FieldCustomAgentKey, field.TypeString, value)
		_node.CustomAgentKey = value
	}
	if value, ok := akc.mutation.OpenaiBase(); ok {
		_spec.SetField(apikey.FieldOpenaiBase, field.TypeString, value)
		_node.OpenaiBase = value
	}
	if value, ok := akc.mutation.OpenaiKey(); ok {
		_spec.SetField(apikey.FieldOpenaiKey, field.TypeString, value)
		_node.OpenaiKey = value
	}
	if nodes := akc.mutation.AgentIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   apikey.AgentTable,
			Columns: []string{apikey.AgentColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(agent.FieldID, field.TypeUint64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.AgentID = 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.ApiKey.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.ApiKeyUpsert) {
//			SetCreatedAt(v+v).
//		}).
//		Exec(ctx)
func (akc *ApiKeyCreate) OnConflict(opts ...sql.ConflictOption) *ApiKeyUpsertOne {
	akc.conflict = opts
	return &ApiKeyUpsertOne{
		create: akc,
	}
}

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

type (
	// ApiKeyUpsertOne is the builder for "upsert"-ing
	//  one ApiKey node.
	ApiKeyUpsertOne struct {
		create *ApiKeyCreate
	}

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

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

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

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

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

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

// SetTitle sets the "title" field.
func (u *ApiKeyUpsert) SetTitle(v string) *ApiKeyUpsert {
	u.Set(apikey.FieldTitle, v)
	return u
}

// UpdateTitle sets the "title" field to the value that was provided on create.
func (u *ApiKeyUpsert) UpdateTitle() *ApiKeyUpsert {
	u.SetExcluded(apikey.FieldTitle)
	return u
}

// ClearTitle clears the value of the "title" field.
func (u *ApiKeyUpsert) ClearTitle() *ApiKeyUpsert {
	u.SetNull(apikey.FieldTitle)
	return u
}

// SetKey sets the "key" field.
func (u *ApiKeyUpsert) SetKey(v string) *ApiKeyUpsert {
	u.Set(apikey.FieldKey, v)
	return u
}

// UpdateKey sets the "key" field to the value that was provided on create.
func (u *ApiKeyUpsert) UpdateKey() *ApiKeyUpsert {
	u.SetExcluded(apikey.FieldKey)
	return u
}

// ClearKey clears the value of the "key" field.
func (u *ApiKeyUpsert) ClearKey() *ApiKeyUpsert {
	u.SetNull(apikey.FieldKey)
	return u
}

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

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

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

// SetAgentID sets the "agent_id" field.
func (u *ApiKeyUpsert) SetAgentID(v uint64) *ApiKeyUpsert {
	u.Set(apikey.FieldAgentID, v)
	return u
}

// UpdateAgentID sets the "agent_id" field to the value that was provided on create.
func (u *ApiKeyUpsert) UpdateAgentID() *ApiKeyUpsert {
	u.SetExcluded(apikey.FieldAgentID)
	return u
}

// SetCustomAgentBase sets the "custom_agent_base" field.
func (u *ApiKeyUpsert) SetCustomAgentBase(v string) *ApiKeyUpsert {
	u.Set(apikey.FieldCustomAgentBase, v)
	return u
}

// UpdateCustomAgentBase sets the "custom_agent_base" field to the value that was provided on create.
func (u *ApiKeyUpsert) UpdateCustomAgentBase() *ApiKeyUpsert {
	u.SetExcluded(apikey.FieldCustomAgentBase)
	return u
}

// ClearCustomAgentBase clears the value of the "custom_agent_base" field.
func (u *ApiKeyUpsert) ClearCustomAgentBase() *ApiKeyUpsert {
	u.SetNull(apikey.FieldCustomAgentBase)
	return u
}

// SetCustomAgentKey sets the "custom_agent_key" field.
func (u *ApiKeyUpsert) SetCustomAgentKey(v string) *ApiKeyUpsert {
	u.Set(apikey.FieldCustomAgentKey, v)
	return u
}

// UpdateCustomAgentKey sets the "custom_agent_key" field to the value that was provided on create.
func (u *ApiKeyUpsert) UpdateCustomAgentKey() *ApiKeyUpsert {
	u.SetExcluded(apikey.FieldCustomAgentKey)
	return u
}

// ClearCustomAgentKey clears the value of the "custom_agent_key" field.
func (u *ApiKeyUpsert) ClearCustomAgentKey() *ApiKeyUpsert {
	u.SetNull(apikey.FieldCustomAgentKey)
	return u
}

// SetOpenaiBase sets the "openai_base" field.
func (u *ApiKeyUpsert) SetOpenaiBase(v string) *ApiKeyUpsert {
	u.Set(apikey.FieldOpenaiBase, v)
	return u
}

// UpdateOpenaiBase sets the "openai_base" field to the value that was provided on create.
func (u *ApiKeyUpsert) UpdateOpenaiBase() *ApiKeyUpsert {
	u.SetExcluded(apikey.FieldOpenaiBase)
	return u
}

// ClearOpenaiBase clears the value of the "openai_base" field.
func (u *ApiKeyUpsert) ClearOpenaiBase() *ApiKeyUpsert {
	u.SetNull(apikey.FieldOpenaiBase)
	return u
}

// SetOpenaiKey sets the "openai_key" field.
func (u *ApiKeyUpsert) SetOpenaiKey(v string) *ApiKeyUpsert {
	u.Set(apikey.FieldOpenaiKey, v)
	return u
}

// UpdateOpenaiKey sets the "openai_key" field to the value that was provided on create.
func (u *ApiKeyUpsert) UpdateOpenaiKey() *ApiKeyUpsert {
	u.SetExcluded(apikey.FieldOpenaiKey)
	return u
}

// ClearOpenaiKey clears the value of the "openai_key" field.
func (u *ApiKeyUpsert) ClearOpenaiKey() *ApiKeyUpsert {
	u.SetNull(apikey.FieldOpenaiKey)
	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.ApiKey.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(apikey.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *ApiKeyUpsertOne) UpdateNewValues() *ApiKeyUpsertOne {
	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(apikey.FieldID)
		}
		if _, exists := u.create.mutation.CreatedAt(); exists {
			s.SetIgnore(apikey.FieldCreatedAt)
		}
	}))
	return u
}

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

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

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

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

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

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

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

// SetTitle sets the "title" field.
func (u *ApiKeyUpsertOne) SetTitle(v string) *ApiKeyUpsertOne {
	return u.Update(func(s *ApiKeyUpsert) {
		s.SetTitle(v)
	})
}

// UpdateTitle sets the "title" field to the value that was provided on create.
func (u *ApiKeyUpsertOne) UpdateTitle() *ApiKeyUpsertOne {
	return u.Update(func(s *ApiKeyUpsert) {
		s.UpdateTitle()
	})
}

// ClearTitle clears the value of the "title" field.
func (u *ApiKeyUpsertOne) ClearTitle() *ApiKeyUpsertOne {
	return u.Update(func(s *ApiKeyUpsert) {
		s.ClearTitle()
	})
}

// SetKey sets the "key" field.
func (u *ApiKeyUpsertOne) SetKey(v string) *ApiKeyUpsertOne {
	return u.Update(func(s *ApiKeyUpsert) {
		s.SetKey(v)
	})
}

// UpdateKey sets the "key" field to the value that was provided on create.
func (u *ApiKeyUpsertOne) UpdateKey() *ApiKeyUpsertOne {
	return u.Update(func(s *ApiKeyUpsert) {
		s.UpdateKey()
	})
}

// ClearKey clears the value of the "key" field.
func (u *ApiKeyUpsertOne) ClearKey() *ApiKeyUpsertOne {
	return u.Update(func(s *ApiKeyUpsert) {
		s.ClearKey()
	})
}

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

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

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

// SetAgentID sets the "agent_id" field.
func (u *ApiKeyUpsertOne) SetAgentID(v uint64) *ApiKeyUpsertOne {
	return u.Update(func(s *ApiKeyUpsert) {
		s.SetAgentID(v)
	})
}

// UpdateAgentID sets the "agent_id" field to the value that was provided on create.
func (u *ApiKeyUpsertOne) UpdateAgentID() *ApiKeyUpsertOne {
	return u.Update(func(s *ApiKeyUpsert) {
		s.UpdateAgentID()
	})
}

// SetCustomAgentBase sets the "custom_agent_base" field.
func (u *ApiKeyUpsertOne) SetCustomAgentBase(v string) *ApiKeyUpsertOne {
	return u.Update(func(s *ApiKeyUpsert) {
		s.SetCustomAgentBase(v)
	})
}

// UpdateCustomAgentBase sets the "custom_agent_base" field to the value that was provided on create.
func (u *ApiKeyUpsertOne) UpdateCustomAgentBase() *ApiKeyUpsertOne {
	return u.Update(func(s *ApiKeyUpsert) {
		s.UpdateCustomAgentBase()
	})
}

// ClearCustomAgentBase clears the value of the "custom_agent_base" field.
func (u *ApiKeyUpsertOne) ClearCustomAgentBase() *ApiKeyUpsertOne {
	return u.Update(func(s *ApiKeyUpsert) {
		s.ClearCustomAgentBase()
	})
}

// SetCustomAgentKey sets the "custom_agent_key" field.
func (u *ApiKeyUpsertOne) SetCustomAgentKey(v string) *ApiKeyUpsertOne {
	return u.Update(func(s *ApiKeyUpsert) {
		s.SetCustomAgentKey(v)
	})
}

// UpdateCustomAgentKey sets the "custom_agent_key" field to the value that was provided on create.
func (u *ApiKeyUpsertOne) UpdateCustomAgentKey() *ApiKeyUpsertOne {
	return u.Update(func(s *ApiKeyUpsert) {
		s.UpdateCustomAgentKey()
	})
}

// ClearCustomAgentKey clears the value of the "custom_agent_key" field.
func (u *ApiKeyUpsertOne) ClearCustomAgentKey() *ApiKeyUpsertOne {
	return u.Update(func(s *ApiKeyUpsert) {
		s.ClearCustomAgentKey()
	})
}

// SetOpenaiBase sets the "openai_base" field.
func (u *ApiKeyUpsertOne) SetOpenaiBase(v string) *ApiKeyUpsertOne {
	return u.Update(func(s *ApiKeyUpsert) {
		s.SetOpenaiBase(v)
	})
}

// UpdateOpenaiBase sets the "openai_base" field to the value that was provided on create.
func (u *ApiKeyUpsertOne) UpdateOpenaiBase() *ApiKeyUpsertOne {
	return u.Update(func(s *ApiKeyUpsert) {
		s.UpdateOpenaiBase()
	})
}

// ClearOpenaiBase clears the value of the "openai_base" field.
func (u *ApiKeyUpsertOne) ClearOpenaiBase() *ApiKeyUpsertOne {
	return u.Update(func(s *ApiKeyUpsert) {
		s.ClearOpenaiBase()
	})
}

// SetOpenaiKey sets the "openai_key" field.
func (u *ApiKeyUpsertOne) SetOpenaiKey(v string) *ApiKeyUpsertOne {
	return u.Update(func(s *ApiKeyUpsert) {
		s.SetOpenaiKey(v)
	})
}

// UpdateOpenaiKey sets the "openai_key" field to the value that was provided on create.
func (u *ApiKeyUpsertOne) UpdateOpenaiKey() *ApiKeyUpsertOne {
	return u.Update(func(s *ApiKeyUpsert) {
		s.UpdateOpenaiKey()
	})
}

// ClearOpenaiKey clears the value of the "openai_key" field.
func (u *ApiKeyUpsertOne) ClearOpenaiKey() *ApiKeyUpsertOne {
	return u.Update(func(s *ApiKeyUpsert) {
		s.ClearOpenaiKey()
	})
}

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

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

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

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

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

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

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

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

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

// ApiKeyUpsertBulk is the builder for "upsert"-ing
// a bulk of ApiKey nodes.
type ApiKeyUpsertBulk struct {
	create *ApiKeyCreateBulk
}

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

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

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

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

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

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

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

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

// SetTitle sets the "title" field.
func (u *ApiKeyUpsertBulk) SetTitle(v string) *ApiKeyUpsertBulk {
	return u.Update(func(s *ApiKeyUpsert) {
		s.SetTitle(v)
	})
}

// UpdateTitle sets the "title" field to the value that was provided on create.
func (u *ApiKeyUpsertBulk) UpdateTitle() *ApiKeyUpsertBulk {
	return u.Update(func(s *ApiKeyUpsert) {
		s.UpdateTitle()
	})
}

// ClearTitle clears the value of the "title" field.
func (u *ApiKeyUpsertBulk) ClearTitle() *ApiKeyUpsertBulk {
	return u.Update(func(s *ApiKeyUpsert) {
		s.ClearTitle()
	})
}

// SetKey sets the "key" field.
func (u *ApiKeyUpsertBulk) SetKey(v string) *ApiKeyUpsertBulk {
	return u.Update(func(s *ApiKeyUpsert) {
		s.SetKey(v)
	})
}

// UpdateKey sets the "key" field to the value that was provided on create.
func (u *ApiKeyUpsertBulk) UpdateKey() *ApiKeyUpsertBulk {
	return u.Update(func(s *ApiKeyUpsert) {
		s.UpdateKey()
	})
}

// ClearKey clears the value of the "key" field.
func (u *ApiKeyUpsertBulk) ClearKey() *ApiKeyUpsertBulk {
	return u.Update(func(s *ApiKeyUpsert) {
		s.ClearKey()
	})
}

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

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

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

// SetAgentID sets the "agent_id" field.
func (u *ApiKeyUpsertBulk) SetAgentID(v uint64) *ApiKeyUpsertBulk {
	return u.Update(func(s *ApiKeyUpsert) {
		s.SetAgentID(v)
	})
}

// UpdateAgentID sets the "agent_id" field to the value that was provided on create.
func (u *ApiKeyUpsertBulk) UpdateAgentID() *ApiKeyUpsertBulk {
	return u.Update(func(s *ApiKeyUpsert) {
		s.UpdateAgentID()
	})
}

// SetCustomAgentBase sets the "custom_agent_base" field.
func (u *ApiKeyUpsertBulk) SetCustomAgentBase(v string) *ApiKeyUpsertBulk {
	return u.Update(func(s *ApiKeyUpsert) {
		s.SetCustomAgentBase(v)
	})
}

// UpdateCustomAgentBase sets the "custom_agent_base" field to the value that was provided on create.
func (u *ApiKeyUpsertBulk) UpdateCustomAgentBase() *ApiKeyUpsertBulk {
	return u.Update(func(s *ApiKeyUpsert) {
		s.UpdateCustomAgentBase()
	})
}

// ClearCustomAgentBase clears the value of the "custom_agent_base" field.
func (u *ApiKeyUpsertBulk) ClearCustomAgentBase() *ApiKeyUpsertBulk {
	return u.Update(func(s *ApiKeyUpsert) {
		s.ClearCustomAgentBase()
	})
}

// SetCustomAgentKey sets the "custom_agent_key" field.
func (u *ApiKeyUpsertBulk) SetCustomAgentKey(v string) *ApiKeyUpsertBulk {
	return u.Update(func(s *ApiKeyUpsert) {
		s.SetCustomAgentKey(v)
	})
}

// UpdateCustomAgentKey sets the "custom_agent_key" field to the value that was provided on create.
func (u *ApiKeyUpsertBulk) UpdateCustomAgentKey() *ApiKeyUpsertBulk {
	return u.Update(func(s *ApiKeyUpsert) {
		s.UpdateCustomAgentKey()
	})
}

// ClearCustomAgentKey clears the value of the "custom_agent_key" field.
func (u *ApiKeyUpsertBulk) ClearCustomAgentKey() *ApiKeyUpsertBulk {
	return u.Update(func(s *ApiKeyUpsert) {
		s.ClearCustomAgentKey()
	})
}

// SetOpenaiBase sets the "openai_base" field.
func (u *ApiKeyUpsertBulk) SetOpenaiBase(v string) *ApiKeyUpsertBulk {
	return u.Update(func(s *ApiKeyUpsert) {
		s.SetOpenaiBase(v)
	})
}

// UpdateOpenaiBase sets the "openai_base" field to the value that was provided on create.
func (u *ApiKeyUpsertBulk) UpdateOpenaiBase() *ApiKeyUpsertBulk {
	return u.Update(func(s *ApiKeyUpsert) {
		s.UpdateOpenaiBase()
	})
}

// ClearOpenaiBase clears the value of the "openai_base" field.
func (u *ApiKeyUpsertBulk) ClearOpenaiBase() *ApiKeyUpsertBulk {
	return u.Update(func(s *ApiKeyUpsert) {
		s.ClearOpenaiBase()
	})
}

// SetOpenaiKey sets the "openai_key" field.
func (u *ApiKeyUpsertBulk) SetOpenaiKey(v string) *ApiKeyUpsertBulk {
	return u.Update(func(s *ApiKeyUpsert) {
		s.SetOpenaiKey(v)
	})
}

// UpdateOpenaiKey sets the "openai_key" field to the value that was provided on create.
func (u *ApiKeyUpsertBulk) UpdateOpenaiKey() *ApiKeyUpsertBulk {
	return u.Update(func(s *ApiKeyUpsert) {
		s.UpdateOpenaiKey()
	})
}

// ClearOpenaiKey clears the value of the "openai_key" field.
func (u *ApiKeyUpsertBulk) ClearOpenaiKey() *ApiKeyUpsertBulk {
	return u.Update(func(s *ApiKeyUpsert) {
		s.ClearOpenaiKey()
	})
}

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

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