// 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/anzhiyu-c/anheyu-app/ent/urlstat"
)

// URLStatCreate is the builder for creating a URLStat entity.
type URLStatCreate struct {
	config
	mutation *URLStatMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

// SetCreatedAt sets the "created_at" field.
func (_c *URLStatCreate) SetCreatedAt(v time.Time) *URLStatCreate {
	_c.mutation.SetCreatedAt(v)
	return _c
}

// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (_c *URLStatCreate) SetNillableCreatedAt(v *time.Time) *URLStatCreate {
	if v != nil {
		_c.SetCreatedAt(*v)
	}
	return _c
}

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

// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
func (_c *URLStatCreate) SetNillableUpdatedAt(v *time.Time) *URLStatCreate {
	if v != nil {
		_c.SetUpdatedAt(*v)
	}
	return _c
}

// SetURLPath sets the "url_path" field.
func (_c *URLStatCreate) SetURLPath(v string) *URLStatCreate {
	_c.mutation.SetURLPath(v)
	return _c
}

// SetPageTitle sets the "page_title" field.
func (_c *URLStatCreate) SetPageTitle(v string) *URLStatCreate {
	_c.mutation.SetPageTitle(v)
	return _c
}

// SetNillablePageTitle sets the "page_title" field if the given value is not nil.
func (_c *URLStatCreate) SetNillablePageTitle(v *string) *URLStatCreate {
	if v != nil {
		_c.SetPageTitle(*v)
	}
	return _c
}

// SetTotalViews sets the "total_views" field.
func (_c *URLStatCreate) SetTotalViews(v int64) *URLStatCreate {
	_c.mutation.SetTotalViews(v)
	return _c
}

// SetNillableTotalViews sets the "total_views" field if the given value is not nil.
func (_c *URLStatCreate) SetNillableTotalViews(v *int64) *URLStatCreate {
	if v != nil {
		_c.SetTotalViews(*v)
	}
	return _c
}

// SetUniqueViews sets the "unique_views" field.
func (_c *URLStatCreate) SetUniqueViews(v int64) *URLStatCreate {
	_c.mutation.SetUniqueViews(v)
	return _c
}

// SetNillableUniqueViews sets the "unique_views" field if the given value is not nil.
func (_c *URLStatCreate) SetNillableUniqueViews(v *int64) *URLStatCreate {
	if v != nil {
		_c.SetUniqueViews(*v)
	}
	return _c
}

// SetBounceCount sets the "bounce_count" field.
func (_c *URLStatCreate) SetBounceCount(v int64) *URLStatCreate {
	_c.mutation.SetBounceCount(v)
	return _c
}

// SetNillableBounceCount sets the "bounce_count" field if the given value is not nil.
func (_c *URLStatCreate) SetNillableBounceCount(v *int64) *URLStatCreate {
	if v != nil {
		_c.SetBounceCount(*v)
	}
	return _c
}

// SetAvgDuration sets the "avg_duration" field.
func (_c *URLStatCreate) SetAvgDuration(v float64) *URLStatCreate {
	_c.mutation.SetAvgDuration(v)
	return _c
}

// SetNillableAvgDuration sets the "avg_duration" field if the given value is not nil.
func (_c *URLStatCreate) SetNillableAvgDuration(v *float64) *URLStatCreate {
	if v != nil {
		_c.SetAvgDuration(*v)
	}
	return _c
}

// SetLastVisitedAt sets the "last_visited_at" field.
func (_c *URLStatCreate) SetLastVisitedAt(v time.Time) *URLStatCreate {
	_c.mutation.SetLastVisitedAt(v)
	return _c
}

// SetNillableLastVisitedAt sets the "last_visited_at" field if the given value is not nil.
func (_c *URLStatCreate) SetNillableLastVisitedAt(v *time.Time) *URLStatCreate {
	if v != nil {
		_c.SetLastVisitedAt(*v)
	}
	return _c
}

// SetID sets the "id" field.
func (_c *URLStatCreate) SetID(v uint) *URLStatCreate {
	_c.mutation.SetID(v)
	return _c
}

