// Code generated by ent, DO NOT EDIT.

package models

import (
	"context"
	"errors"
	"fmt"
	"juwan-backend/app/search/rpc/internal/models/favorites"
	"juwan-backend/app/search/rpc/internal/models/predicate"
	"sync"
	"time"

	"entgo.io/ent"
	"entgo.io/ent/dialect/sql"
)

const (
	// Operation types.
	OpCreate    = ent.OpCreate
	OpDelete    = ent.OpDelete
	OpDeleteOne = ent.OpDeleteOne
	OpUpdate    = ent.OpUpdate
	OpUpdateOne = ent.OpUpdateOne

	// Node types.
	TypeFavorites = "Favorites"
)

// FavoritesMutation represents an operation that mutates the Favorites nodes in the graph.
type FavoritesMutation struct {
	config
	op            Op
	typ           string
	id            *int64
	user_id       *int64
	adduser_id    *int64
	target_type   *string
	target_id     *int64
	addtarget_id  *int64
	created_at    *time.Time
	clearedFields map[string]struct{}
	done          bool
	oldValue      func(context.Context) (*Favorites, error)
	predicates    []predicate.Favorites
}

var _ ent.Mutation = (*FavoritesMutation)(nil)

// favoritesOption allows management of the mutation configuration using functional options.
type favoritesOption func(*FavoritesMutation)

// newFavoritesMutation creates new mutation for the Favorites entity.
func newFavoritesMutation(c config, op Op, opts ...favoritesOption) *FavoritesMutation {
	m := &FavoritesMutation{
		config:        c,
		op:            op,
		typ:           TypeFavorites,
		clearedFields: make(map[string]struct{}),
	}
	for _, opt := range opts {
		opt(m)
	}
	return m
}

// withFavoritesID sets the ID field of the mutation.
func withFavoritesID(id int64) favoritesOption {
	return func(m *FavoritesMutation) {
		var (
			err   error
			once  sync.Once
			value *Favorites
		)
		m.oldValue = func(ctx context.Context) (*Favorites, error) {
			once.Do(func() {
				if m.done {
					err = errors.New("querying old values post mutation is not allowed")
				} else {
					value, err = m.Client().Favorites.Get(ctx, id)
				}
			})
			return value, err
		}
		m.id = &id
	}
}

// withFavorites sets the old Favorites of the mutation.
func withFavorites(node *Favorites) favoritesOption {
	return func(m *FavoritesMutation) {
		m.oldValue = func(context.Context) (*Favorites, error) {
			return node, nil
		}
		m.id = &node.ID
	}
}

// Client returns a new `ent.Client` from the mutation. If the mutation was
// executed in a transaction (ent.Tx), a transactional client is returned.
func (m FavoritesMutation) Client() *Client {
	client := &Client{config: m.config}
	client.init()
	return client
}

// Tx returns an `ent.Tx` for mutations that were executed in transactions;
// it returns an error otherwise.
func (m FavoritesMutation) Tx() (*Tx, error) {
	if _, ok := m.driver.(*txDriver); !ok {
		return nil, errors.New("models: mutation is not running in a transaction")
	}
	tx := &Tx{config: m.config}
	tx.init()
	return tx, nil
}

// SetID sets the value of the id field. Note that this
// operation is only accepted on creation of Favorites entities.
func (m *FavoritesMutation) SetID(id int64) {
	m.id = &id
}

// ID returns the ID value in the mutation. Note that the ID is only available
// if it was provided to the builder or after it was returned from the database.
func (m *FavoritesMutation) ID() (id int64, exists bool) {
	if m.id == nil {
		return
	}
	return *m.id, true
}

// IDs queries the database and returns the entity ids that match the mutation's predicate.
// That means, if the mutation is applied within a transaction with an isolation level such
// as sql.LevelSerializable, the returned ids match the ids of the rows that will be updated
// or updated by the mutation.
func (m *FavoritesMutation) IDs(ctx context.Context) ([]int64, error) {
	switch {
	case m.op.Is(OpUpdateOne | OpDeleteOne):
		id, exists := m.ID()
		if exists {
			return []int64{id}, nil
		}
		fallthrough
	case m.op.Is(OpUpdate | OpDelete):
		return m.Client().Favorites.Query().Where(m.predicates...).IDs(ctx)
	default:
		return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op)
	}
}

// SetUserID sets the "user_id" field.
func (m *FavoritesMutation) SetUserID(i int64) {
	m.user_id = &i
	m.adduser_id = nil
}

// UserID returns the value of the "user_id" field in the mutation.
func (m *FavoritesMutation) UserID() (r int64, exists bool) {
	v := m.user_id
	if v == nil {
		return
	}
	return *v, true
}

