// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"

	"ariga.io/ogent/internal/integration/ogent/ent/hat"
	"ariga.io/ogent/internal/integration/ogent/ent/predicate"
	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
)

// HatUpdate is the builder for updating Hat entities.
type HatUpdate struct {
	config
	hooks    []Hook
	mutation *HatMutation
}

// Where appends a list predicates to the HatUpdate builder.
func (hu *HatUpdate) Where(ps ...predicate.Hat) *HatUpdate {
	hu.mutation.Where(ps...)
	return hu
}

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

// Mutation returns the HatMutation object of the builder.
func (hu *HatUpdate) Mutation() *HatMutation {
	return hu.mutation
}

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

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

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

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

func (hu *HatUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := sqlgraph.NewUpdateSpec(hat.Table, hat.Columns, sqlgraph.NewFieldSpec(hat.FieldID, field.TypeInt))
	if ps := hu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := hu.mutation.Name(); ok {
		_spec.SetField(hat.FieldName, field.TypeString, value)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, hu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{hat.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	hu.mutation.done = true
	return n, nil
}

// HatUpdateOne is the builder for updating a single Hat entity.
type HatUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *HatMutation
}

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

// Mutation returns the HatMutation object of the builder.
func (huo *HatUpdateOne) Mutation() *HatMutation {
	return huo.mutation
}

// Where appends a list predicates to the HatUpdate builder.
func (huo *HatUpdateOne) Where(ps ...predicate.Hat) *HatUpdateOne {
	huo.mutation.Where(ps...)
	return huo
}

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

// Save executes the query and returns the updated Hat entity.
func (huo *HatUpdateOne) Save(ctx context.Context) (*Hat, error) {
	return withHooks[*Hat, HatMutation](ctx, huo.sqlSave, huo.mutation, huo.hooks)
}

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

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

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

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