// Mutation returns the URLStatMutation object of the builder.
func (_c *URLStatCreate) Mutation() *URLStatMutation {
	return _c.mutation
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (_c *URLStatCreate) defaults() {
	if _, ok := _c.mutation.CreatedAt(); !ok {
		v := urlstat.DefaultCreatedAt()
		_c.mutation.SetCreatedAt(v)
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		v := urlstat.DefaultUpdatedAt()
		_c.mutation.SetUpdatedAt(v)
	}
	if _, ok := _c.mutation.TotalViews(); !ok {
		v := urlstat.DefaultTotalViews
		_c.mutation.SetTotalViews(v)
	}
	if _, ok := _c.mutation.UniqueViews(); !ok {
		v := urlstat.DefaultUniqueViews
		_c.mutation.SetUniqueViews(v)
	}
	if _, ok := _c.mutation.BounceCount(); !ok {
		v := urlstat.DefaultBounceCount
		_c.mutation.SetBounceCount(v)
	}
	if _, ok := _c.mutation.AvgDuration(); !ok {
		v := urlstat.DefaultAvgDuration
		_c.mutation.SetAvgDuration(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_c *URLStatCreate) check() error {
	if _, ok := _c.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "URLStat.created_at"`)}
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "URLStat.updated_at"`)}
	}
	if _, ok := _c.mutation.URLPath(); !ok {
		return &ValidationError{Name: "url_path", err: errors.New(`ent: missing required field "URLStat.url_path"`)}
	}
	if v, ok := _c.mutation.URLPath(); ok {
		if err := urlstat.URLPathValidator(v); err != nil {
			return &ValidationError{Name: "url_path", err: fmt.Errorf(`ent: validator failed for field "URLStat.url_path": %w`, err)}
		}
	}
	if v, ok := _c.mutation.PageTitle(); ok {
		if err := urlstat.PageTitleValidator(v); err != nil {
			return &ValidationError{Name: "page_title", err: fmt.Errorf(`ent: validator failed for field "URLStat.page_title": %w`, err)}
		}
	}
	if _, ok := _c.mutation.TotalViews(); !ok {
		return &ValidationError{Name: "total_views", err: errors.New(`ent: missing required field "URLStat.total_views"`)}
	}
	if _, ok := _c.mutation.UniqueViews(); !ok {
		return &ValidationError{Name: "unique_views", err: errors.New(`ent: missing required field "URLStat.unique_views"`)}
	}
	if _, ok := _c.mutation.BounceCount(); !ok {
		return &ValidationError{Name: "bounce_count", err: errors.New(`ent: missing required field "URLStat.bounce_count"`)}
	}
	if _, ok := _c.mutation.AvgDuration(); !ok {
		return &ValidationError{Name: "avg_duration", err: errors.New(`ent: missing required field "URLStat.avg_duration"`)}
	}
	return nil
}