// OldUserID returns the old "user_id" field's value of the Favorites entity.
// If the Favorites object wasn't provided to the builder, the object is fetched from the database.
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
func (m *FavoritesMutation) OldUserID(ctx context.Context) (v int64, err error) {
	if !m.op.Is(OpUpdateOne) {
		return v, errors.New("OldUserID is only allowed on UpdateOne operations")
	}
	if m.id == nil || m.oldValue == nil {
		return v, errors.New("OldUserID requires an ID field in the mutation")
	}
	oldValue, err := m.oldValue(ctx)
	if err != nil {
		return v, fmt.Errorf("querying old value for OldUserID: %w", err)
	}
	return oldValue.UserID, nil
}

// AddUserID adds i to the "user_id" field.
func (m *FavoritesMutation) AddUserID(i int64) {
	if m.adduser_id != nil {
		*m.adduser_id += i
	} else {
		m.adduser_id = &i
	}
}

// AddedUserID returns the value that was added to the "user_id" field in this mutation.
func (m *FavoritesMutation) AddedUserID() (r int64, exists bool) {
	v := m.adduser_id
	if v == nil {
		return
	}
	return *v, true
}

// ResetUserID resets all changes to the "user_id" field.
func (m *FavoritesMutation) ResetUserID() {
	m.user_id = nil
	m.adduser_id = nil
}

// SetTargetType sets the "target_type" field.
func (m *FavoritesMutation) SetTargetType(s string) {
	m.target_type = &s
}

// TargetType returns the value of the "target_type" field in the mutation.
func (m *FavoritesMutation) TargetType() (r string, exists bool) {
	v := m.target_type
	if v == nil {
		return
	}
	return *v, true
}

// OldTargetType returns the old "target_type" field's value of the Favorites entity.
// If the Favorites object wasn't provided to the builder, the object is fetched from the database.
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
func (m *FavoritesMutation) OldTargetType(ctx context.Context) (v string, err error) {
	if !m.op.Is(OpUpdateOne) {
		return v, errors.New("OldTargetType is only allowed on UpdateOne operations")
	}
	if m.id == nil || m.oldValue == nil {
		return v, errors.New("OldTargetType requires an ID field in the mutation")
	}
	oldValue, err := m.oldValue(ctx)
	if err != nil {
		return v, fmt.Errorf("querying old value for OldTargetType: %w", err)
	}
	return oldValue.TargetType, nil
}

// ResetTargetType resets all changes to the "target_type" field.
func (m *FavoritesMutation) ResetTargetType() {
	m.target_type = nil
}

// SetTargetID sets the "target_id" field.
func (m *FavoritesMutation) SetTargetID(i int64) {
	m.target_id = &i
	m.addtarget_id = nil
}

// TargetID returns the value of the "target_id" field in the mutation.
func (m *FavoritesMutation) TargetID() (r int64, exists bool) {
	v := m.target_id
	if v == nil {
		return
	}
	return *v, true
}

// OldTargetID returns the old "target_id" field's value of the Favorites entity.
// If the Favorites object wasn't provided to the builder, the object is fetched from the database.
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
func (m *FavoritesMutation) OldTargetID(ctx context.Context) (v int64, err error) {
	if !m.op.Is(OpUpdateOne) {
		return v, errors.New("OldTargetID is only allowed on UpdateOne operations")
	}
	if m.id == nil || m.oldValue == nil {
		return v, errors.New("OldTargetID requires an ID field in the mutation")
	}
	oldValue, err := m.oldValue(ctx)
	if err != nil {
		return v, fmt.Errorf("querying old value for OldTargetID: %w", err)
	}
	return oldValue.TargetID, nil
}

// AddTargetID adds i to the "target_id" field.
func (m *FavoritesMutation) AddTargetID(i int64) {
	if m.addtarget_id != nil {
		*m.addtarget_id += i
	} else {
		m.addtarget_id = &i
	}
}

// AddedTargetID returns the value that was added to the "target_id" field in this mutation.
func (m *FavoritesMutation) AddedTargetID() (r int64, exists bool) {
	v := m.addtarget_id
	if v == nil {
		return
	}
	return *v, true
}

// ResetTargetID resets all changes to the "target_id" field.
func (m *FavoritesMutation) ResetTargetID() {
	m.target_id = nil
	m.addtarget_id = nil
}

// SetCreatedAt sets the "created_at" field.
func (m *FavoritesMutation) SetCreatedAt(t time.Time) {
	m.created_at = &t
}

