// Code generated by ent, DO NOT EDIT.

package ent

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

	"entgo.io/ent/dialect"
	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/google/uuid"
	"github.com/lightsparkdev/spark/so/ent/pendingsendtransfer"
	"github.com/lightsparkdev/spark/so/ent/schema/schematype"
)

// PendingSendTransferCreate is the builder for creating a PendingSendTransfer entity.
type PendingSendTransferCreate struct {
	config
	mutation *PendingSendTransferMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

// SetCreateTime sets the "create_time" field.
func (pstc *PendingSendTransferCreate) SetCreateTime(t time.Time) *PendingSendTransferCreate {
	pstc.mutation.SetCreateTime(t)
	return pstc
}

// SetNillableCreateTime sets the "create_time" field if the given value is not nil.
func (pstc *PendingSendTransferCreate) SetNillableCreateTime(t *time.Time) *PendingSendTransferCreate {
	if t != nil {
		pstc.SetCreateTime(*t)
	}
	return pstc
}

// SetUpdateTime sets the "update_time" field.
func (pstc *PendingSendTransferCreate) SetUpdateTime(t time.Time) *PendingSendTransferCreate {
	pstc.mutation.SetUpdateTime(t)
	return pstc
}

// SetNillableUpdateTime sets the "update_time" field if the given value is not nil.
func (pstc *PendingSendTransferCreate) SetNillableUpdateTime(t *time.Time) *PendingSendTransferCreate {
	if t != nil {
		pstc.SetUpdateTime(*t)
	}
	return pstc
}

// SetTransferID sets the "transfer_id" field.
func (pstc *PendingSendTransferCreate) SetTransferID(u uuid.UUID) *PendingSendTransferCreate {
	pstc.mutation.SetTransferID(u)
	return pstc
}

// SetStatus sets the "status" field.
func (pstc *PendingSendTransferCreate) SetStatus(ssts schematype.PendingSendTransferStatus) *PendingSendTransferCreate {
	pstc.mutation.SetStatus(ssts)
	return pstc
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (pstc *PendingSendTransferCreate) SetNillableStatus(ssts *schematype.PendingSendTransferStatus) *PendingSendTransferCreate {
	if ssts != nil {
		pstc.SetStatus(*ssts)
	}
	return pstc
}

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

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

// Mutation returns the PendingSendTransferMutation object of the builder.
func (pstc *PendingSendTransferCreate) Mutation() *PendingSendTransferMutation {
	return pstc.mutation
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (pstc *PendingSendTransferCreate) defaults() {
	if _, ok := pstc.mutation.CreateTime(); !ok {
		v := pendingsendtransfer.DefaultCreateTime()
		pstc.mutation.SetCreateTime(v)
	}
	if _, ok := pstc.mutation.UpdateTime(); !ok {
		v := pendingsendtransfer.DefaultUpdateTime()
		pstc.mutation.SetUpdateTime(v)
	}
	if _, ok := pstc.mutation.Status(); !ok {
		v := pendingsendtransfer.DefaultStatus
		pstc.mutation.SetStatus(v)
	}
	if _, ok := pstc.mutation.ID(); !ok {
		v := pendingsendtransfer.DefaultID()
		pstc.mutation.SetID(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (pstc *PendingSendTransferCreate) check() error {
	if _, ok := pstc.mutation.CreateTime(); !ok {
		return &ValidationError{Name: "create_time", err: errors.New(`ent: missing required field "PendingSendTransfer.create_time"`)}
	}
	if _, ok := pstc.mutation.UpdateTime(); !ok {
		return &ValidationError{Name: "update_time", err: errors.New(`ent: missing required field "PendingSendTransfer.update_time"`)}
	}
	if _, ok := pstc.mutation.TransferID(); !ok {
		return &ValidationError{Name: "transfer_id", err: errors.New(`ent: missing required field "PendingSendTransfer.transfer_id"`)}
	}
	if _, ok := pstc.mutation.Status(); !ok {
		return &ValidationError{Name: "status", err: errors.New(`ent: missing required field "PendingSendTransfer.status"`)}
	}
	if v, ok := pstc.mutation.Status(); ok {
		if err := pendingsendtransfer.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "PendingSendTransfer.status": %w`, err)}
		}
	}
	return nil
}

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

func (pstc *PendingSendTransferCreate) createSpec() (*PendingSendTransfer, *sqlgraph.CreateSpec) {
	var (
		_node = &PendingSendTransfer{config: pstc.config}
		_spec = sqlgraph.NewCreateSpec(pendingsendtransfer.Table, sqlgraph.NewFieldSpec(pendingsendtransfer.FieldID, field.TypeUUID))
	)
	_spec.OnConflict = pstc.conflict
	if id, ok := pstc.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = &id
	}
	if value, ok := pstc.mutation.CreateTime(); ok {
		_spec.SetField(pendingsendtransfer.FieldCreateTime, field.TypeTime, value)
		_node.CreateTime = value
	}
	if value, ok := pstc.mutation.UpdateTime(); ok {
		_spec.SetField(pendingsendtransfer.FieldUpdateTime, field.TypeTime, value)
		_node.UpdateTime = value
	}
	if value, ok := pstc.mutation.TransferID(); ok {
		_spec.SetField(pendingsendtransfer.FieldTransferID, field.TypeUUID, value)
		_node.TransferID = value
	}
	if value, ok := pstc.mutation.Status(); ok {
		_spec.SetField(pendingsendtransfer.FieldStatus, field.TypeEnum, value)
		_node.Status = value
	}
	return _node, _spec
}

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.PendingSendTransfer.Create().
//		SetCreateTime(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.PendingSendTransferUpsert) {
//			SetCreateTime(v+v).
//		}).
//		Exec(ctx)
func (pstc *PendingSendTransferCreate) OnConflict(opts ...sql.ConflictOption) *PendingSendTransferUpsertOne {
	pstc.conflict = opts
	return &PendingSendTransferUpsertOne{
		create: pstc,
	}
}

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

type (
	// PendingSendTransferUpsertOne is the builder for "upsert"-ing
	//  one PendingSendTransfer node.
	PendingSendTransferUpsertOne struct {
		create *PendingSendTransferCreate
	}

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

// SetUpdateTime sets the "update_time" field.
func (u *PendingSendTransferUpsert) SetUpdateTime(v time.Time) *PendingSendTransferUpsert {
	u.Set(pendingsendtransfer.FieldUpdateTime, v)
	return u
}

// UpdateUpdateTime sets the "update_time" field to the value that was provided on create.
func (u *PendingSendTransferUpsert) UpdateUpdateTime() *PendingSendTransferUpsert {
	u.SetExcluded(pendingsendtransfer.FieldUpdateTime)
	return u
}

// SetStatus sets the "status" field.
func (u *PendingSendTransferUpsert) SetStatus(v schematype.PendingSendTransferStatus) *PendingSendTransferUpsert {
	u.Set(pendingsendtransfer.FieldStatus, v)
	return u
}

// UpdateStatus sets the "status" field to the value that was provided on create.
func (u *PendingSendTransferUpsert) UpdateStatus() *PendingSendTransferUpsert {
	u.SetExcluded(pendingsendtransfer.FieldStatus)
	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.PendingSendTransfer.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(pendingsendtransfer.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *PendingSendTransferUpsertOne) UpdateNewValues() *PendingSendTransferUpsertOne {
	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(pendingsendtransfer.FieldID)
		}
		if _, exists := u.create.mutation.CreateTime(); exists {
			s.SetIgnore(pendingsendtransfer.FieldCreateTime)
		}
		if _, exists := u.create.mutation.TransferID(); exists {
			s.SetIgnore(pendingsendtransfer.FieldTransferID)
		}
	}))
	return u
}

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

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

// SetUpdateTime sets the "update_time" field.
func (u *PendingSendTransferUpsertOne) SetUpdateTime(v time.Time) *PendingSendTransferUpsertOne {
	return u.Update(func(s *PendingSendTransferUpsert) {
		s.SetUpdateTime(v)
	})
}

// UpdateUpdateTime sets the "update_time" field to the value that was provided on create.
func (u *PendingSendTransferUpsertOne) UpdateUpdateTime() *PendingSendTransferUpsertOne {
	return u.Update(func(s *PendingSendTransferUpsert) {
		s.UpdateUpdateTime()
	})
}

// SetStatus sets the "status" field.
func (u *PendingSendTransferUpsertOne) SetStatus(v schematype.PendingSendTransferStatus) *PendingSendTransferUpsertOne {
	return u.Update(func(s *PendingSendTransferUpsert) {
		s.SetStatus(v)
	})
}

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

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

// ExecX is like Exec, but panics if an error occurs.
func (u *PendingSendTransferUpsertOne) 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 *PendingSendTransferUpsertOne) ID(ctx context.Context) (id uuid.UUID, err error) {
	if u.create.driver.Dialect() == dialect.MySQL {
		// In case of "ON CONFLICT", there is no way to get back non-numeric ID
		// fields from the database since MySQL does not support the RETURNING clause.
		return id, errors.New("ent: PendingSendTransferUpsertOne.ID is not supported by MySQL driver. Use PendingSendTransferUpsertOne.Exec instead")
	}
	node, err := u.create.Save(ctx)
	if err != nil {
		return id, err
	}
	return node.ID, nil
}

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

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

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

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

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

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

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.PendingSendTransfer.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.PendingSendTransferUpsert) {
//			SetCreateTime(v+v).
//		}).
//		Exec(ctx)
func (pstcb *PendingSendTransferCreateBulk) OnConflict(opts ...sql.ConflictOption) *PendingSendTransferUpsertBulk {
	pstcb.conflict = opts
	return &PendingSendTransferUpsertBulk{
		create: pstcb,
	}
}

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

// PendingSendTransferUpsertBulk is the builder for "upsert"-ing
// a bulk of PendingSendTransfer nodes.
type PendingSendTransferUpsertBulk struct {
	create *PendingSendTransferCreateBulk
}

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

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

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

// SetUpdateTime sets the "update_time" field.
func (u *PendingSendTransferUpsertBulk) SetUpdateTime(v time.Time) *PendingSendTransferUpsertBulk {
	return u.Update(func(s *PendingSendTransferUpsert) {
		s.SetUpdateTime(v)
	})
}

// UpdateUpdateTime sets the "update_time" field to the value that was provided on create.
func (u *PendingSendTransferUpsertBulk) UpdateUpdateTime() *PendingSendTransferUpsertBulk {
	return u.Update(func(s *PendingSendTransferUpsert) {
		s.UpdateUpdateTime()
	})
}

// SetStatus sets the "status" field.
func (u *PendingSendTransferUpsertBulk) SetStatus(v schematype.PendingSendTransferStatus) *PendingSendTransferUpsertBulk {
	return u.Update(func(s *PendingSendTransferUpsert) {
		s.SetStatus(v)
	})
}

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

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

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