func (_c *URLStatCreate) sqlSave(ctx context.Context) (*URLStat, error) {
	if err := _c.check(); err != nil {
		return nil, err
	}
	_node, _spec := _c.createSpec()
	if err := sqlgraph.CreateNode(ctx, _c.driver, _spec); err != nil {
		if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	if _spec.ID.Value != _node.ID {
		id := _spec.ID.Value.(int64)
		_node.ID = uint(id)
	}
	_c.mutation.id = &_node.ID
	_c.mutation.done = true
	return _node, nil
}

func (_c *URLStatCreate) createSpec() (*URLStat, *sqlgraph.CreateSpec) {
	var (
		_node = &URLStat{config: _c.config}
		_spec = sqlgraph.NewCreateSpec(urlstat.Table, sqlgraph.NewFieldSpec(urlstat.FieldID, field.TypeUint))
	)
	_spec.OnConflict = _c.conflict
	if id, ok := _c.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := _c.mutation.CreatedAt(); ok {
		_spec.SetField(urlstat.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := _c.mutation.UpdatedAt(); ok {
		_spec.SetField(urlstat.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	if value, ok := _c.mutation.URLPath(); ok {
		_spec.SetField(urlstat.FieldURLPath, field.TypeString, value)
		_node.URLPath = value
	}
	if value, ok := _c.mutation.PageTitle(); ok {
		_spec.SetField(urlstat.FieldPageTitle, field.TypeString, value)
		_node.PageTitle = &value
	}
	if value, ok := _c.mutation.TotalViews(); ok {
		_spec.SetField(urlstat.FieldTotalViews, field.TypeInt64, value)
		_node.TotalViews = value
	}
	if value, ok := _c.mutation.UniqueViews(); ok {
		_spec.SetField(urlstat.FieldUniqueViews, field.TypeInt64, value)
		_node.UniqueViews = value
	}
	if value, ok := _c.mutation.BounceCount(); ok {
		_spec.SetField(urlstat.FieldBounceCount, field.TypeInt64, value)
		_node.BounceCount = value
	}
	if value, ok := _c.mutation.AvgDuration(); ok {
		_spec.SetField(urlstat.FieldAvgDuration, field.TypeFloat64, value)
		_node.AvgDuration = value
	}
	if value, ok := _c.mutation.LastVisitedAt(); ok {
		_spec.SetField(urlstat.FieldLastVisitedAt, field.TypeTime, value)
		_node.LastVisitedAt = &value
	}
	return _node, _spec
}

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.URLStat.Create().
//		SetCreatedAt(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.URLStatUpsert) {
//			SetCreatedAt(v+v).
//		}).
//		Exec(ctx)
func (_c *URLStatCreate) OnConflict(opts ...sql.ConflictOption) *URLStatUpsertOne {
	_c.conflict = opts
	return &URLStatUpsertOne{
		create: _c,
	}
}

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

type (
	// URLStatUpsertOne is the builder for "upsert"-ing
	//  one URLStat node.
	URLStatUpsertOne struct {
		create *URLStatCreate
	}

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

// SetUpdatedAt sets the "updated_at" field.
func (u *URLStatUpsert) SetUpdatedAt(v time.Time) *URLStatUpsert {
	u.Set(urlstat.FieldUpdatedAt, v)
	return u
}

// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *URLStatUpsert) UpdateUpdatedAt() *URLStatUpsert {
	u.SetExcluded(urlstat.FieldUpdatedAt)
	return u
}

// SetURLPath sets the "url_path" field.
func (u *URLStatUpsert) SetURLPath(v string) *URLStatUpsert {
	u.Set(urlstat.FieldURLPath, v)
	return u
}

// UpdateURLPath sets the "url_path" field to the value that was provided on create.
func (u *URLStatUpsert) UpdateURLPath() *URLStatUpsert {
	u.SetExcluded(urlstat.FieldURLPath)
	return u
}

// SetPageTitle sets the "page_title" field.
func (u *URLStatUpsert) SetPageTitle(v string) *URLStatUpsert {
	u.Set(urlstat.FieldPageTitle, v)
	return u
}

// UpdatePageTitle sets the "page_title" field to the value that was provided on create.
func (u *URLStatUpsert) UpdatePageTitle() *URLStatUpsert {
	u.SetExcluded(urlstat.FieldPageTitle)
	return u
}

// ClearPageTitle clears the value of the "page_title" field.
func (u *URLStatUpsert) ClearPageTitle() *URLStatUpsert {
	u.SetNull(urlstat.FieldPageTitle)
	return u
}

// SetTotalViews sets the "total_views" field.
func (u *URLStatUpsert) SetTotalViews(v int64) *URLStatUpsert {
	u.Set(urlstat.FieldTotalViews, v)
	return u
}

// UpdateTotalViews sets the "total_views" field to the value that was provided on create.
func (u *URLStatUpsert) UpdateTotalViews() *URLStatUpsert {
	u.SetExcluded(urlstat.FieldTotalViews)
	return u
}

// AddTotalViews adds v to the "total_views" field.
func (u *URLStatUpsert) AddTotalViews(v int64) *URLStatUpsert {
	u.Add(urlstat.FieldTotalViews, v)
	return u
}

// SetUniqueViews sets the "unique_views" field.
func (u *URLStatUpsert) SetUniqueViews(v int64) *URLStatUpsert {
	u.Set(urlstat.FieldUniqueViews, v)
	return u
}

// UpdateUniqueViews sets the "unique_views" field to the value that was provided on create.
func (u *URLStatUpsert) UpdateUniqueViews() *URLStatUpsert {
	u.SetExcluded(urlstat.FieldUniqueViews)
	return u
}

// AddUniqueViews adds v to the "unique_views" field.
func (u *URLStatUpsert) AddUniqueViews(v int64) *URLStatUpsert {
	u.Add(urlstat.FieldUniqueViews, v)
	return u
}

// SetBounceCount sets the "bounce_count" field.
func (u *URLStatUpsert) SetBounceCount(v int64) *URLStatUpsert {
	u.Set(urlstat.FieldBounceCount, v)
	return u
}

// UpdateBounceCount sets the "bounce_count" field to the value that was provided on create.
func (u *URLStatUpsert) UpdateBounceCount() *URLStatUpsert {
	u.SetExcluded(urlstat.FieldBounceCount)
	return u
}

// AddBounceCount adds v to the "bounce_count" field.
func (u *URLStatUpsert) AddBounceCount(v int64) *URLStatUpsert {
	u.Add(urlstat.FieldBounceCount, v)
	return u
}

// SetAvgDuration sets the "avg_duration" field.
func (u *URLStatUpsert) SetAvgDuration(v float64) *URLStatUpsert {
	u.Set(urlstat.FieldAvgDuration, v)
	return u
}

// UpdateAvgDuration sets the "avg_duration" field to the value that was provided on create.
func (u *URLStatUpsert) UpdateAvgDuration() *URLStatUpsert {
	u.SetExcluded(urlstat.FieldAvgDuration)
	return u
}

// AddAvgDuration adds v to the "avg_duration" field.
func (u *URLStatUpsert) AddAvgDuration(v float64) *URLStatUpsert {
	u.Add(urlstat.FieldAvgDuration, v)
	return u
}

// SetLastVisitedAt sets the "last_visited_at" field.
func (u *URLStatUpsert) SetLastVisitedAt(v time.Time) *URLStatUpsert {
	u.Set(urlstat.FieldLastVisitedAt, v)
	return u
}

// UpdateLastVisitedAt sets the "last_visited_at" field to the value that was provided on create.
func (u *URLStatUpsert) UpdateLastVisitedAt() *URLStatUpsert {
	u.SetExcluded(urlstat.FieldLastVisitedAt)
	return u
}

// ClearLastVisitedAt clears the value of the "last_visited_at" field.
func (u *URLStatUpsert) ClearLastVisitedAt() *URLStatUpsert {
	u.SetNull(urlstat.FieldLastVisitedAt)
	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.URLStat.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(urlstat.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *URLStatUpsertOne) UpdateNewValues() *URLStatUpsertOne {
	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(urlstat.FieldID)
		}
		if _, exists := u.create.mutation.CreatedAt(); exists {
			s.SetIgnore(urlstat.FieldCreatedAt)
		}
	}))
	return u
}

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

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

// SetUpdatedAt sets the "updated_at" field.
func (u *URLStatUpsertOne) SetUpdatedAt(v time.Time) *URLStatUpsertOne {
	return u.Update(func(s *URLStatUpsert) {
		s.SetUpdatedAt(v)
	})
}

// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *URLStatUpsertOne) UpdateUpdatedAt() *URLStatUpsertOne {
	return u.Update(func(s *URLStatUpsert) {
		s.UpdateUpdatedAt()
	})
}

// SetURLPath sets the "url_path" field.
func (u *URLStatUpsertOne) SetURLPath(v string) *URLStatUpsertOne {
	return u.Update(func(s *URLStatUpsert) {
		s.SetURLPath(v)
	})
}

// UpdateURLPath sets the "url_path" field to the value that was provided on create.
func (u *URLStatUpsertOne) UpdateURLPath() *URLStatUpsertOne {
	return u.Update(func(s *URLStatUpsert) {
		s.UpdateURLPath()
	})
}

// SetPageTitle sets the "page_title" field.
func (u *URLStatUpsertOne) SetPageTitle(v string) *URLStatUpsertOne {
	return u.Update(func(s *URLStatUpsert) {
		s.SetPageTitle(v)
	})
}

// UpdatePageTitle sets the "page_title" field to the value that was provided on create.
func (u *URLStatUpsertOne) UpdatePageTitle() *URLStatUpsertOne {
	return u.Update(func(s *URLStatUpsert) {
		s.UpdatePageTitle()
	})
}

// ClearPageTitle clears the value of the "page_title" field.
func (u *URLStatUpsertOne) ClearPageTitle() *URLStatUpsertOne {
	return u.Update(func(s *URLStatUpsert) {
		s.ClearPageTitle()
	})
}

// SetTotalViews sets the "total_views" field.
func (u *URLStatUpsertOne) SetTotalViews(v int64) *URLStatUpsertOne {
	return u.Update(func(s *URLStatUpsert) {
		s.SetTotalViews(v)
	})
}

// AddTotalViews adds v to the "total_views" field.
func (u *URLStatUpsertOne) AddTotalViews(v int64) *URLStatUpsertOne {
	return u.Update(func(s *URLStatUpsert) {
		s.AddTotalViews(v)
	})
}

// UpdateTotalViews sets the "total_views" field to the value that was provided on create.
func (u *URLStatUpsertOne) UpdateTotalViews() *URLStatUpsertOne {
	return u.Update(func(s *URLStatUpsert) {
		s.UpdateTotalViews()
	})
}

// SetUniqueViews sets the "unique_views" field.
func (u *URLStatUpsertOne) SetUniqueViews(v int64) *URLStatUpsertOne {
	return u.Update(func(s *URLStatUpsert) {
		s.SetUniqueViews(v)
	})
}

// AddUniqueViews adds v to the "unique_views" field.
func (u *URLStatUpsertOne) AddUniqueViews(v int64) *URLStatUpsertOne {
	return u.Update(func(s *URLStatUpsert) {
		s.AddUniqueViews(v)
	})
}

// UpdateUniqueViews sets the "unique_views" field to the value that was provided on create.
func (u *URLStatUpsertOne) UpdateUniqueViews() *URLStatUpsertOne {
	return u.Update(func(s *URLStatUpsert) {
		s.UpdateUniqueViews()
	})
}

// SetBounceCount sets the "bounce_count" field.
func (u *URLStatUpsertOne) SetBounceCount(v int64) *URLStatUpsertOne {
	return u.Update(func(s *URLStatUpsert) {
		s.SetBounceCount(v)
	})
}

// AddBounceCount adds v to the "bounce_count" field.
func (u *URLStatUpsertOne) AddBounceCount(v int64) *URLStatUpsertOne {
	return u.Update(func(s *URLStatUpsert) {
		s.AddBounceCount(v)
	})
}

// UpdateBounceCount sets the "bounce_count" field to the value that was provided on create.
func (u *URLStatUpsertOne) UpdateBounceCount() *URLStatUpsertOne {
	return u.Update(func(s *URLStatUpsert) {
		s.UpdateBounceCount()
	})
}

// SetAvgDuration sets the "avg_duration" field.
func (u *URLStatUpsertOne) SetAvgDuration(v float64) *URLStatUpsertOne {
	return u.Update(func(s *URLStatUpsert) {
		s.SetAvgDuration(v)
	})
}

// AddAvgDuration adds v to the "avg_duration" field.
func (u *URLStatUpsertOne) AddAvgDuration(v float64) *URLStatUpsertOne {
	return u.Update(func(s *URLStatUpsert) {
		s.AddAvgDuration(v)
	})
}

// UpdateAvgDuration sets the "avg_duration" field to the value that was provided on create.
func (u *URLStatUpsertOne) UpdateAvgDuration() *URLStatUpsertOne {
	return u.Update(func(s *URLStatUpsert) {
		s.UpdateAvgDuration()
	})
}

// SetLastVisitedAt sets the "last_visited_at" field.
func (u *URLStatUpsertOne) SetLastVisitedAt(v time.Time) *URLStatUpsertOne {
	return u.Update(func(s *URLStatUpsert) {
		s.SetLastVisitedAt(v)
	})
}

// UpdateLastVisitedAt sets the "last_visited_at" field to the value that was provided on create.
func (u *URLStatUpsertOne) UpdateLastVisitedAt() *URLStatUpsertOne {
	return u.Update(func(s *URLStatUpsert) {
		s.UpdateLastVisitedAt()
	})
}

// ClearLastVisitedAt clears the value of the "last_visited_at" field.
func (u *URLStatUpsertOne) ClearLastVisitedAt() *URLStatUpsertOne {
	return u.Update(func(s *URLStatUpsert) {
		s.ClearLastVisitedAt()
	})
}

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

// ExecX is like Exec, but panics if an error occurs.
func (u *URLStatUpsertOne) 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 *URLStatUpsertOne) ID(ctx context.Context) (id uint, err error) {
	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 *URLStatUpsertOne) IDX(ctx context.Context) uint {
	id, err := u.ID(ctx)
	if err != nil {
		panic(err)
	}
	return id
}

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

