// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"time"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/cloudreve/Cloudreve/v4/ent/oauthclient"
	"github.com/cloudreve/Cloudreve/v4/ent/oauthgrant"
	"github.com/cloudreve/Cloudreve/v4/inventory/types"
)

// OAuthClientCreate is the builder for creating a OAuthClient entity.
type OAuthClientCreate struct {
	config
	mutation *OAuthClientMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

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

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

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

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

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

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

// SetGUID sets the "guid" field.
func (occ *OAuthClientCreate) SetGUID(s string) *OAuthClientCreate {
	occ.mutation.SetGUID(s)
	return occ
}

// SetSecret sets the "secret" field.
func (occ *OAuthClientCreate) SetSecret(s string) *OAuthClientCreate {
	occ.mutation.SetSecret(s)
	return occ
}

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

// SetHomepageURL sets the "homepage_url" field.
func (occ *OAuthClientCreate) SetHomepageURL(s string) *OAuthClientCreate {
	occ.mutation.SetHomepageURL(s)
	return occ
}

// SetNillableHomepageURL sets the "homepage_url" field if the given value is not nil.
func (occ *OAuthClientCreate) SetNillableHomepageURL(s *string) *OAuthClientCreate {
	if s != nil {
		occ.SetHomepageURL(*s)
	}
	return occ
}

// SetRedirectUris sets the "redirect_uris" field.
func (occ *OAuthClientCreate) SetRedirectUris(s []string) *OAuthClientCreate {
	occ.mutation.SetRedirectUris(s)
	return occ
}

// SetScopes sets the "scopes" field.
func (occ *OAuthClientCreate) SetScopes(s []string) *OAuthClientCreate {
	occ.mutation.SetScopes(s)
	return occ
}

// SetProps sets the "props" field.
func (occ *OAuthClientCreate) SetProps(tacp *types.OAuthClientProps) *OAuthClientCreate {
	occ.mutation.SetProps(tacp)
	return occ
}

// SetIsEnabled sets the "is_enabled" field.
func (occ *OAuthClientCreate) SetIsEnabled(b bool) *OAuthClientCreate {
	occ.mutation.SetIsEnabled(b)
	return occ
}

// SetNillableIsEnabled sets the "is_enabled" field if the given value is not nil.
func (occ *OAuthClientCreate) SetNillableIsEnabled(b *bool) *OAuthClientCreate {
	if b != nil {
		occ.SetIsEnabled(*b)
	}
	return occ
}

// AddGrantIDs adds the "grants" edge to the OAuthGrant entity by IDs.
func (occ *OAuthClientCreate) AddGrantIDs(ids ...int) *OAuthClientCreate {
	occ.mutation.AddGrantIDs(ids...)
	return occ
}

// AddGrants adds the "grants" edges to the OAuthGrant entity.
func (occ *OAuthClientCreate) AddGrants(o ...*OAuthGrant) *OAuthClientCreate {
	ids := make([]int, len(o))
	for i := range o {
		ids[i] = o[i].ID
	}
	return occ.AddGrantIDs(ids...)
}

// Mutation returns the OAuthClientMutation object of the builder.
func (occ *OAuthClientCreate) Mutation() *OAuthClientMutation {
	return occ.mutation
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (occ *OAuthClientCreate) defaults() error {
	if _, ok := occ.mutation.CreatedAt(); !ok {
		if oauthclient.DefaultCreatedAt == nil {
			return fmt.Errorf("ent: uninitialized oauthclient.DefaultCreatedAt (forgotten import ent/runtime?)")
		}
		v := oauthclient.DefaultCreatedAt()
		occ.mutation.SetCreatedAt(v)
	}
	if _, ok := occ.mutation.UpdatedAt(); !ok {
		if oauthclient.DefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized oauthclient.DefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := oauthclient.DefaultUpdatedAt()
		occ.mutation.SetUpdatedAt(v)
	}
	if _, ok := occ.mutation.RedirectUris(); !ok {
		v := oauthclient.DefaultRedirectUris
		occ.mutation.SetRedirectUris(v)
	}
	if _, ok := occ.mutation.Scopes(); !ok {
		v := oauthclient.DefaultScopes
		occ.mutation.SetScopes(v)
	}
	if _, ok := occ.mutation.Props(); !ok {
		v := oauthclient.DefaultProps
		occ.mutation.SetProps(v)
	}
	if _, ok := occ.mutation.IsEnabled(); !ok {
		v := oauthclient.DefaultIsEnabled
		occ.mutation.SetIsEnabled(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (occ *OAuthClientCreate) check() error {
	if _, ok := occ.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "OAuthClient.created_at"`)}
	}
	if _, ok := occ.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "OAuthClient.updated_at"`)}
	}
	if _, ok := occ.mutation.GUID(); !ok {
		return &ValidationError{Name: "guid", err: errors.New(`ent: missing required field "OAuthClient.guid"`)}
	}
	if v, ok := occ.mutation.GUID(); ok {
		if err := oauthclient.GUIDValidator(v); err != nil {
			return &ValidationError{Name: "guid", err: fmt.Errorf(`ent: validator failed for field "OAuthClient.guid": %w`, err)}
		}
	}
	if _, ok := occ.mutation.Secret(); !ok {
		return &ValidationError{Name: "secret", err: errors.New(`ent: missing required field "OAuthClient.secret"`)}
	}
	if v, ok := occ.mutation.Secret(); ok {
		if err := oauthclient.SecretValidator(v); err != nil {
			return &ValidationError{Name: "secret", err: fmt.Errorf(`ent: validator failed for field "OAuthClient.secret": %w`, err)}
		}
	}
	if _, ok := occ.mutation.Name(); !ok {
		return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "OAuthClient.name"`)}
	}
	if v, ok := occ.mutation.Name(); ok {
		if err := oauthclient.NameValidator(v); err != nil {
			return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "OAuthClient.name": %w`, err)}
		}
	}
	if v, ok := occ.mutation.HomepageURL(); ok {
		if err := oauthclient.HomepageURLValidator(v); err != nil {
			return &ValidationError{Name: "homepage_url", err: fmt.Errorf(`ent: validator failed for field "OAuthClient.homepage_url": %w`, err)}
		}
	}
	if _, ok := occ.mutation.RedirectUris(); !ok {
		return &ValidationError{Name: "redirect_uris", err: errors.New(`ent: missing required field "OAuthClient.redirect_uris"`)}
	}
	if _, ok := occ.mutation.Scopes(); !ok {
		return &ValidationError{Name: "scopes", err: errors.New(`ent: missing required field "OAuthClient.scopes"`)}
	}
	if _, ok := occ.mutation.Props(); !ok {
		return &ValidationError{Name: "props", err: errors.New(`ent: missing required field "OAuthClient.props"`)}
	}
	if _, ok := occ.mutation.IsEnabled(); !ok {
		return &ValidationError{Name: "is_enabled", err: errors.New(`ent: missing required field "OAuthClient.is_enabled"`)}
	}
	return nil
}

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

