// 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/lightsparkdev/spark/so/ent/gossip"
	"github.com/lightsparkdev/spark/so/ent/predicate"
	"github.com/lightsparkdev/spark/so/ent/schema/schematype"
)

// GossipUpdate is the builder for updating Gossip entities.
type GossipUpdate struct {
	config
	hooks    []Hook
	mutation *GossipMutation
}

// Where appends a list predicates to the GossipUpdate builder.
func (gu *GossipUpdate) Where(ps ...predicate.Gossip) *GossipUpdate {
	gu.mutation.Where(ps...)
	return gu
}

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

// SetReceipts sets the "receipts" field.
func (gu *GossipUpdate) SetReceipts(b []byte) *GossipUpdate {
	gu.mutation.SetReceipts(b)
	return gu
}

// SetStatus sets the "status" field.
func (gu *GossipUpdate) SetStatus(ss schematype.GossipStatus) *GossipUpdate {
	gu.mutation.SetStatus(ss)
	return gu
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (gu *GossipUpdate) SetNillableStatus(ss *schematype.GossipStatus) *GossipUpdate {
	if ss != nil {
		gu.SetStatus(*ss)
	}
	return gu
}

// Mutation returns the GossipMutation object of the builder.
func (gu *GossipUpdate) Mutation() *GossipMutation {
	return gu.mutation
}

// Save executes the query and returns the number of nodes affected by the update operation.
func (gu *GossipUpdate) Save(ctx context.Context) (int, error) {
	gu.defaults()
	return withHooks(ctx, gu.sqlSave, gu.mutation, gu.hooks)
}

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

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

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

// defaults sets the default values of the builder before save.
func (gu *GossipUpdate) defaults() {
	if _, ok := gu.mutation.UpdateTime(); !ok {
		v := gossip.UpdateDefaultUpdateTime()
		gu.mutation.SetUpdateTime(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (gu *GossipUpdate) check() error {
	if v, ok := gu.mutation.Status(); ok {
		if err := gossip.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "Gossip.status": %w`, err)}
		}
	}
	return nil
}

func (gu *GossipUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := gu.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(gossip.Table, gossip.Columns, sqlgraph.NewFieldSpec(gossip.FieldID, field.TypeUUID))
	if ps := gu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := gu.mutation.UpdateTime(); ok {
		_spec.SetField(gossip.FieldUpdateTime, field.TypeTime, value)
	}
	if value, ok := gu.mutation.Receipts(); ok {
		_spec.SetField(gossip.FieldReceipts, field.TypeBytes, value)
	}
	if value, ok := gu.mutation.Status(); ok {
		_spec.SetField(gossip.FieldStatus, field.TypeEnum, value)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, gu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{gossip.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	gu.mutation.done = true
	return n, nil
}

// GossipUpdateOne is the builder for updating a single Gossip entity.
type GossipUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *GossipMutation
}

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

// SetReceipts sets the "receipts" field.
func (guo *GossipUpdateOne) SetReceipts(b []byte) *GossipUpdateOne {
	guo.mutation.SetReceipts(b)
	return guo
}

// SetStatus sets the "status" field.
func (guo *GossipUpdateOne) SetStatus(ss schematype.GossipStatus) *GossipUpdateOne {
	guo.mutation.SetStatus(ss)
	return guo
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (guo *GossipUpdateOne) SetNillableStatus(ss *schematype.GossipStatus) *GossipUpdateOne {
	if ss != nil {
		guo.SetStatus(*ss)
	}
	return guo
}

// Mutation returns the GossipMutation object of the builder.
func (guo *GossipUpdateOne) Mutation() *GossipMutation {
	return guo.mutation
}

// Where appends a list predicates to the GossipUpdate builder.
func (guo *GossipUpdateOne) Where(ps ...predicate.Gossip) *GossipUpdateOne {
	guo.mutation.Where(ps...)
	return guo
}

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

// Save executes the query and returns the updated Gossip entity.
func (guo *GossipUpdateOne) Save(ctx context.Context) (*Gossip, error) {
	guo.defaults()
	return withHooks(ctx, guo.sqlSave, guo.mutation, guo.hooks)
}

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

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

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

// defaults sets the default values of the builder before save.
func (guo *GossipUpdateOne) defaults() {
	if _, ok := guo.mutation.UpdateTime(); !ok {
		v := gossip.UpdateDefaultUpdateTime()
		guo.mutation.SetUpdateTime(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (guo *GossipUpdateOne) check() error {
	if v, ok := guo.mutation.Status(); ok {
		if err := gossip.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "Gossip.status": %w`, err)}
		}
	}
	return nil
}

func (guo *GossipUpdateOne) sqlSave(ctx context.Context) (_node *Gossip, err error) {
	if err := guo.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(gossip.Table, gossip.Columns, sqlgraph.NewFieldSpec(gossip.FieldID, field.TypeUUID))
	id, ok := guo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Gossip.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := guo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, gossip.FieldID)
		for _, f := range fields {
			if !gossip.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != gossip.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := guo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := guo.mutation.UpdateTime(); ok {
		_spec.SetField(gossip.FieldUpdateTime, field.TypeTime, value)
	}
	if value, ok := guo.mutation.Receipts(); ok {
		_spec.SetField(gossip.FieldReceipts, field.TypeBytes, value)
	}
	if value, ok := guo.mutation.Status(); ok {
		_spec.SetField(gossip.FieldStatus, field.TypeEnum, value)
	}
	_node = &Gossip{config: guo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, guo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{gossip.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	guo.mutation.done = true
	return _node, nil
}