// Save creates the URLStat entities in the database.
func (_c *URLStatCreateBulk) Save(ctx context.Context) ([]*URLStat, error) {
	if _c.err != nil {
		return nil, _c.err
	}
	specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
	nodes := make([]*URLStat, len(_c.builders))
	mutators := make([]Mutator, len(_c.builders))
	for i := range _c.builders {
		func(i int, root context.Context) {
			builder := _c.builders[i]
			builder.defaults()
			var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
				mutation, ok := m.(*URLStatMutation)
				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, _c.builders[i+1].mutation)
				} else {
					spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
					spec.OnConflict = _c.conflict
					// Invoke the actual operation on the latest mutation in the chain.
					if err = sqlgraph.BatchCreate(ctx, _c.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
				if specs[i].ID.Value != nil && nodes[i].ID == 0 {
					id := specs[i].ID.Value.(int64)
					nodes[i].ID = uint(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, _c.builders[0].mutation); err != nil {
			return nil, err
		}
	}
	return nodes, nil
}

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

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

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

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.URLStat.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.URLStatUpsert) {
//			SetCreatedAt(v+v).
//		}).
//		Exec(ctx)
func (_c *URLStatCreateBulk) OnConflict(opts ...sql.ConflictOption) *URLStatUpsertBulk {
	_c.conflict = opts
	return &URLStatUpsertBulk{
		create: _c,
	}
}

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

// URLStatUpsertBulk is the builder for "upsert"-ing
// a bulk of URLStat nodes.
type URLStatUpsertBulk struct {
	create *URLStatCreateBulk
}

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

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

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

// SetUpdatedAt sets the "updated_at" field.
func (u *URLStatUpsertBulk) SetUpdatedAt(v time.Time) *URLStatUpsertBulk {
	return u.Update(func(s *URLStatUpsert) {
		s.SetUpdatedAt(v)
	})
}

// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *URLStatUpsertBulk) UpdateUpdatedAt() *URLStatUpsertBulk {
	return u.Update(func(s *URLStatUpsert) {
		s.UpdateUpdatedAt()
	})
}

// SetURLPath sets the "url_path" field.
func (u *URLStatUpsertBulk) SetURLPath(v string) *URLStatUpsertBulk {
	return u.Update(func(s *URLStatUpsert) {
		s.SetURLPath(v)
	})
}

// UpdateURLPath sets the "url_path" field to the value that was provided on create.
func (u *URLStatUpsertBulk) UpdateURLPath() *URLStatUpsertBulk {
	return u.Update(func(s *URLStatUpsert) {
		s.UpdateURLPath()
	})
}

// SetPageTitle sets the "page_title" field.
func (u *URLStatUpsertBulk) SetPageTitle(v string) *URLStatUpsertBulk {
	return u.Update(func(s *URLStatUpsert) {
		s.SetPageTitle(v)
	})
}

// UpdatePageTitle sets the "page_title" field to the value that was provided on create.
func (u *URLStatUpsertBulk) UpdatePageTitle() *URLStatUpsertBulk {
	return u.Update(func(s *URLStatUpsert) {
		s.UpdatePageTitle()
	})
}

// ClearPageTitle clears the value of the "page_title" field.
func (u *URLStatUpsertBulk) ClearPageTitle() *URLStatUpsertBulk {
	return u.Update(func(s *URLStatUpsert) {
		s.ClearPageTitle()
	})
}

// SetTotalViews sets the "total_views" field.
func (u *URLStatUpsertBulk) SetTotalViews(v int64) *URLStatUpsertBulk {
	return u.Update(func(s *URLStatUpsert) {
		s.SetTotalViews(v)
	})
}

// AddTotalViews adds v to the "total_views" field.
func (u *URLStatUpsertBulk) AddTotalViews(v int64) *URLStatUpsertBulk {
	return u.Update(func(s *URLStatUpsert) {
		s.AddTotalViews(v)
	})
}

// UpdateTotalViews sets the "total_views" field to the value that was provided on create.
func (u *URLStatUpsertBulk) UpdateTotalViews() *URLStatUpsertBulk {
	return u.Update(func(s *URLStatUpsert) {
		s.UpdateTotalViews()
	})
}

// SetUniqueViews sets the "unique_views" field.
func (u *URLStatUpsertBulk) SetUniqueViews(v int64) *URLStatUpsertBulk {
	return u.Update(func(s *URLStatUpsert) {
		s.SetUniqueViews(v)
	})
}

// AddUniqueViews adds v to the "unique_views" field.
func (u *URLStatUpsertBulk) AddUniqueViews(v int64) *URLStatUpsertBulk {
	return u.Update(func(s *URLStatUpsert) {
		s.AddUniqueViews(v)
	})
}

// UpdateUniqueViews sets the "unique_views" field to the value that was provided on create.
func (u *URLStatUpsertBulk) UpdateUniqueViews() *URLStatUpsertBulk {
	return u.Update(func(s *URLStatUpsert) {
		s.UpdateUniqueViews()
	})
}

// SetBounceCount sets the "bounce_count" field.
func (u *URLStatUpsertBulk) SetBounceCount(v int64) *URLStatUpsertBulk {
	return u.Update(func(s *URLStatUpsert) {
		s.SetBounceCount(v)
	})
}

// AddBounceCount adds v to the "bounce_count" field.
func (u *URLStatUpsertBulk) AddBounceCount(v int64) *URLStatUpsertBulk {
	return u.Update(func(s *URLStatUpsert) {
		s.AddBounceCount(v)
	})
}

// UpdateBounceCount sets the "bounce_count" field to the value that was provided on create.
func (u *URLStatUpsertBulk) UpdateBounceCount() *URLStatUpsertBulk {
	return u.Update(func(s *URLStatUpsert) {
		s.UpdateBounceCount()
	})
}

// SetAvgDuration sets the "avg_duration" field.
func (u *URLStatUpsertBulk) SetAvgDuration(v float64) *URLStatUpsertBulk {
	return u.Update(func(s *URLStatUpsert) {
		s.SetAvgDuration(v)
	})
}

// AddAvgDuration adds v to the "avg_duration" field.
func (u *URLStatUpsertBulk) AddAvgDuration(v float64) *URLStatUpsertBulk {
	return u.Update(func(s *URLStatUpsert) {
		s.AddAvgDuration(v)
	})
}

// UpdateAvgDuration sets the "avg_duration" field to the value that was provided on create.
func (u *URLStatUpsertBulk) UpdateAvgDuration() *URLStatUpsertBulk {
	return u.Update(func(s *URLStatUpsert) {
		s.UpdateAvgDuration()
	})
}

// SetLastVisitedAt sets the "last_visited_at" field.
func (u *URLStatUpsertBulk) SetLastVisitedAt(v time.Time) *URLStatUpsertBulk {
	return u.Update(func(s *URLStatUpsert) {
		s.SetLastVisitedAt(v)
	})
}

// UpdateLastVisitedAt sets the "last_visited_at" field to the value that was provided on create.
func (u *URLStatUpsertBulk) UpdateLastVisitedAt() *URLStatUpsertBulk {
	return u.Update(func(s *URLStatUpsert) {
		s.UpdateLastVisitedAt()
	})
}

// ClearLastVisitedAt clears the value of the "last_visited_at" field.
func (u *URLStatUpsertBulk) ClearLastVisitedAt() *URLStatUpsertBulk {
	return u.Update(func(s *URLStatUpsert) {
		s.ClearLastVisitedAt()
	})
}

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

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