func (occ *OAuthClientCreate) createSpec() (*OAuthClient, *sqlgraph.CreateSpec) {
	var (
		_node = &OAuthClient{config: occ.config}
		_spec = sqlgraph.NewCreateSpec(oauthclient.Table, sqlgraph.NewFieldSpec(oauthclient.FieldID, field.TypeInt))
	)

	if id, ok := occ.mutation.ID(); ok {
		_node.ID = id
		id64 := int64(id)
		_spec.ID.Value = id64
	}

	_spec.OnConflict = occ.conflict
	if value, ok := occ.mutation.CreatedAt(); ok {
		_spec.SetField(oauthclient.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := occ.mutation.UpdatedAt(); ok {
		_spec.SetField(oauthclient.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	if value, ok := occ.mutation.DeletedAt(); ok {
		_spec.SetField(oauthclient.FieldDeletedAt, field.TypeTime, value)
		_node.DeletedAt = &value
	}
	if value, ok := occ.mutation.GUID(); ok {
		_spec.SetField(oauthclient.FieldGUID, field.TypeString, value)
		_node.GUID = value
	}
	if value, ok := occ.mutation.Secret(); ok {
		_spec.SetField(oauthclient.FieldSecret, field.TypeString, value)
		_node.Secret = value
	}
	if value, ok := occ.mutation.Name(); ok {
		_spec.SetField(oauthclient.FieldName, field.TypeString, value)
		_node.Name = value
	}
	if value, ok := occ.mutation.HomepageURL(); ok {
		_spec.SetField(oauthclient.FieldHomepageURL, field.TypeString, value)
		_node.HomepageURL = value
	}
	if value, ok := occ.mutation.RedirectUris(); ok {
		_spec.SetField(oauthclient.FieldRedirectUris, field.TypeJSON, value)
		_node.RedirectUris = value
	}
	if value, ok := occ.mutation.Scopes(); ok {
		_spec.SetField(oauthclient.FieldScopes, field.TypeJSON, value)
		_node.Scopes = value
	}
	if value, ok := occ.mutation.Props(); ok {
		_spec.SetField(oauthclient.FieldProps, field.TypeJSON, value)
		_node.Props = value
	}
	if value, ok := occ.mutation.IsEnabled(); ok {
		_spec.SetField(oauthclient.FieldIsEnabled, field.TypeBool, value)
		_node.IsEnabled = value
	}
	if nodes := occ.mutation.GrantsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   oauthclient.GrantsTable,
			Columns: []string{oauthclient.GrantsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(oauthgrant.FieldID, field.TypeInt),
			},
		}
		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.OAuthClient.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.OAuthClientUpsert) {
//			SetCreatedAt(v+v).
//		}).
//		Exec(ctx)
func (occ *OAuthClientCreate) OnConflict(opts ...sql.ConflictOption) *OAuthClientUpsertOne {
	occ.conflict = opts
	return &OAuthClientUpsertOne{
		create: occ,
	}
}

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

type (
	// OAuthClientUpsertOne is the builder for "upsert"-ing
	//  one OAuthClient node.
	OAuthClientUpsertOne struct {
		create *OAuthClientCreate
	}

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

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

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

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

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

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

// SetGUID sets the "guid" field.
func (u *OAuthClientUpsert) SetGUID(v string) *OAuthClientUpsert {
	u.Set(oauthclient.FieldGUID, v)
	return u
}

// UpdateGUID sets the "guid" field to the value that was provided on create.
func (u *OAuthClientUpsert) UpdateGUID() *OAuthClientUpsert {
	u.SetExcluded(oauthclient.FieldGUID)
	return u
}

// SetSecret sets the "secret" field.
func (u *OAuthClientUpsert) SetSecret(v string) *OAuthClientUpsert {
	u.Set(oauthclient.FieldSecret, v)
	return u
}

// UpdateSecret sets the "secret" field to the value that was provided on create.
func (u *OAuthClientUpsert) UpdateSecret() *OAuthClientUpsert {
	u.SetExcluded(oauthclient.FieldSecret)
	return u
}

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

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

// SetHomepageURL sets the "homepage_url" field.
func (u *OAuthClientUpsert) SetHomepageURL(v string) *OAuthClientUpsert {
	u.Set(oauthclient.FieldHomepageURL, v)
	return u
}

// UpdateHomepageURL sets the "homepage_url" field to the value that was provided on create.
func (u *OAuthClientUpsert) UpdateHomepageURL() *OAuthClientUpsert {
	u.SetExcluded(oauthclient.FieldHomepageURL)
	return u
}

// ClearHomepageURL clears the value of the "homepage_url" field.
func (u *OAuthClientUpsert) ClearHomepageURL() *OAuthClientUpsert {
	u.SetNull(oauthclient.FieldHomepageURL)
	return u
}

// SetRedirectUris sets the "redirect_uris" field.
func (u *OAuthClientUpsert) SetRedirectUris(v []string) *OAuthClientUpsert {
	u.Set(oauthclient.FieldRedirectUris, v)
	return u
}

// UpdateRedirectUris sets the "redirect_uris" field to the value that was provided on create.
func (u *OAuthClientUpsert) UpdateRedirectUris() *OAuthClientUpsert {
	u.SetExcluded(oauthclient.FieldRedirectUris)
	return u
}

// SetScopes sets the "scopes" field.
func (u *OAuthClientUpsert) SetScopes(v []string) *OAuthClientUpsert {
	u.Set(oauthclient.FieldScopes, v)
	return u
}

// UpdateScopes sets the "scopes" field to the value that was provided on create.
func (u *OAuthClientUpsert) UpdateScopes() *OAuthClientUpsert {
	u.SetExcluded(oauthclient.FieldScopes)
	return u
}

// SetProps sets the "props" field.
func (u *OAuthClientUpsert) SetProps(v *types.OAuthClientProps) *OAuthClientUpsert {
	u.Set(oauthclient.FieldProps, v)
	return u
}

// UpdateProps sets the "props" field to the value that was provided on create.
func (u *OAuthClientUpsert) UpdateProps() *OAuthClientUpsert {
	u.SetExcluded(oauthclient.FieldProps)
	return u
}

// SetIsEnabled sets the "is_enabled" field.
func (u *OAuthClientUpsert) SetIsEnabled(v bool) *OAuthClientUpsert {
	u.Set(oauthclient.FieldIsEnabled, v)
	return u
}

// UpdateIsEnabled sets the "is_enabled" field to the value that was provided on create.
func (u *OAuthClientUpsert) UpdateIsEnabled() *OAuthClientUpsert {
	u.SetExcluded(oauthclient.FieldIsEnabled)
	return u
}

// UpdateNewValues updates the mutable fields using the new values that were set on create.
// Using this option is equivalent to using:
//
//	client.OAuthClient.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//		).
//		Exec(ctx)
func (u *OAuthClientUpsertOne) UpdateNewValues() *OAuthClientUpsertOne {
	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.CreatedAt(); exists {
			s.SetIgnore(oauthclient.FieldCreatedAt)
		}
	}))
	return u
}

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

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

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

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

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

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

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

// SetGUID sets the "guid" field.
func (u *OAuthClientUpsertOne) SetGUID(v string) *OAuthClientUpsertOne {
	return u.Update(func(s *OAuthClientUpsert) {
		s.SetGUID(v)
	})
}

// UpdateGUID sets the "guid" field to the value that was provided on create.
func (u *OAuthClientUpsertOne) UpdateGUID() *OAuthClientUpsertOne {
	return u.Update(func(s *OAuthClientUpsert) {
		s.UpdateGUID()
	})
}

// SetSecret sets the "secret" field.
func (u *OAuthClientUpsertOne) SetSecret(v string) *OAuthClientUpsertOne {
	return u.Update(func(s *OAuthClientUpsert) {
		s.SetSecret(v)
	})
}

// UpdateSecret sets the "secret" field to the value that was provided on create.
func (u *OAuthClientUpsertOne) UpdateSecret() *OAuthClientUpsertOne {
	return u.Update(func(s *OAuthClientUpsert) {
		s.UpdateSecret()
	})
}

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

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

// SetHomepageURL sets the "homepage_url" field.
func (u *OAuthClientUpsertOne) SetHomepageURL(v string) *OAuthClientUpsertOne {
	return u.Update(func(s *OAuthClientUpsert) {
		s.SetHomepageURL(v)
	})
}

// UpdateHomepageURL sets the "homepage_url" field to the value that was provided on create.
func (u *OAuthClientUpsertOne) UpdateHomepageURL() *OAuthClientUpsertOne {
	return u.Update(func(s *OAuthClientUpsert) {
		s.UpdateHomepageURL()
	})
}

// ClearHomepageURL clears the value of the "homepage_url" field.
func (u *OAuthClientUpsertOne) ClearHomepageURL() *OAuthClientUpsertOne {
	return u.Update(func(s *OAuthClientUpsert) {
		s.ClearHomepageURL()
	})
}

// SetRedirectUris sets the "redirect_uris" field.
func (u *OAuthClientUpsertOne) SetRedirectUris(v []string) *OAuthClientUpsertOne {
	return u.Update(func(s *OAuthClientUpsert) {
		s.SetRedirectUris(v)
	})
}

// UpdateRedirectUris sets the "redirect_uris" field to the value that was provided on create.
func (u *OAuthClientUpsertOne) UpdateRedirectUris() *OAuthClientUpsertOne {
	return u.Update(func(s *OAuthClientUpsert) {
		s.UpdateRedirectUris()
	})
}

// SetScopes sets the "scopes" field.
func (u *OAuthClientUpsertOne) SetScopes(v []string) *OAuthClientUpsertOne {
	return u.Update(func(s *OAuthClientUpsert) {
		s.SetScopes(v)
	})
}

// UpdateScopes sets the "scopes" field to the value that was provided on create.
func (u *OAuthClientUpsertOne) UpdateScopes() *OAuthClientUpsertOne {
	return u.Update(func(s *OAuthClientUpsert) {
		s.UpdateScopes()
	})
}

// SetProps sets the "props" field.
func (u *OAuthClientUpsertOne) SetProps(v *types.OAuthClientProps) *OAuthClientUpsertOne {
	return u.Update(func(s *OAuthClientUpsert) {
		s.SetProps(v)
	})
}

// UpdateProps sets the "props" field to the value that was provided on create.
func (u *OAuthClientUpsertOne) UpdateProps() *OAuthClientUpsertOne {
	return u.Update(func(s *OAuthClientUpsert) {
		s.UpdateProps()
	})
}

// SetIsEnabled sets the "is_enabled" field.
func (u *OAuthClientUpsertOne) SetIsEnabled(v bool) *OAuthClientUpsertOne {
	return u.Update(func(s *OAuthClientUpsert) {
		s.SetIsEnabled(v)
	})
}

// UpdateIsEnabled sets the "is_enabled" field to the value that was provided on create.
func (u *OAuthClientUpsertOne) UpdateIsEnabled() *OAuthClientUpsertOne {
	return u.Update(func(s *OAuthClientUpsert) {
		s.UpdateIsEnabled()
	})
}

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

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

func (m *OAuthClientCreate) SetRawID(t int) *OAuthClientCreate {
	m.mutation.SetRawID(t)
	return m
}

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

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

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

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

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

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

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

// OAuthClientUpsertBulk is the builder for "upsert"-ing
// a bulk of OAuthClient nodes.
type OAuthClientUpsertBulk struct {
	create *OAuthClientCreateBulk
}

// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
//	client.OAuthClient.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//		).
//		Exec(ctx)
func (u *OAuthClientUpsertBulk) UpdateNewValues() *OAuthClientUpsertBulk {
	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.CreatedAt(); exists {
				s.SetIgnore(oauthclient.FieldCreatedAt)
			}
		}
	}))
	return u
}

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

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

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

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

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

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

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

