// 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/Wei-Shaw/sub2api/ent/group"
	"github.com/Wei-Shaw/sub2api/ent/predicate"
	"github.com/Wei-Shaw/sub2api/ent/user"
	"github.com/Wei-Shaw/sub2api/ent/usersubscription"
)

// UserSubscriptionUpdate is the builder for updating UserSubscription entities.
type UserSubscriptionUpdate struct {
	config
	hooks    []Hook
	mutation *UserSubscriptionMutation
}

// Where appends a list predicates to the UserSubscriptionUpdate builder.
func (_u *UserSubscriptionUpdate) Where(ps ...predicate.UserSubscription) *UserSubscriptionUpdate {
	_u.mutation.Where(ps...)
	return _u
}

// SetUpdatedAt sets the "updated_at" field.
func (_u *UserSubscriptionUpdate) SetUpdatedAt(v time.Time) *UserSubscriptionUpdate {
	_u.mutation.SetUpdatedAt(v)
	return _u
}

// SetUserID sets the "user_id" field.
func (_u *UserSubscriptionUpdate) SetUserID(v int64) *UserSubscriptionUpdate {
	_u.mutation.SetUserID(v)
	return _u
}

// SetNillableUserID sets the "user_id" field if the given value is not nil.
func (_u *UserSubscriptionUpdate) SetNillableUserID(v *int64) *UserSubscriptionUpdate {
	if v != nil {
		_u.SetUserID(*v)
	}
	return _u
}

// SetGroupID sets the "group_id" field.
func (_u *UserSubscriptionUpdate) SetGroupID(v int64) *UserSubscriptionUpdate {
	_u.mutation.SetGroupID(v)
	return _u
}

// SetNillableGroupID sets the "group_id" field if the given value is not nil.
func (_u *UserSubscriptionUpdate) SetNillableGroupID(v *int64) *UserSubscriptionUpdate {
	if v != nil {
		_u.SetGroupID(*v)
	}
	return _u
}

// SetStartsAt sets the "starts_at" field.
func (_u *UserSubscriptionUpdate) SetStartsAt(v time.Time) *UserSubscriptionUpdate {
	_u.mutation.SetStartsAt(v)
	return _u
}

// SetNillableStartsAt sets the "starts_at" field if the given value is not nil.
func (_u *UserSubscriptionUpdate) SetNillableStartsAt(v *time.Time) *UserSubscriptionUpdate {
	if v != nil {
		_u.SetStartsAt(*v)
	}
	return _u
}

// SetExpiresAt sets the "expires_at" field.
func (_u *UserSubscriptionUpdate) SetExpiresAt(v time.Time) *UserSubscriptionUpdate {
	_u.mutation.SetExpiresAt(v)
	return _u
}

// SetNillableExpiresAt sets the "expires_at" field if the given value is not nil.
func (_u *UserSubscriptionUpdate) SetNillableExpiresAt(v *time.Time) *UserSubscriptionUpdate {
	if v != nil {
		_u.SetExpiresAt(*v)
	}
	return _u
}