// CreatedAt returns the value of the "created_at" field in the mutation.
func (m *FavoritesMutation) CreatedAt() (r time.Time, exists bool) {
	v := m.created_at
	if v == nil {
		return
	}
	return *v, true
}

// OldCreatedAt returns the old "created_at" field's value of the Favorites entity.
// If the Favorites object wasn't provided to the builder, the object is fetched from the database.
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
func (m *FavoritesMutation) OldCreatedAt(ctx context.Context) (v time.Time, err error) {
	if !m.op.Is(OpUpdateOne) {
		return v, errors.New("OldCreatedAt is only allowed on UpdateOne operations")
	}
	if m.id == nil || m.oldValue == nil {
		return v, errors.New("OldCreatedAt requires an ID field in the mutation")
	}
	oldValue, err := m.oldValue(ctx)
	if err != nil {
		return v, fmt.Errorf("querying old value for OldCreatedAt: %w", err)
	}
	return oldValue.CreatedAt, nil
}

// ResetCreatedAt resets all changes to the "created_at" field.
func (m *FavoritesMutation) ResetCreatedAt() {
	m.created_at = nil
}

// Where appends a list predicates to the FavoritesMutation builder.
func (m *FavoritesMutation) Where(ps ...predicate.Favorites) {
	m.predicates = append(m.predicates, ps...)
}

// WhereP appends storage-level predicates to the FavoritesMutation builder. Using this method,
// users can use type-assertion to append predicates that do not depend on any generated package.
func (m *FavoritesMutation) WhereP(ps ...func(*sql.Selector)) {
	p := make([]predicate.Favorites, len(ps))
	for i := range ps {
		p[i] = ps[i]
	}
	m.Where(p...)
}

// Op returns the operation name.
func (m *FavoritesMutation) Op() Op {
	return m.op
}

// SetOp allows setting the mutation operation.
func (m *FavoritesMutation) SetOp(op Op) {
	m.op = op
}

// Type returns the node type of this mutation (Favorites).
func (m *FavoritesMutation) Type() string {
	return m.typ
}

// Fields returns all fields that were changed during this mutation. Note that in
// order to get all numeric fields that were incremented/decremented, call
// AddedFields().
func (m *FavoritesMutation) Fields() []string {
	fields := make([]string, 0, 4)
	if m.user_id != nil {
		fields = append(fields, favorites.FieldUserID)
	}
	if m.target_type != nil {
		fields = append(fields, favorites.FieldTargetType)
	}
	if m.target_id != nil {
		fields = append(fields, favorites.FieldTargetID)
	}
	if m.created_at != nil {
		fields = append(fields, favorites.FieldCreatedAt)
	}
	return fields
}

// Field returns the value of a field with the given name. The second boolean
// return value indicates that this field was not set, or was not defined in the
// schema.
func (m *FavoritesMutation) Field(name string) (ent.Value, bool) {
	switch name {
	case favorites.FieldUserID:
		return m.UserID()
	case favorites.FieldTargetType:
		return m.TargetType()
	case favorites.FieldTargetID:
		return m.TargetID()
	case favorites.FieldCreatedAt:
		return m.CreatedAt()
	}
	return nil, false
}

// OldField returns the old value of the field from the database. An error is
// returned if the mutation operation is not UpdateOne, or the query to the
// database failed.
func (m *FavoritesMutation) OldField(ctx context.Context, name string) (ent.Value, error) {
	switch name {
	case favorites.FieldUserID:
		return m.OldUserID(ctx)
	case favorites.FieldTargetType:
		return m.OldTargetType(ctx)
	case favorites.FieldTargetID:
		return m.OldTargetID(ctx)
	case favorites.FieldCreatedAt:
		return m.OldCreatedAt(ctx)
	}
	return nil, fmt.Errorf("unknown Favorites field %s", name)
}

// SetField sets the value of a field with the given name. It returns an error if
// the field is not defined in the schema, or if the type mismatched the field
// type.
func (m *FavoritesMutation) SetField(name string, value ent.Value) error {
	switch name {
	case favorites.FieldUserID:
		v, ok := value.(int64)
		if !ok {
			return fmt.Errorf("unexpected type %T for field %s", value, name)
		}
		m.SetUserID(v)
		return nil
	case favorites.FieldTargetType:
		v, ok := value.(string)
		if !ok {
			return fmt.Errorf("unexpected type %T for field %s", value, name)
		}
		m.SetTargetType(v)
		return nil
	case favorites.FieldTargetID:
		v, ok := value.(int64)
		if !ok {
			return fmt.Errorf("unexpected type %T for field %s", value, name)
		}
		m.SetTargetID(v)
		return nil
	case favorites.FieldCreatedAt:
		v, ok := value.(time.Time)
		if !ok {
			return fmt.Errorf("unexpected type %T for field %s", value, name)
		}
		m.SetCreatedAt(v)
		return nil
	}
	return fmt.Errorf("unknown Favorites field %s", name)
}