// SetGUID sets the "guid" field.
func (u *OAuthClientUpsertBulk) SetGUID(v string) *OAuthClientUpsertBulk {
	return u.Update(func(s *OAuthClientUpsert) {
		s.SetGUID(v)
	})
}

// UpdateGUID sets the "guid" field to the value that was provided on create.
func (u *OAuthClientUpsertBulk) UpdateGUID() *OAuthClientUpsertBulk {
	return u.Update(func(s *OAuthClientUpsert) {
		s.UpdateGUID()
	})
}

// SetSecret sets the "secret" field.
func (u *OAuthClientUpsertBulk) SetSecret(v string) *OAuthClientUpsertBulk {
	return u.Update(func(s *OAuthClientUpsert) {
		s.SetSecret(v)
	})
}

// UpdateSecret sets the "secret" field to the value that was provided on create.
func (u *OAuthClientUpsertBulk) UpdateSecret() *OAuthClientUpsertBulk {
	return u.Update(func(s *OAuthClientUpsert) {
		s.UpdateSecret()
	})
}

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

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

// SetHomepageURL sets the "homepage_url" field.
func (u *OAuthClientUpsertBulk) SetHomepageURL(v string) *OAuthClientUpsertBulk {
	return u.Update(func(s *OAuthClientUpsert) {
		s.SetHomepageURL(v)
	})
}