// SetStatus sets the "status" field.
func (_u *UserSubscriptionUpdate) SetStatus(v string) *UserSubscriptionUpdate {
	_u.mutation.SetStatus(v)
	return _u
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (_u *UserSubscriptionUpdate) SetNillableStatus(v *string) *UserSubscriptionUpdate {
	if v != nil {
		_u.SetStatus(*v)
	}
	return _u
}

// SetDailyWindowStart sets the "daily_window_start" field.
func (_u *UserSubscriptionUpdate) SetDailyWindowStart(v time.Time) *UserSubscriptionUpdate {
	_u.mutation.SetDailyWindowStart(v)
	return _u
}

// SetNillableDailyWindowStart sets the "daily_window_start" field if the given value is not nil.
func (_u *UserSubscriptionUpdate) SetNillableDailyWindowStart(v *time.Time) *UserSubscriptionUpdate {
	if v != nil {
		_u.SetDailyWindowStart(*v)
	}
	return _u
}

// ClearDailyWindowStart clears the value of the "daily_window_start" field.
func (_u *UserSubscriptionUpdate) ClearDailyWindowStart() *UserSubscriptionUpdate {
	_u.mutation.ClearDailyWindowStart()
	return _u
}

// SetWeeklyWindowStart sets the "weekly_window_start" field.
func (_u *UserSubscriptionUpdate) SetWeeklyWindowStart(v time.Time) *UserSubscriptionUpdate {
	_u.mutation.SetWeeklyWindowStart(v)
	return _u
}

// SetNillableWeeklyWindowStart sets the "weekly_window_start" field if the given value is not nil.
func (_u *UserSubscriptionUpdate) SetNillableWeeklyWindowStart(v *time.Time) *UserSubscriptionUpdate {
	if v != nil {
		_u.SetWeeklyWindowStart(*v)
	}
	return _u
}

// ClearWeeklyWindowStart clears the value of the "weekly_window_start" field.
func (_u *UserSubscriptionUpdate) ClearWeeklyWindowStart() *UserSubscriptionUpdate {
	_u.mutation.ClearWeeklyWindowStart()
	return _u
}

// SetMonthlyWindowStart sets the "monthly_window_start" field.
func (_u *UserSubscriptionUpdate) SetMonthlyWindowStart(v time.Time) *UserSubscriptionUpdate {
	_u.mutation.SetMonthlyWindowStart(v)
	return _u
}

// SetNillableMonthlyWindowStart sets the "monthly_window_start" field if the given value is not nil.
func (_u *UserSubscriptionUpdate) SetNillableMonthlyWindowStart(v *time.Time) *UserSubscriptionUpdate {
	if v != nil {
		_u.SetMonthlyWindowStart(*v)
	}
	return _u
}

// ClearMonthlyWindowStart clears the value of the "monthly_window_start" field.
func (_u *UserSubscriptionUpdate) ClearMonthlyWindowStart() *UserSubscriptionUpdate {
	_u.mutation.ClearMonthlyWindowStart()
	return _u
}

// SetDailyUsageUsd sets the "daily_usage_usd" field.
func (_u *UserSubscriptionUpdate) SetDailyUsageUsd(v float64) *UserSubscriptionUpdate {
	_u.mutation.ResetDailyUsageUsd()
	_u.mutation.SetDailyUsageUsd(v)
	return _u
}

// SetNillableDailyUsageUsd sets the "daily_usage_usd" field if the given value is not nil.
func (_u *UserSubscriptionUpdate) SetNillableDailyUsageUsd(v *float64) *UserSubscriptionUpdate {
	if v != nil {
		_u.SetDailyUsageUsd(*v)
	}
	return _u
}

// AddDailyUsageUsd adds value to the "daily_usage_usd" field.
func (_u *UserSubscriptionUpdate) AddDailyUsageUsd(v float64) *UserSubscriptionUpdate {
	_u.mutation.AddDailyUsageUsd(v)
	return _u
}

// SetWeeklyUsageUsd sets the "weekly_usage_usd" field.
func (_u *UserSubscriptionUpdate) SetWeeklyUsageUsd(v float64) *UserSubscriptionUpdate {
	_u.mutation.ResetWeeklyUsageUsd()
	_u.mutation.SetWeeklyUsageUsd(v)
	return _u
}

// SetNillableWeeklyUsageUsd sets the "weekly_usage_usd" field if the given value is not nil.
func (_u *UserSubscriptionUpdate) SetNillableWeeklyUsageUsd(v *float64) *UserSubscriptionUpdate {
	if v != nil {
		_u.SetWeeklyUsageUsd(*v)
	}
	return _u
}

// AddWeeklyUsageUsd adds value to the "weekly_usage_usd" field.
func (_u *UserSubscriptionUpdate) AddWeeklyUsageUsd(v float64) *UserSubscriptionUpdate {
	_u.mutation.AddWeeklyUsageUsd(v)
	return _u
}

// SetMonthlyUsageUsd sets the "monthly_usage_usd" field.
func (_u *UserSubscriptionUpdate) SetMonthlyUsageUsd(v float64) *UserSubscriptionUpdate {
	_u.mutation.ResetMonthlyUsageUsd()
	_u.mutation.SetMonthlyUsageUsd(v)
	return _u
}

// SetNillableMonthlyUsageUsd sets the "monthly_usage_usd" field if the given value is not nil.
func (_u *UserSubscriptionUpdate) SetNillableMonthlyUsageUsd(v *float64) *UserSubscriptionUpdate {
	if v != nil {
		_u.SetMonthlyUsageUsd(*v)
	}
	return _u
}

// AddMonthlyUsageUsd adds value to the "monthly_usage_usd" field.
func (_u *UserSubscriptionUpdate) AddMonthlyUsageUsd(v float64) *UserSubscriptionUpdate {
	_u.mutation.AddMonthlyUsageUsd(v)
	return _u
}

// SetAssignedBy sets the "assigned_by" field.
func (_u *UserSubscriptionUpdate) SetAssignedBy(v int64) *UserSubscriptionUpdate {
	_u.mutation.SetAssignedBy(v)
	return _u
}

// SetNillableAssignedBy sets the "assigned_by" field if the given value is not nil.
func (_u *UserSubscriptionUpdate) SetNillableAssignedBy(v *int64) *UserSubscriptionUpdate {
	if v != nil {
		_u.SetAssignedBy(*v)
	}
	return _u
}

// ClearAssignedBy clears the value of the "assigned_by" field.
func (_u *UserSubscriptionUpdate) ClearAssignedBy() *UserSubscriptionUpdate {
	_u.mutation.ClearAssignedBy()
	return _u
}

// SetAssignedAt sets the "assigned_at" field.
func (_u *UserSubscriptionUpdate) SetAssignedAt(v time.Time) *UserSubscriptionUpdate {
	_u.mutation.SetAssignedAt(v)
	return _u
}

// SetNillableAssignedAt sets the "assigned_at" field if the given value is not nil.
func (_u *UserSubscriptionUpdate) SetNillableAssignedAt(v *time.Time) *UserSubscriptionUpdate {
	if v != nil {
		_u.SetAssignedAt(*v)
	}
	return _u
}

// SetNotes sets the "notes" field.
func (_u *UserSubscriptionUpdate) SetNotes(v string) *UserSubscriptionUpdate {
	_u.mutation.SetNotes(v)
	return _u
}

// SetNillableNotes sets the "notes" field if the given value is not nil.
func (_u *UserSubscriptionUpdate) SetNillableNotes(v *string) *UserSubscriptionUpdate {
	if v != nil {
		_u.SetNotes(*v)
	}
	return _u
}

// ClearNotes clears the value of the "notes" field.
func (_u *UserSubscriptionUpdate) ClearNotes() *UserSubscriptionUpdate {
	_u.mutation.ClearNotes()
	return _u
}

// SetUser sets the "user" edge to the User entity.
func (_u *UserSubscriptionUpdate) SetUser(v *User) *UserSubscriptionUpdate {
	return _u.SetUserID(v.ID)
}

// SetGroup sets the "group" edge to the Group entity.
func (_u *UserSubscriptionUpdate) SetGroup(v *Group) *UserSubscriptionUpdate {
	return _u.SetGroupID(v.ID)
}

// SetAssignedByUserID sets the "assigned_by_user" edge to the User entity by ID.
func (_u *UserSubscriptionUpdate) SetAssignedByUserID(id int64) *UserSubscriptionUpdate {
	_u.mutation.SetAssignedByUserID(id)
	return _u
}

// SetNillableAssignedByUserID sets the "assigned_by_user" edge to the User entity by ID if the given value is not nil.
func (_u *UserSubscriptionUpdate) SetNillableAssignedByUserID(id *int64) *UserSubscriptionUpdate {
	if id != nil {
		_u = _u.SetAssignedByUserID(*id)
	}
	return _u
}

// SetAssignedByUser sets the "assigned_by_user" edge to the User entity.
func (_u *UserSubscriptionUpdate) SetAssignedByUser(v *User) *UserSubscriptionUpdate {
	return _u.SetAssignedByUserID(v.ID)
}

// Mutation returns the UserSubscriptionMutation object of the builder.
func (_u *UserSubscriptionUpdate) Mutation() *UserSubscriptionMutation {
	return _u.mutation
}

// ClearUser clears the "user" edge to the User entity.
func (_u *UserSubscriptionUpdate) ClearUser() *UserSubscriptionUpdate {
	_u.mutation.ClearUser()
	return _u
}

// ClearGroup clears the "group" edge to the Group entity.
func (_u *UserSubscriptionUpdate) ClearGroup() *UserSubscriptionUpdate {
	_u.mutation.ClearGroup()
	return _u
}

// ClearAssignedByUser clears the "assigned_by_user" edge to the User entity.
func (_u *UserSubscriptionUpdate) ClearAssignedByUser() *UserSubscriptionUpdate {
	_u.mutation.ClearAssignedByUser()
	return _u
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (_u *UserSubscriptionUpdate) defaults() {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		v := usersubscription.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_u *UserSubscriptionUpdate) check() error {
	if v, ok := _u.mutation.Status(); ok {
		if err := usersubscription.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "UserSubscription.status": %w`, err)}
		}
	}
	if _u.mutation.UserCleared() && len(_u.mutation.UserIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "UserSubscription.user"`)
	}
	if _u.mutation.GroupCleared() && len(_u.mutation.GroupIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "UserSubscription.group"`)
	}
	return nil
}

func (_u *UserSubscriptionUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(usersubscription.Table, usersubscription.Columns, sqlgraph.NewFieldSpec(usersubscription.FieldID, field.TypeInt64))
	if ps := _u.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(usersubscription.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.StartsAt(); ok {
		_spec.SetField(usersubscription.FieldStartsAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.ExpiresAt(); ok {
		_spec.SetField(usersubscription.FieldExpiresAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.Status(); ok {
		_spec.SetField(usersubscription.FieldStatus, field.TypeString, value)
	}
	if value, ok := _u.mutation.DailyWindowStart(); ok {
		_spec.SetField(usersubscription.FieldDailyWindowStart, field.TypeTime, value)
	}
	if _u.mutation.DailyWindowStartCleared() {
		_spec.ClearField(usersubscription.FieldDailyWindowStart, field.TypeTime)
	}
	if value, ok := _u.mutation.WeeklyWindowStart(); ok {
		_spec.SetField(usersubscription.FieldWeeklyWindowStart, field.TypeTime, value)
	}
	if _u.mutation.WeeklyWindowStartCleared() {
		_spec.ClearField(usersubscription.FieldWeeklyWindowStart, field.TypeTime)
	}
	if value, ok := _u.mutation.MonthlyWindowStart(); ok {
		_spec.SetField(usersubscription.FieldMonthlyWindowStart, field.TypeTime, value)
	}
	if _u.mutation.MonthlyWindowStartCleared() {
		_spec.ClearField(usersubscription.FieldMonthlyWindowStart, field.TypeTime)
	}
	if value, ok := _u.mutation.DailyUsageUsd(); ok {
		_spec.SetField(usersubscription.FieldDailyUsageUsd, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.AddedDailyUsageUsd(); ok {
		_spec.AddField(usersubscription.FieldDailyUsageUsd, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.WeeklyUsageUsd(); ok {
		_spec.SetField(usersubscription.FieldWeeklyUsageUsd, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.AddedWeeklyUsageUsd(); ok {
		_spec.AddField(usersubscription.FieldWeeklyUsageUsd, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.MonthlyUsageUsd(); ok {
		_spec.SetField(usersubscription.FieldMonthlyUsageUsd, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.AddedMonthlyUsageUsd(); ok {
		_spec.AddField(usersubscription.FieldMonthlyUsageUsd, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.AssignedAt(); ok {
		_spec.SetField(usersubscription.FieldAssignedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.Notes(); ok {
		_spec.SetField(usersubscription.FieldNotes, field.TypeString, value)
	}
	if _u.mutation.NotesCleared() {
		_spec.ClearField(usersubscription.FieldNotes, field.TypeString)
	}
	if _u.mutation.UserCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   usersubscription.UserTable,
			Columns: []string{usersubscription.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.UserIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   usersubscription.UserTable,
			Columns: []string{usersubscription.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _u.mutation.GroupCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   usersubscription.GroupTable,
			Columns: []string{usersubscription.GroupColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(group.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.GroupIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   usersubscription.GroupTable,
			Columns: []string{usersubscription.GroupColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(group.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _u.mutation.AssignedByUserCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   usersubscription.AssignedByUserTable,
			Columns: []string{usersubscription.AssignedByUserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.AssignedByUserIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   usersubscription.AssignedByUserTable,
			Columns: []string{usersubscription.AssignedByUserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{usersubscription.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// UserSubscriptionUpdateOne is the builder for updating a single UserSubscription entity.
type UserSubscriptionUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *UserSubscriptionMutation
}

// SetUpdatedAt sets the "updated_at" field.
func (_u *UserSubscriptionUpdateOne) SetUpdatedAt(v time.Time) *UserSubscriptionUpdateOne {
	_u.mutation.SetUpdatedAt(v)
	return _u
}

// SetUserID sets the "user_id" field.
func (_u *UserSubscriptionUpdateOne) SetUserID(v int64) *UserSubscriptionUpdateOne {
	_u.mutation.SetUserID(v)
	return _u
}

// SetNillableUserID sets the "user_id" field if the given value is not nil.
func (_u *UserSubscriptionUpdateOne) SetNillableUserID(v *int64) *UserSubscriptionUpdateOne {
	if v != nil {
		_u.SetUserID(*v)
	}
	return _u
}

// SetGroupID sets the "group_id" field.
func (_u *UserSubscriptionUpdateOne) SetGroupID(v int64) *UserSubscriptionUpdateOne {
	_u.mutation.SetGroupID(v)
	return _u
}

// SetNillableGroupID sets the "group_id" field if the given value is not nil.
func (_u *UserSubscriptionUpdateOne) SetNillableGroupID(v *int64) *UserSubscriptionUpdateOne {
	if v != nil {
		_u.SetGroupID(*v)
	}
	return _u
}

// SetStartsAt sets the "starts_at" field.
func (_u *UserSubscriptionUpdateOne) SetStartsAt(v time.Time) *UserSubscriptionUpdateOne {
	_u.mutation.SetStartsAt(v)
	return _u
}

// SetNillableStartsAt sets the "starts_at" field if the given value is not nil.
func (_u *UserSubscriptionUpdateOne) SetNillableStartsAt(v *time.Time) *UserSubscriptionUpdateOne {
	if v != nil {
		_u.SetStartsAt(*v)
	}
	return _u
}

// SetExpiresAt sets the "expires_at" field.
func (_u *UserSubscriptionUpdateOne) SetExpiresAt(v time.Time) *UserSubscriptionUpdateOne {
	_u.mutation.SetExpiresAt(v)
	return _u
}

// SetNillableExpiresAt sets the "expires_at" field if the given value is not nil.
func (_u *UserSubscriptionUpdateOne) SetNillableExpiresAt(v *time.Time) *UserSubscriptionUpdateOne {
	if v != nil {
		_u.SetExpiresAt(*v)
	}
	return _u
}

// SetStatus sets the "status" field.
func (_u *UserSubscriptionUpdateOne) SetStatus(v string) *UserSubscriptionUpdateOne {
	_u.mutation.SetStatus(v)
	return _u
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (_u *UserSubscriptionUpdateOne) SetNillableStatus(v *string) *UserSubscriptionUpdateOne {
	if v != nil {
		_u.SetStatus(*v)
	}
	return _u
}

// SetDailyWindowStart sets the "daily_window_start" field.
func (_u *UserSubscriptionUpdateOne) SetDailyWindowStart(v time.Time) *UserSubscriptionUpdateOne {
	_u.mutation.SetDailyWindowStart(v)
	return _u
}

// SetNillableDailyWindowStart sets the "daily_window_start" field if the given value is not nil.
func (_u *UserSubscriptionUpdateOne) SetNillableDailyWindowStart(v *time.Time) *UserSubscriptionUpdateOne {
	if v != nil {
		_u.SetDailyWindowStart(*v)
	}
	return _u
}

// ClearDailyWindowStart clears the value of the "daily_window_start" field.
func (_u *UserSubscriptionUpdateOne) ClearDailyWindowStart() *UserSubscriptionUpdateOne {
	_u.mutation.ClearDailyWindowStart()
	return _u
}

// SetWeeklyWindowStart sets the "weekly_window_start" field.
func (_u *UserSubscriptionUpdateOne) SetWeeklyWindowStart(v time.Time) *UserSubscriptionUpdateOne {
	_u.mutation.SetWeeklyWindowStart(v)
	return _u
}

// SetNillableWeeklyWindowStart sets the "weekly_window_start" field if the given value is not nil.
func (_u *UserSubscriptionUpdateOne) SetNillableWeeklyWindowStart(v *time.Time) *UserSubscriptionUpdateOne {
	if v != nil {
		_u.SetWeeklyWindowStart(*v)
	}
	return _u
}

// ClearWeeklyWindowStart clears the value of the "weekly_window_start" field.
func (_u *UserSubscriptionUpdateOne) ClearWeeklyWindowStart() *UserSubscriptionUpdateOne {
	_u.mutation.ClearWeeklyWindowStart()
	return _u
}

// SetMonthlyWindowStart sets the "monthly_window_start" field.
func (_u *UserSubscriptionUpdateOne) SetMonthlyWindowStart(v time.Time) *UserSubscriptionUpdateOne {
	_u.mutation.SetMonthlyWindowStart(v)
	return _u
}

// SetNillableMonthlyWindowStart sets the "monthly_window_start" field if the given value is not nil.
func (_u *UserSubscriptionUpdateOne) SetNillableMonthlyWindowStart(v *time.Time) *UserSubscriptionUpdateOne {
	if v != nil {
		_u.SetMonthlyWindowStart(*v)
	}
	return _u
}

// ClearMonthlyWindowStart clears the value of the "monthly_window_start" field.
func (_u *UserSubscriptionUpdateOne) ClearMonthlyWindowStart() *UserSubscriptionUpdateOne {
	_u.mutation.ClearMonthlyWindowStart()
	return _u
}

// SetDailyUsageUsd sets the "daily_usage_usd" field.
func (_u *UserSubscriptionUpdateOne) SetDailyUsageUsd(v float64) *UserSubscriptionUpdateOne {
	_u.mutation.ResetDailyUsageUsd()
	_u.mutation.SetDailyUsageUsd(v)
	return _u
}

// SetNillableDailyUsageUsd sets the "daily_usage_usd" field if the given value is not nil.
func (_u *UserSubscriptionUpdateOne) SetNillableDailyUsageUsd(v *float64) *UserSubscriptionUpdateOne {
	if v != nil {
		_u.SetDailyUsageUsd(*v)
	}
	return _u
}

// AddDailyUsageUsd adds value to the "daily_usage_usd" field.
func (_u *UserSubscriptionUpdateOne) AddDailyUsageUsd(v float64) *UserSubscriptionUpdateOne {
	_u.mutation.AddDailyUsageUsd(v)
	return _u
}

// SetWeeklyUsageUsd sets the "weekly_usage_usd" field.
func (_u *UserSubscriptionUpdateOne) SetWeeklyUsageUsd(v float64) *UserSubscriptionUpdateOne {
	_u.mutation.ResetWeeklyUsageUsd()
	_u.mutation.SetWeeklyUsageUsd(v)
	return _u
}

// SetNillableWeeklyUsageUsd sets the "weekly_usage_usd" field if the given value is not nil.
func (_u *UserSubscriptionUpdateOne) SetNillableWeeklyUsageUsd(v *float64) *UserSubscriptionUpdateOne {
	if v != nil {
		_u.SetWeeklyUsageUsd(*v)
	}
	return _u
}

// AddWeeklyUsageUsd adds value to the "weekly_usage_usd" field.
func (_u *UserSubscriptionUpdateOne) AddWeeklyUsageUsd(v float64) *UserSubscriptionUpdateOne {
	_u.mutation.AddWeeklyUsageUsd(v)
	return _u
}

// SetMonthlyUsageUsd sets the "monthly_usage_usd" field.
func (_u *UserSubscriptionUpdateOne) SetMonthlyUsageUsd(v float64) *UserSubscriptionUpdateOne {
	_u.mutation.ResetMonthlyUsageUsd()
	_u.mutation.SetMonthlyUsageUsd(v)
	return _u
}

// SetNillableMonthlyUsageUsd sets the "monthly_usage_usd" field if the given value is not nil.
func (_u *UserSubscriptionUpdateOne) SetNillableMonthlyUsageUsd(v *float64) *UserSubscriptionUpdateOne {
	if v != nil {
		_u.SetMonthlyUsageUsd(*v)
	}
	return _u
}

// AddMonthlyUsageUsd adds value to the "monthly_usage_usd" field.
func (_u *UserSubscriptionUpdateOne) AddMonthlyUsageUsd(v float64) *UserSubscriptionUpdateOne {
	_u.mutation.AddMonthlyUsageUsd(v)
	return _u
}

// SetAssignedBy sets the "assigned_by" field.
func (_u *UserSubscriptionUpdateOne) SetAssignedBy(v int64) *UserSubscriptionUpdateOne {
	_u.mutation.SetAssignedBy(v)
	return _u
}

// SetNillableAssignedBy sets the "assigned_by" field if the given value is not nil.
func (_u *UserSubscriptionUpdateOne) SetNillableAssignedBy(v *int64) *UserSubscriptionUpdateOne {
	if v != nil {
		_u.SetAssignedBy(*v)
	}
	return _u
}

// ClearAssignedBy clears the value of the "assigned_by" field.
func (_u *UserSubscriptionUpdateOne) ClearAssignedBy() *UserSubscriptionUpdateOne {
	_u.mutation.ClearAssignedBy()
	return _u
}

// SetAssignedAt sets the "assigned_at" field.
func (_u *UserSubscriptionUpdateOne) SetAssignedAt(v time.Time) *UserSubscriptionUpdateOne {
	_u.mutation.SetAssignedAt(v)
	return _u
}

// SetNillableAssignedAt sets the "assigned_at" field if the given value is not nil.
func (_u *UserSubscriptionUpdateOne) SetNillableAssignedAt(v *time.Time) *UserSubscriptionUpdateOne {
	if v != nil {
		_u.SetAssignedAt(*v)
	}
	return _u
}

// SetNotes sets the "notes" field.
func (_u *UserSubscriptionUpdateOne) SetNotes(v string) *UserSubscriptionUpdateOne {
	_u.mutation.SetNotes(v)
	return _u
}

// SetNillableNotes sets the "notes" field if the given value is not nil.
func (_u *UserSubscriptionUpdateOne) SetNillableNotes(v *string) *UserSubscriptionUpdateOne {
	if v != nil {
		_u.SetNotes(*v)
	}
	return _u
}

// ClearNotes clears the value of the "notes" field.
func (_u *UserSubscriptionUpdateOne) ClearNotes() *UserSubscriptionUpdateOne {
	_u.mutation.ClearNotes()
	return _u
}

// SetUser sets the "user" edge to the User entity.
func (_u *UserSubscriptionUpdateOne) SetUser(v *User) *UserSubscriptionUpdateOne {
	return _u.SetUserID(v.ID)
}

// SetGroup sets the "group" edge to the Group entity.
func (_u *UserSubscriptionUpdateOne) SetGroup(v *Group) *UserSubscriptionUpdateOne {
	return _u.SetGroupID(v.ID)
}

// SetAssignedByUserID sets the "assigned_by_user" edge to the User entity by ID.
func (_u *UserSubscriptionUpdateOne) SetAssignedByUserID(id int64) *UserSubscriptionUpdateOne {
	_u.mutation.SetAssignedByUserID(id)
	return _u
}

// SetNillableAssignedByUserID sets the "assigned_by_user" edge to the User entity by ID if the given value is not nil.
func (_u *UserSubscriptionUpdateOne) SetNillableAssignedByUserID(id *int64) *UserSubscriptionUpdateOne {
	if id != nil {
		_u = _u.SetAssignedByUserID(*id)
	}
	return _u
}

// SetAssignedByUser sets the "assigned_by_user" edge to the User entity.
func (_u *UserSubscriptionUpdateOne) SetAssignedByUser(v *User) *UserSubscriptionUpdateOne {
	return _u.SetAssignedByUserID(v.ID)
}

// Mutation returns the UserSubscriptionMutation object of the builder.
func (_u *UserSubscriptionUpdateOne) Mutation() *UserSubscriptionMutation {
	return _u.mutation
}

// ClearUser clears the "user" edge to the User entity.
func (_u *UserSubscriptionUpdateOne) ClearUser() *UserSubscriptionUpdateOne {
	_u.mutation.ClearUser()
	return _u
}

// ClearGroup clears the "group" edge to the Group entity.
func (_u *UserSubscriptionUpdateOne) ClearGroup() *UserSubscriptionUpdateOne {
	_u.mutation.ClearGroup()
	return _u
}

// ClearAssignedByUser clears the "assigned_by_user" edge to the User entity.
func (_u *UserSubscriptionUpdateOne) ClearAssignedByUser() *UserSubscriptionUpdateOne {
	_u.mutation.ClearAssignedByUser()
	return _u
}

// Where appends a list predicates to the UserSubscriptionUpdate builder.
func (_u *UserSubscriptionUpdateOne) Where(ps ...predicate.UserSubscription) *UserSubscriptionUpdateOne {
	_u.mutation.Where(ps...)
	return _u
}

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

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

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

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

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

// defaults sets the default values of the builder before save.
func (_u *UserSubscriptionUpdateOne) defaults() {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		v := usersubscription.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_u *UserSubscriptionUpdateOne) check() error {
	if v, ok := _u.mutation.Status(); ok {
		if err := usersubscription.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "UserSubscription.status": %w`, err)}
		}
	}
	if _u.mutation.UserCleared() && len(_u.mutation.UserIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "UserSubscription.user"`)
	}
	if _u.mutation.GroupCleared() && len(_u.mutation.GroupIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "UserSubscription.group"`)
	}
	return nil
}

