// 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/predicate"

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

// ApiKeyUpdate is the builder for updating ApiKey entities.
type ApiKeyUpdate struct {
	config
	hooks    []Hook
	mutation *ApiKeyMutation
}

// Where appends a list predicates to the ApiKeyUpdate builder.
func (aku *ApiKeyUpdate) Where(ps ...predicate.ApiKey) *ApiKeyUpdate {
	aku.mutation.Where(ps...)
	return aku
}

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

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

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

// ClearDeletedAt clears the value of the "deleted_at" field.
func (aku *ApiKeyUpdate) ClearDeletedAt() *ApiKeyUpdate {
	aku.mutation.ClearDeletedAt()
	return aku
}

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

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

// ClearTitle clears the value of the "title" field.
func (aku *ApiKeyUpdate) ClearTitle() *ApiKeyUpdate {
	aku.mutation.ClearTitle()
	return aku
}

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

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

// ClearKey clears the value of the "key" field.
func (aku *ApiKeyUpdate) ClearKey() *ApiKeyUpdate {
	aku.mutation.ClearKey()
	return aku
}

// SetOrganizationID sets the "organization_id" field.
func (aku *ApiKeyUpdate) SetOrganizationID(u uint64) *ApiKeyUpdate {
	aku.mutation.ResetOrganizationID()
	aku.mutation.SetOrganizationID(u)
	return aku
}

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

// AddOrganizationID adds u to the "organization_id" field.
func (aku *ApiKeyUpdate) AddOrganizationID(u int64) *ApiKeyUpdate {
	aku.mutation.AddOrganizationID(u)
	return aku
}

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

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

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

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

// ClearCustomAgentBase clears the value of the "custom_agent_base" field.
func (aku *ApiKeyUpdate) ClearCustomAgentBase() *ApiKeyUpdate {
	aku.mutation.ClearCustomAgentBase()
	return aku
}

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

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

// ClearCustomAgentKey clears the value of the "custom_agent_key" field.
func (aku *ApiKeyUpdate) ClearCustomAgentKey() *ApiKeyUpdate {
	aku.mutation.ClearCustomAgentKey()
	return aku
}

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

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

// ClearOpenaiBase clears the value of the "openai_base" field.
func (aku *ApiKeyUpdate) ClearOpenaiBase() *ApiKeyUpdate {
	aku.mutation.ClearOpenaiBase()
	return aku
}

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

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

// ClearOpenaiKey clears the value of the "openai_key" field.
func (aku *ApiKeyUpdate) ClearOpenaiKey() *ApiKeyUpdate {
	aku.mutation.ClearOpenaiKey()
	return aku
}

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

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

// ClearAgent clears the "agent" edge to the Agent entity.
func (aku *ApiKeyUpdate) ClearAgent() *ApiKeyUpdate {
	aku.mutation.ClearAgent()
	return aku
}

// Save executes the query and returns the number of nodes affected by the update operation.
func (aku *ApiKeyUpdate) Save(ctx context.Context) (int, error) {
	if err := aku.defaults(); err != nil {
		return 0, err
	}
	return withHooks(ctx, aku.sqlSave, aku.mutation, aku.hooks)
}

// SaveX is like Save, but panics if an error occurs.
func (aku *ApiKeyUpdate) SaveX(ctx context.Context) int {
	affected, err := aku.Save(ctx)
	if err != nil {
		panic(err)
	}
	return affected
}

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

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