// UpdateHomepageURL sets the "homepage_url" field to the value that was provided on create.
func (u *OAuthClientUpsertBulk) UpdateHomepageURL() *OAuthClientUpsertBulk {
	return u.Update(func(s *OAuthClientUpsert) {
		s.UpdateHomepageURL()
	})
}

// ClearHomepageURL clears the value of the "homepage_url" field.
func (u *OAuthClientUpsertBulk) ClearHomepageURL() *OAuthClientUpsertBulk {
	return u.Update(func(s *OAuthClientUpsert) {
		s.ClearHomepageURL()
	})
}

// SetRedirectUris sets the "redirect_uris" field.
func (u *OAuthClientUpsertBulk) SetRedirectUris(v []string) *OAuthClientUpsertBulk {
	return u.Update(func(s *OAuthClientUpsert) {
		s.SetRedirectUris(v)
	})
}

// UpdateRedirectUris sets the "redirect_uris" field to the value that was provided on create.
func (u *OAuthClientUpsertBulk) UpdateRedirectUris() *OAuthClientUpsertBulk {
	return u.Update(func(s *OAuthClientUpsert) {
		s.UpdateRedirectUris()
	})
}

// SetScopes sets the "scopes" field.
func (u *OAuthClientUpsertBulk) SetScopes(v []string) *OAuthClientUpsertBulk {
	return u.Update(func(s *OAuthClientUpsert) {
		s.SetScopes(v)
	})
}