// AddedFields returns all numeric fields that were incremented/decremented during
// this mutation.
func (m *FavoritesMutation) AddedFields() []string {
	var fields []string
	if m.adduser_id != nil {
		fields = append(fields, favorites.FieldUserID)
	}
	if m.addtarget_id != nil {
		fields = append(fields, favorites.FieldTargetID)
	}
	return fields
}

// AddedField returns the numeric value that was incremented/decremented on a field
// with the given name. The second boolean return value indicates that this field
// was not set, or was not defined in the schema.
func (m *FavoritesMutation) AddedField(name string) (ent.Value, bool) {
	switch name {
	case favorites.FieldUserID:
		return m.AddedUserID()
	case favorites.FieldTargetID:
		return m.AddedTargetID()
	}
	return nil, false
}

// AddField adds the value to the field with the given name. It returns an error if
// the field is not defined in the schema, or if the type mismatched the field
// type.
func (m *FavoritesMutation) AddField(name string, value ent.Value) error {
	switch name {
	case favorites.FieldUserID:
		v, ok := value.(int64)
		if !ok {
			return fmt.Errorf("unexpected type %T for field %s", value, name)
		}
		m.AddUserID(v)
		return nil
	case favorites.FieldTargetID:
		v, ok := value.(int64)
		if !ok {
			return fmt.Errorf("unexpected type %T for field %s", value, name)
		}
		m.AddTargetID(v)
		return nil
	}
	return fmt.Errorf("unknown Favorites numeric field %s", name)
}

// ClearedFields returns all nullable fields that were cleared during this
// mutation.
func (m *FavoritesMutation) ClearedFields() []string {
	return nil
}

// FieldCleared returns a boolean indicating if a field with the given name was
// cleared in this mutation.
func (m *FavoritesMutation) FieldCleared(name string) bool {
	_, ok := m.clearedFields[name]
	return ok
}

// ClearField clears the value of the field with the given name. It returns an
// error if the field is not defined in the schema.
func (m *FavoritesMutation) ClearField(name string) error {
	return fmt.Errorf("unknown Favorites nullable field %s", name)
}

// ResetField resets all changes in the mutation for the field with the given name.
// It returns an error if the field is not defined in the schema.
func (m *FavoritesMutation) ResetField(name string) error {
	switch name {
	case favorites.FieldUserID:
		m.ResetUserID()
		return nil
	case favorites.FieldTargetType:
		m.ResetTargetType()
		return nil
	case favorites.FieldTargetID:
		m.ResetTargetID()
		return nil
	case favorites.FieldCreatedAt:
		m.ResetCreatedAt()
		return nil
	}
	return fmt.Errorf("unknown Favorites field %s", name)
}

// AddedEdges returns all edge names that were set/added in this mutation.
func (m *FavoritesMutation) AddedEdges() []string {
	edges := make([]string, 0, 0)
	return edges
}

// AddedIDs returns all IDs (to other nodes) that were added for the given edge
// name in this mutation.
func (m *FavoritesMutation) AddedIDs(name string) []ent.Value {
	return nil
}

// RemovedEdges returns all edge names that were removed in this mutation.
func (m *FavoritesMutation) RemovedEdges() []string {
	edges := make([]string, 0, 0)
	return edges
}

// RemovedIDs returns all IDs (to other nodes) that were removed for the edge with
// the given name in this mutation.
func (m *FavoritesMutation) RemovedIDs(name string) []ent.Value {
	return nil
}

// ClearedEdges returns all edge names that were cleared in this mutation.
func (m *FavoritesMutation) ClearedEdges() []string {
	edges := make([]string, 0, 0)
	return edges
}

// EdgeCleared returns a boolean which indicates if the edge with the given name
// was cleared in this mutation.
func (m *FavoritesMutation) EdgeCleared(name string) bool {
	return false
}

// ClearEdge clears the value of the edge with the given name. It returns an error
// if that edge is not defined in the schema.
func (m *FavoritesMutation) ClearEdge(name string) error {
	return fmt.Errorf("unknown Favorites unique edge %s", name)
}

// ResetEdge resets all changes to the edge with the given name in this mutation.
// It returns an error if the edge is not defined in the schema.
func (m *FavoritesMutation) ResetEdge(name string) error {
	return fmt.Errorf("unknown Favorites edge %s", name)
}