// defaults sets the default values of the builder before save.
func (aku *ApiKeyUpdate) defaults() error {
	if _, ok := aku.mutation.UpdatedAt(); !ok {
		if apikey.UpdateDefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized apikey.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := apikey.UpdateDefaultUpdatedAt()
		aku.mutation.SetUpdatedAt(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (aku *ApiKeyUpdate) check() error {
	if _, ok := aku.mutation.AgentID(); aku.mutation.AgentCleared() && !ok {
		return errors.New(`ent: clearing a required unique edge "ApiKey.agent"`)
	}
	return nil
}

func (aku *ApiKeyUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := aku.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(apikey.Table, apikey.Columns, sqlgraph.NewFieldSpec(apikey.FieldID, field.TypeUint64))
	if ps := aku.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := aku.mutation.UpdatedAt(); ok {
		_spec.SetField(apikey.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := aku.mutation.DeletedAt(); ok {
		_spec.SetField(apikey.FieldDeletedAt, field.TypeTime, value)
	}
	if aku.mutation.DeletedAtCleared() {
		_spec.ClearField(apikey.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := aku.mutation.Title(); ok {
		_spec.SetField(apikey.FieldTitle, field.TypeString, value)
	}
	if aku.mutation.TitleCleared() {
		_spec.ClearField(apikey.FieldTitle, field.TypeString)
	}
	if value, ok := aku.mutation.Key(); ok {
		_spec.SetField(apikey.FieldKey, field.TypeString, value)
	}
	if aku.mutation.KeyCleared() {
		_spec.ClearField(apikey.FieldKey, field.TypeString)
	}
	if value, ok := aku.mutation.OrganizationID(); ok {
		_spec.SetField(apikey.FieldOrganizationID, field.TypeUint64, value)
	}
	if value, ok := aku.mutation.AddedOrganizationID(); ok {
		_spec.AddField(apikey.FieldOrganizationID, field.TypeUint64, value)
	}
	if value, ok := aku.mutation.CustomAgentBase(); ok {
		_spec.SetField(apikey.FieldCustomAgentBase, field.TypeString, value)
	}
	if aku.mutation.CustomAgentBaseCleared() {
		_spec.ClearField(apikey.FieldCustomAgentBase, field.TypeString)
	}
	if value, ok := aku.mutation.CustomAgentKey(); ok {
		_spec.SetField(apikey.FieldCustomAgentKey, field.TypeString, value)
	}
	if aku.mutation.CustomAgentKeyCleared() {
		_spec.ClearField(apikey.FieldCustomAgentKey, field.TypeString)
	}
	if value, ok := aku.mutation.OpenaiBase(); ok {
		_spec.SetField(apikey.FieldOpenaiBase, field.TypeString, value)
	}
	if aku.mutation.OpenaiBaseCleared() {
		_spec.ClearField(apikey.FieldOpenaiBase, field.TypeString)
	}
	if value, ok := aku.mutation.OpenaiKey(); ok {
		_spec.SetField(apikey.FieldOpenaiKey, field.TypeString, value)
	}
	if aku.mutation.OpenaiKeyCleared() {
		_spec.ClearField(apikey.FieldOpenaiKey, field.TypeString)
	}
	if aku.mutation.AgentCleared() {
		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),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := aku.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)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, aku.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{apikey.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	aku.mutation.done = true
	return n, nil
}

// ApiKeyUpdateOne is the builder for updating a single ApiKey entity.
type ApiKeyUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *ApiKeyMutation
}

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

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

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

// ClearDeletedAt clears the value of the "deleted_at" field.
func (akuo *ApiKeyUpdateOne) ClearDeletedAt() *ApiKeyUpdateOne {
	akuo.mutation.ClearDeletedAt()
	return akuo
}

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

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

// ClearTitle clears the value of the "title" field.
func (akuo *ApiKeyUpdateOne) ClearTitle() *ApiKeyUpdateOne {
	akuo.mutation.ClearTitle()
	return akuo
}

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

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

// ClearKey clears the value of the "key" field.
func (akuo *ApiKeyUpdateOne) ClearKey() *ApiKeyUpdateOne {
	akuo.mutation.ClearKey()
	return akuo
}

// SetOrganizationID sets the "organization_id" field.
func (akuo *ApiKeyUpdateOne) SetOrganizationID(u uint64) *ApiKeyUpdateOne {
	akuo.mutation.ResetOrganizationID()
	akuo.mutation.SetOrganizationID(u)
	return akuo
}

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

// AddOrganizationID adds u to the "organization_id" field.
func (akuo *ApiKeyUpdateOne) AddOrganizationID(u int64) *ApiKeyUpdateOne {
	akuo.mutation.AddOrganizationID(u)
	return akuo
}

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

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

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

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

// ClearCustomAgentBase clears the value of the "custom_agent_base" field.
func (akuo *ApiKeyUpdateOne) ClearCustomAgentBase() *ApiKeyUpdateOne {
	akuo.mutation.ClearCustomAgentBase()
	return akuo
}

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

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

// ClearCustomAgentKey clears the value of the "custom_agent_key" field.
func (akuo *ApiKeyUpdateOne) ClearCustomAgentKey() *ApiKeyUpdateOne {
	akuo.mutation.ClearCustomAgentKey()
	return akuo
}

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

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

// ClearOpenaiBase clears the value of the "openai_base" field.
func (akuo *ApiKeyUpdateOne) ClearOpenaiBase() *ApiKeyUpdateOne {
	akuo.mutation.ClearOpenaiBase()
	return akuo
}

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

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

// ClearOpenaiKey clears the value of the "openai_key" field.
func (akuo *ApiKeyUpdateOne) ClearOpenaiKey() *ApiKeyUpdateOne {
	akuo.mutation.ClearOpenaiKey()
	return akuo
}

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

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

// ClearAgent clears the "agent" edge to the Agent entity.
func (akuo *ApiKeyUpdateOne) ClearAgent() *ApiKeyUpdateOne {
	akuo.mutation.ClearAgent()
	return akuo
}

// Where appends a list predicates to the ApiKeyUpdate builder.
func (akuo *ApiKeyUpdateOne) Where(ps ...predicate.ApiKey) *ApiKeyUpdateOne {
	akuo.mutation.Where(ps...)
	return akuo
}

// Select allows selecting one or more fields (columns) of the returned entity.
// The default is selecting all fields defined in the entity schema.
func (akuo *ApiKeyUpdateOne) Select(field string, fields ...string) *ApiKeyUpdateOne {
	akuo.fields = append([]string{field}, fields...)
	return akuo
}

// Save executes the query and returns the updated ApiKey entity.
func (akuo *ApiKeyUpdateOne) Save(ctx context.Context) (*ApiKey, error) {
	if err := akuo.defaults(); err != nil {
		return nil, err
	}
	return withHooks(ctx, akuo.sqlSave, akuo.mutation, akuo.hooks)
}

// SaveX is like Save, but panics if an error occurs.
func (akuo *ApiKeyUpdateOne) SaveX(ctx context.Context) *ApiKey {
	node, err := akuo.Save(ctx)
	if err != nil {
		panic(err)
	}
	return node
}

// Exec executes the query on the entity.
func (akuo *ApiKeyUpdateOne) Exec(ctx context.Context) error {
	_, err := akuo.Save(ctx)
	return err
}

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

// defaults sets the default values of the builder before save.
func (akuo *ApiKeyUpdateOne) defaults() error {
	if _, ok := akuo.mutation.UpdatedAt(); !ok {
		if apikey.UpdateDefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized apikey.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := apikey.UpdateDefaultUpdatedAt()
		akuo.mutation.SetUpdatedAt(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (akuo *ApiKeyUpdateOne) check() error {
	if _, ok := akuo.mutation.AgentID(); akuo.mutation.AgentCleared() && !ok {
		return errors.New(`ent: clearing a required unique edge "ApiKey.agent"`)
	}
	return nil
}

func (akuo *ApiKeyUpdateOne) sqlSave(ctx context.Context) (_node *ApiKey, err error) {
	if err := akuo.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(apikey.Table, apikey.Columns, sqlgraph.NewFieldSpec(apikey.FieldID, field.TypeUint64))
	id, ok := akuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "ApiKey.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := akuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, apikey.FieldID)
		for _, f := range fields {
			if !apikey.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != apikey.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := akuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := akuo.mutation.UpdatedAt(); ok {
		_spec.SetField(apikey.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := akuo.mutation.DeletedAt(); ok {
		_spec.SetField(apikey.FieldDeletedAt, field.TypeTime, value)
	}
	if akuo.mutation.DeletedAtCleared() {
		_spec.ClearField(apikey.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := akuo.mutation.Title(); ok {
		_spec.SetField(apikey.FieldTitle, field.TypeString, value)
	}
	if akuo.mutation.TitleCleared() {
		_spec.ClearField(apikey.FieldTitle, field.TypeString)
	}
	if value, ok := akuo.mutation.Key(); ok {
		_spec.SetField(apikey.FieldKey, field.TypeString, value)
	}
	if akuo.mutation.KeyCleared() {
		_spec.ClearField(apikey.FieldKey, field.TypeString)
	}
	if value, ok := akuo.mutation.OrganizationID(); ok {
		_spec.SetField(apikey.FieldOrganizationID, field.TypeUint64, value)
	}
	if value, ok := akuo.mutation.AddedOrganizationID(); ok {
		_spec.AddField(apikey.FieldOrganizationID, field.TypeUint64, value)
	}
	if value, ok := akuo.mutation.CustomAgentBase(); ok {
		_spec.SetField(apikey.FieldCustomAgentBase, field.TypeString, value)
	}
	if akuo.mutation.CustomAgentBaseCleared() {
		_spec.ClearField(apikey.FieldCustomAgentBase, field.TypeString)
	}
	if value, ok := akuo.mutation.CustomAgentKey(); ok {
		_spec.SetField(apikey.FieldCustomAgentKey, field.TypeString, value)
	}
	if akuo.mutation.CustomAgentKeyCleared() {
		_spec.ClearField(apikey.FieldCustomAgentKey, field.TypeString)
	}
	if value, ok := akuo.mutation.OpenaiBase(); ok {
		_spec.SetField(apikey.FieldOpenaiBase, field.TypeString, value)
	}
	if akuo.mutation.OpenaiBaseCleared() {
		_spec.ClearField(apikey.FieldOpenaiBase, field.TypeString)
	}
	if value, ok := akuo.mutation.OpenaiKey(); ok {
		_spec.SetField(apikey.FieldOpenaiKey, field.TypeString, value)
	}
	if akuo.mutation.OpenaiKeyCleared() {
		_spec.ClearField(apikey.FieldOpenaiKey, field.TypeString)
	}
	if akuo.mutation.AgentCleared() {
		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),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := akuo.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)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &ApiKey{config: akuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, akuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{apikey.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	akuo.mutation.done = true
	return _node, nil
}