// UpdateScopes sets the "scopes" field to the value that was provided on create.
func (u *OAuthClientUpsertBulk) UpdateScopes() *OAuthClientUpsertBulk {
	return u.Update(func(s *OAuthClientUpsert) {
		s.UpdateScopes()
	})
}

// SetProps sets the "props" field.
func (u *OAuthClientUpsertBulk) SetProps(v *types.OAuthClientProps) *OAuthClientUpsertBulk {
	return u.Update(func(s *OAuthClientUpsert) {
		s.SetProps(v)
	})
}

// UpdateProps sets the "props" field to the value that was provided on create.
func (u *OAuthClientUpsertBulk) UpdateProps() *OAuthClientUpsertBulk {
	return u.Update(func(s *OAuthClientUpsert) {
		s.UpdateProps()
	})
}

// SetIsEnabled sets the "is_enabled" field.
func (u *OAuthClientUpsertBulk) SetIsEnabled(v bool) *OAuthClientUpsertBulk {
	return u.Update(func(s *OAuthClientUpsert) {
		s.SetIsEnabled(v)
	})
}

// UpdateIsEnabled sets the "is_enabled" field to the value that was provided on create.
func (u *OAuthClientUpsertBulk) UpdateIsEnabled() *OAuthClientUpsertBulk {
	return u.Update(func(s *OAuthClientUpsert) {
		s.UpdateIsEnabled()
	})
}

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

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