func (_u *UserSubscriptionUpdateOne) sqlSave(ctx context.Context) (_node *UserSubscription, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(usersubscription.Table, usersubscription.Columns, sqlgraph.NewFieldSpec(usersubscription.FieldID, field.TypeInt64))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "UserSubscription.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := _u.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, usersubscription.FieldID)
		for _, f := range fields {
			if !usersubscription.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != usersubscription.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := _u.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(usersubscription.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.StartsAt(); ok {
		_spec.SetField(usersubscription.FieldStartsAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.ExpiresAt(); ok {
		_spec.SetField(usersubscription.FieldExpiresAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.Status(); ok {
		_spec.SetField(usersubscription.FieldStatus, field.TypeString, value)
	}
	if value, ok := _u.mutation.DailyWindowStart(); ok {
		_spec.SetField(usersubscription.FieldDailyWindowStart, field.TypeTime, value)
	}
	if _u.mutation.DailyWindowStartCleared() {
		_spec.ClearField(usersubscription.FieldDailyWindowStart, field.TypeTime)
	}
	if value, ok := _u.mutation.WeeklyWindowStart(); ok {
		_spec.SetField(usersubscription.FieldWeeklyWindowStart, field.TypeTime, value)
	}
	if _u.mutation.WeeklyWindowStartCleared() {
		_spec.ClearField(usersubscription.FieldWeeklyWindowStart, field.TypeTime)
	}
	if value, ok := _u.mutation.MonthlyWindowStart(); ok {
		_spec.SetField(usersubscription.FieldMonthlyWindowStart, field.TypeTime, value)
	}
	if _u.mutation.MonthlyWindowStartCleared() {
		_spec.ClearField(usersubscription.FieldMonthlyWindowStart, field.TypeTime)
	}
	if value, ok := _u.mutation.DailyUsageUsd(); ok {
		_spec.SetField(usersubscription.FieldDailyUsageUsd, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.AddedDailyUsageUsd(); ok {
		_spec.AddField(usersubscription.FieldDailyUsageUsd, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.WeeklyUsageUsd(); ok {
		_spec.SetField(usersubscription.FieldWeeklyUsageUsd, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.AddedWeeklyUsageUsd(); ok {
		_spec.AddField(usersubscription.FieldWeeklyUsageUsd, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.MonthlyUsageUsd(); ok {
		_spec.SetField(usersubscription.FieldMonthlyUsageUsd, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.AddedMonthlyUsageUsd(); ok {
		_spec.AddField(usersubscription.FieldMonthlyUsageUsd, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.AssignedAt(); ok {
		_spec.SetField(usersubscription.FieldAssignedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.Notes(); ok {
		_spec.SetField(usersubscription.FieldNotes, field.TypeString, value)
	}
	if _u.mutation.NotesCleared() {
		_spec.ClearField(usersubscription.FieldNotes, field.TypeString)
	}
	if _u.mutation.UserCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   usersubscription.UserTable,
			Columns: []string{usersubscription.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.UserIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   usersubscription.UserTable,
			Columns: []string{usersubscription.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _u.mutation.GroupCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   usersubscription.GroupTable,
			Columns: []string{usersubscription.GroupColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(group.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.GroupIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   usersubscription.GroupTable,
			Columns: []string{usersubscription.GroupColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(group.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _u.mutation.AssignedByUserCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   usersubscription.AssignedByUserTable,
			Columns: []string{usersubscription.AssignedByUserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.AssignedByUserIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   usersubscription.AssignedByUserTable,
			Columns: []string{usersubscription.AssignedByUserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &UserSubscription{config: _u.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{usersubscription.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
