// 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/announcement"
	"github.com/Wei-Shaw/sub2api/ent/announcementread"
	"github.com/Wei-Shaw/sub2api/internal/domain"
)

// AnnouncementCreate is the builder for creating a Announcement entity.
type AnnouncementCreate struct {
	config
	mutation *AnnouncementMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

// SetTitle sets the "title" field.
func (_c *AnnouncementCreate) SetTitle(v string) *AnnouncementCreate {
	_c.mutation.SetTitle(v)
	return _c
}

// SetContent sets the "content" field.
func (_c *AnnouncementCreate) SetContent(v string) *AnnouncementCreate {
	_c.mutation.SetContent(v)
	return _c
}

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

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

// SetTargeting sets the "targeting" field.
func (_c *AnnouncementCreate) SetTargeting(v domain.AnnouncementTargeting) *AnnouncementCreate {
	_c.mutation.SetTargeting(v)
	return _c
}

// SetNillableTargeting sets the "targeting" field if the given value is not nil.
func (_c *AnnouncementCreate) SetNillableTargeting(v *domain.AnnouncementTargeting) *AnnouncementCreate {
	if v != nil {
		_c.SetTargeting(*v)
	}
	return _c
}

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

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

// SetEndsAt sets the "ends_at" field.
func (_c *AnnouncementCreate) SetEndsAt(v time.Time) *AnnouncementCreate {
	_c.mutation.SetEndsAt(v)
	return _c
}

// SetNillableEndsAt sets the "ends_at" field if the given value is not nil.
func (_c *AnnouncementCreate) SetNillableEndsAt(v *time.Time) *AnnouncementCreate {
	if v != nil {
		_c.SetEndsAt(*v)
	}
	return _c
}

// SetCreatedBy sets the "created_by" field.
func (_c *AnnouncementCreate) SetCreatedBy(v int64) *AnnouncementCreate {
	_c.mutation.SetCreatedBy(v)
	return _c
}

// SetNillableCreatedBy sets the "created_by" field if the given value is not nil.
func (_c *AnnouncementCreate) SetNillableCreatedBy(v *int64) *AnnouncementCreate {
	if v != nil {
		_c.SetCreatedBy(*v)
	}
	return _c
}

// SetUpdatedBy sets the "updated_by" field.
func (_c *AnnouncementCreate) SetUpdatedBy(v int64) *AnnouncementCreate {
	_c.mutation.SetUpdatedBy(v)
	return _c
}

// SetNillableUpdatedBy sets the "updated_by" field if the given value is not nil.
func (_c *AnnouncementCreate) SetNillableUpdatedBy(v *int64) *AnnouncementCreate {
	if v != nil {
		_c.SetUpdatedBy(*v)
	}
	return _c
}

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

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

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

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

// AddReadIDs adds the "reads" edge to the AnnouncementRead entity by IDs.
func (_c *AnnouncementCreate) AddReadIDs(ids ...int64) *AnnouncementCreate {
	_c.mutation.AddReadIDs(ids...)
	return _c
}

// AddReads adds the "reads" edges to the AnnouncementRead entity.
func (_c *AnnouncementCreate) AddReads(v ...*AnnouncementRead) *AnnouncementCreate {
	ids := make([]int64, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _c.AddReadIDs(ids...)
}

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

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

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_c *AnnouncementCreate) 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 *AnnouncementCreate) defaults() {
	if _, ok := _c.mutation.Status(); !ok {
		v := announcement.DefaultStatus
		_c.mutation.SetStatus(v)
	}
	if _, ok := _c.mutation.CreatedAt(); !ok {
		v := announcement.DefaultCreatedAt()
		_c.mutation.SetCreatedAt(v)
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		v := announcement.DefaultUpdatedAt()
		_c.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_c *AnnouncementCreate) check() error {
	if _, ok := _c.mutation.Title(); !ok {
		return &ValidationError{Name: "title", err: errors.New(`ent: missing required field "Announcement.title"`)}
	}
	if v, ok := _c.mutation.Title(); ok {
		if err := announcement.TitleValidator(v); err != nil {
			return &ValidationError{Name: "title", err: fmt.Errorf(`ent: validator failed for field "Announcement.title": %w`, err)}
		}
	}
	if _, ok := _c.mutation.Content(); !ok {
		return &ValidationError{Name: "content", err: errors.New(`ent: missing required field "Announcement.content"`)}
	}
	if v, ok := _c.mutation.Content(); ok {
		if err := announcement.ContentValidator(v); err != nil {
			return &ValidationError{Name: "content", err: fmt.Errorf(`ent: validator failed for field "Announcement.content": %w`, err)}
		}
	}
	if _, ok := _c.mutation.Status(); !ok {
		return &ValidationError{Name: "status", err: errors.New(`ent: missing required field "Announcement.status"`)}
	}
	if v, ok := _c.mutation.Status(); ok {
		if err := announcement.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "Announcement.status": %w`, err)}
		}
	}
	if _, ok := _c.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Announcement.created_at"`)}
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "Announcement.updated_at"`)}
	}
	return nil
}

func (_c *AnnouncementCreate) sqlSave(ctx context.Context) (*Announcement, 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
	}
	id := _spec.ID.Value.(int64)
	_node.ID = int64(id)
	_c.mutation.id = &_node.ID
	_c.mutation.done = true
	return _node, nil
}

func (_c *AnnouncementCreate) createSpec() (*Announcement, *sqlgraph.CreateSpec) {
	var (
		_node = &Announcement{config: _c.config}
		_spec = sqlgraph.NewCreateSpec(announcement.Table, sqlgraph.NewFieldSpec(announcement.FieldID, field.TypeInt64))
	)
	_spec.OnConflict = _c.conflict
	if value, ok := _c.mutation.Title(); ok {
		_spec.SetField(announcement.FieldTitle, field.TypeString, value)
		_node.Title = value
	}
	if value, ok := _c.mutation.Content(); ok {
		_spec.SetField(announcement.FieldContent, field.TypeString, value)
		_node.Content = value
	}
	if value, ok := _c.mutation.Status(); ok {
		_spec.SetField(announcement.FieldStatus, field.TypeString, value)
		_node.Status = value
	}
	if value, ok := _c.mutation.Targeting(); ok {
		_spec.SetField(announcement.FieldTargeting, field.TypeJSON, value)
		_node.Targeting = value
	}
	if value, ok := _c.mutation.StartsAt(); ok {
		_spec.SetField(announcement.FieldStartsAt, field.TypeTime, value)
		_node.StartsAt = &value
	}
	if value, ok := _c.mutation.EndsAt(); ok {
		_spec.SetField(announcement.FieldEndsAt, field.TypeTime, value)
		_node.EndsAt = &value
	}
	if value, ok := _c.mutation.CreatedBy(); ok {
		_spec.SetField(announcement.FieldCreatedBy, field.TypeInt64, value)
		_node.CreatedBy = &value
	}
	if value, ok := _c.mutation.UpdatedBy(); ok {
		_spec.SetField(announcement.FieldUpdatedBy, field.TypeInt64, value)
		_node.UpdatedBy = &value
	}
	if value, ok := _c.mutation.CreatedAt(); ok {
		_spec.SetField(announcement.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := _c.mutation.UpdatedAt(); ok {
		_spec.SetField(announcement.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	if nodes := _c.mutation.ReadsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   announcement.ReadsTable,
			Columns: []string{announcement.ReadsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(announcementread.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges = append(_spec.Edges, edge)
	}
	return _node, _spec
}

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

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

type (
	// AnnouncementUpsertOne is the builder for "upsert"-ing
	//  one Announcement node.
	AnnouncementUpsertOne struct {
		create *AnnouncementCreate
	}

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

// SetTitle sets the "title" field.
func (u *AnnouncementUpsert) SetTitle(v string) *AnnouncementUpsert {
	u.Set(announcement.FieldTitle, v)
	return u
}

// UpdateTitle sets the "title" field to the value that was provided on create.
func (u *AnnouncementUpsert) UpdateTitle() *AnnouncementUpsert {
	u.SetExcluded(announcement.FieldTitle)
	return u
}

// SetContent sets the "content" field.
func (u *AnnouncementUpsert) SetContent(v string) *AnnouncementUpsert {
	u.Set(announcement.FieldContent, v)
	return u
}

// UpdateContent sets the "content" field to the value that was provided on create.
func (u *AnnouncementUpsert) UpdateContent() *AnnouncementUpsert {
	u.SetExcluded(announcement.FieldContent)
	return u
}

// SetStatus sets the "status" field.
func (u *AnnouncementUpsert) SetStatus(v string) *AnnouncementUpsert {
	u.Set(announcement.FieldStatus, v)
	return u
}

// UpdateStatus sets the "status" field to the value that was provided on create.
func (u *AnnouncementUpsert) UpdateStatus() *AnnouncementUpsert {
	u.SetExcluded(announcement.FieldStatus)
	return u
}

// SetTargeting sets the "targeting" field.
func (u *AnnouncementUpsert) SetTargeting(v domain.AnnouncementTargeting) *AnnouncementUpsert {
	u.Set(announcement.FieldTargeting, v)
	return u
}

// UpdateTargeting sets the "targeting" field to the value that was provided on create.
func (u *AnnouncementUpsert) UpdateTargeting() *AnnouncementUpsert {
	u.SetExcluded(announcement.FieldTargeting)
	return u
}

// ClearTargeting clears the value of the "targeting" field.
func (u *AnnouncementUpsert) ClearTargeting() *AnnouncementUpsert {
	u.SetNull(announcement.FieldTargeting)
	return u
}

// SetStartsAt sets the "starts_at" field.
func (u *AnnouncementUpsert) SetStartsAt(v time.Time) *AnnouncementUpsert {
	u.Set(announcement.FieldStartsAt, v)
	return u
}

// UpdateStartsAt sets the "starts_at" field to the value that was provided on create.
func (u *AnnouncementUpsert) UpdateStartsAt() *AnnouncementUpsert {
	u.SetExcluded(announcement.FieldStartsAt)
	return u
}

// ClearStartsAt clears the value of the "starts_at" field.
func (u *AnnouncementUpsert) ClearStartsAt() *AnnouncementUpsert {
	u.SetNull(announcement.FieldStartsAt)
	return u
}

// SetEndsAt sets the "ends_at" field.
func (u *AnnouncementUpsert) SetEndsAt(v time.Time) *AnnouncementUpsert {
	u.Set(announcement.FieldEndsAt, v)
	return u
}

// UpdateEndsAt sets the "ends_at" field to the value that was provided on create.
func (u *AnnouncementUpsert) UpdateEndsAt() *AnnouncementUpsert {
	u.SetExcluded(announcement.FieldEndsAt)
	return u
}

// ClearEndsAt clears the value of the "ends_at" field.
func (u *AnnouncementUpsert) ClearEndsAt() *AnnouncementUpsert {
	u.SetNull(announcement.FieldEndsAt)
	return u
}

// SetCreatedBy sets the "created_by" field.
func (u *AnnouncementUpsert) SetCreatedBy(v int64) *AnnouncementUpsert {
	u.Set(announcement.FieldCreatedBy, v)
	return u
}

// UpdateCreatedBy sets the "created_by" field to the value that was provided on create.
func (u *AnnouncementUpsert) UpdateCreatedBy() *AnnouncementUpsert {
	u.SetExcluded(announcement.FieldCreatedBy)
	return u
}

// AddCreatedBy adds v to the "created_by" field.
func (u *AnnouncementUpsert) AddCreatedBy(v int64) *AnnouncementUpsert {
	u.Add(announcement.FieldCreatedBy, v)
	return u
}

// ClearCreatedBy clears the value of the "created_by" field.
func (u *AnnouncementUpsert) ClearCreatedBy() *AnnouncementUpsert {
	u.SetNull(announcement.FieldCreatedBy)
	return u
}

// SetUpdatedBy sets the "updated_by" field.
func (u *AnnouncementUpsert) SetUpdatedBy(v int64) *AnnouncementUpsert {
	u.Set(announcement.FieldUpdatedBy, v)
	return u
}

// UpdateUpdatedBy sets the "updated_by" field to the value that was provided on create.
func (u *AnnouncementUpsert) UpdateUpdatedBy() *AnnouncementUpsert {
	u.SetExcluded(announcement.FieldUpdatedBy)
	return u
}

// AddUpdatedBy adds v to the "updated_by" field.
func (u *AnnouncementUpsert) AddUpdatedBy(v int64) *AnnouncementUpsert {
	u.Add(announcement.FieldUpdatedBy, v)
	return u
}

// ClearUpdatedBy clears the value of the "updated_by" field.
func (u *AnnouncementUpsert) ClearUpdatedBy() *AnnouncementUpsert {
	u.SetNull(announcement.FieldUpdatedBy)
	return u
}

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

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

// UpdateNewValues updates the mutable fields using the new values that were set on create.
// Using this option is equivalent to using:
//
//	client.Announcement.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//		).
//		Exec(ctx)
func (u *AnnouncementUpsertOne) UpdateNewValues() *AnnouncementUpsertOne {
	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.CreatedAt(); exists {
			s.SetIgnore(announcement.FieldCreatedAt)
		}
	}))
	return u
}

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

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

// SetTitle sets the "title" field.
func (u *AnnouncementUpsertOne) SetTitle(v string) *AnnouncementUpsertOne {
	return u.Update(func(s *AnnouncementUpsert) {
		s.SetTitle(v)
	})
}

// UpdateTitle sets the "title" field to the value that was provided on create.
func (u *AnnouncementUpsertOne) UpdateTitle() *AnnouncementUpsertOne {
	return u.Update(func(s *AnnouncementUpsert) {
		s.UpdateTitle()
	})
}

// SetContent sets the "content" field.
func (u *AnnouncementUpsertOne) SetContent(v string) *AnnouncementUpsertOne {
	return u.Update(func(s *AnnouncementUpsert) {
		s.SetContent(v)
	})
}

// UpdateContent sets the "content" field to the value that was provided on create.
func (u *AnnouncementUpsertOne) UpdateContent() *AnnouncementUpsertOne {
	return u.Update(func(s *AnnouncementUpsert) {
		s.UpdateContent()
	})
}

// SetStatus sets the "status" field.
func (u *AnnouncementUpsertOne) SetStatus(v string) *AnnouncementUpsertOne {
	return u.Update(func(s *AnnouncementUpsert) {
		s.SetStatus(v)
	})
}

// UpdateStatus sets the "status" field to the value that was provided on create.
func (u *AnnouncementUpsertOne) UpdateStatus() *AnnouncementUpsertOne {
	return u.Update(func(s *AnnouncementUpsert) {
		s.UpdateStatus()
	})
}

// SetTargeting sets the "targeting" field.
func (u *AnnouncementUpsertOne) SetTargeting(v domain.AnnouncementTargeting) *AnnouncementUpsertOne {
	return u.Update(func(s *AnnouncementUpsert) {
		s.SetTargeting(v)
	})
}

// UpdateTargeting sets the "targeting" field to the value that was provided on create.
func (u *AnnouncementUpsertOne) UpdateTargeting() *AnnouncementUpsertOne {
	return u.Update(func(s *AnnouncementUpsert) {
		s.UpdateTargeting()
	})
}

// ClearTargeting clears the value of the "targeting" field.
func (u *AnnouncementUpsertOne) ClearTargeting() *AnnouncementUpsertOne {
	return u.Update(func(s *AnnouncementUpsert) {
		s.ClearTargeting()
	})
}

// SetStartsAt sets the "starts_at" field.
func (u *AnnouncementUpsertOne) SetStartsAt(v time.Time) *AnnouncementUpsertOne {
	return u.Update(func(s *AnnouncementUpsert) {
		s.SetStartsAt(v)
	})
}

// UpdateStartsAt sets the "starts_at" field to the value that was provided on create.
func (u *AnnouncementUpsertOne) UpdateStartsAt() *AnnouncementUpsertOne {
	return u.Update(func(s *AnnouncementUpsert) {
		s.UpdateStartsAt()
	})
}

// ClearStartsAt clears the value of the "starts_at" field.
func (u *AnnouncementUpsertOne) ClearStartsAt() *AnnouncementUpsertOne {
	return u.Update(func(s *AnnouncementUpsert) {
		s.ClearStartsAt()
	})
}

// SetEndsAt sets the "ends_at" field.
func (u *AnnouncementUpsertOne) SetEndsAt(v time.Time) *AnnouncementUpsertOne {
	return u.Update(func(s *AnnouncementUpsert) {
		s.SetEndsAt(v)
	})
}

// UpdateEndsAt sets the "ends_at" field to the value that was provided on create.
func (u *AnnouncementUpsertOne) UpdateEndsAt() *AnnouncementUpsertOne {
	return u.Update(func(s *AnnouncementUpsert) {
		s.UpdateEndsAt()
	})
}

// ClearEndsAt clears the value of the "ends_at" field.
func (u *AnnouncementUpsertOne) ClearEndsAt() *AnnouncementUpsertOne {
	return u.Update(func(s *AnnouncementUpsert) {
		s.ClearEndsAt()
	})
}

// SetCreatedBy sets the "created_by" field.
func (u *AnnouncementUpsertOne) SetCreatedBy(v int64) *AnnouncementUpsertOne {
	return u.Update(func(s *AnnouncementUpsert) {
		s.SetCreatedBy(v)
	})
}

// AddCreatedBy adds v to the "created_by" field.
func (u *AnnouncementUpsertOne) AddCreatedBy(v int64) *AnnouncementUpsertOne {
	return u.Update(func(s *AnnouncementUpsert) {
		s.AddCreatedBy(v)
	})
}

// UpdateCreatedBy sets the "created_by" field to the value that was provided on create.
func (u *AnnouncementUpsertOne) UpdateCreatedBy() *AnnouncementUpsertOne {
	return u.Update(func(s *AnnouncementUpsert) {
		s.UpdateCreatedBy()
	})
}

// ClearCreatedBy clears the value of the "created_by" field.
func (u *AnnouncementUpsertOne) ClearCreatedBy() *AnnouncementUpsertOne {
	return u.Update(func(s *AnnouncementUpsert) {
		s.ClearCreatedBy()
	})
}

// SetUpdatedBy sets the "updated_by" field.
func (u *AnnouncementUpsertOne) SetUpdatedBy(v int64) *AnnouncementUpsertOne {
	return u.Update(func(s *AnnouncementUpsert) {
		s.SetUpdatedBy(v)
	})
}

// AddUpdatedBy adds v to the "updated_by" field.
func (u *AnnouncementUpsertOne) AddUpdatedBy(v int64) *AnnouncementUpsertOne {
	return u.Update(func(s *AnnouncementUpsert) {
		s.AddUpdatedBy(v)
	})
}

// UpdateUpdatedBy sets the "updated_by" field to the value that was provided on create.
func (u *AnnouncementUpsertOne) UpdateUpdatedBy() *AnnouncementUpsertOne {
	return u.Update(func(s *AnnouncementUpsert) {
		s.UpdateUpdatedBy()
	})
}

// ClearUpdatedBy clears the value of the "updated_by" field.
func (u *AnnouncementUpsertOne) ClearUpdatedBy() *AnnouncementUpsertOne {
	return u.Update(func(s *AnnouncementUpsert) {
		s.ClearUpdatedBy()
	})
}

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

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

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

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

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

// Save creates the Announcement entities in the database.
func (_c *AnnouncementCreateBulk) Save(ctx context.Context) ([]*Announcement, error) {
	if _c.err != nil {
		return nil, _c.err
	}
	specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
	nodes := make([]*Announcement, 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.(*AnnouncementMutation)
				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 {
					id := specs[i].ID.Value.(int64)
					nodes[i].ID = int64(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 *AnnouncementCreateBulk) SaveX(ctx context.Context) []*Announcement {
	v, err := _c.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

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

// ExecX is like Exec, but panics if an error occurs.
func (_c *AnnouncementCreateBulk) 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.Announcement.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.AnnouncementUpsert) {
//			SetTitle(v+v).
//		}).
//		Exec(ctx)
func (_c *AnnouncementCreateBulk) OnConflict(opts ...sql.ConflictOption) *AnnouncementUpsertBulk {
	_c.conflict = opts
	return &AnnouncementUpsertBulk{
		create: _c,
	}
}

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

// AnnouncementUpsertBulk is the builder for "upsert"-ing
// a bulk of Announcement nodes.
type AnnouncementUpsertBulk struct {
	create *AnnouncementCreateBulk
}

// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
//	client.Announcement.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//		).
//		Exec(ctx)
func (u *AnnouncementUpsertBulk) UpdateNewValues() *AnnouncementUpsertBulk {
	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.CreatedAt(); exists {
				s.SetIgnore(announcement.FieldCreatedAt)
			}
		}
	}))
	return u
}

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

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

// SetTitle sets the "title" field.
func (u *AnnouncementUpsertBulk) SetTitle(v string) *AnnouncementUpsertBulk {
	return u.Update(func(s *AnnouncementUpsert) {
		s.SetTitle(v)
	})
}

// UpdateTitle sets the "title" field to the value that was provided on create.
func (u *AnnouncementUpsertBulk) UpdateTitle() *AnnouncementUpsertBulk {
	return u.Update(func(s *AnnouncementUpsert) {
		s.UpdateTitle()
	})
}

// SetContent sets the "content" field.
func (u *AnnouncementUpsertBulk) SetContent(v string) *AnnouncementUpsertBulk {
	return u.Update(func(s *AnnouncementUpsert) {
		s.SetContent(v)
	})
}

// UpdateContent sets the "content" field to the value that was provided on create.
func (u *AnnouncementUpsertBulk) UpdateContent() *AnnouncementUpsertBulk {
	return u.Update(func(s *AnnouncementUpsert) {
		s.UpdateContent()
	})
}

// SetStatus sets the "status" field.
func (u *AnnouncementUpsertBulk) SetStatus(v string) *AnnouncementUpsertBulk {
	return u.Update(func(s *AnnouncementUpsert) {
		s.SetStatus(v)
	})
}

// UpdateStatus sets the "status" field to the value that was provided on create.
func (u *AnnouncementUpsertBulk) UpdateStatus() *AnnouncementUpsertBulk {
	return u.Update(func(s *AnnouncementUpsert) {
		s.UpdateStatus()
	})
}

// SetTargeting sets the "targeting" field.
func (u *AnnouncementUpsertBulk) SetTargeting(v domain.AnnouncementTargeting) *AnnouncementUpsertBulk {
	return u.Update(func(s *AnnouncementUpsert) {
		s.SetTargeting(v)
	})
}

// UpdateTargeting sets the "targeting" field to the value that was provided on create.
func (u *AnnouncementUpsertBulk) UpdateTargeting() *AnnouncementUpsertBulk {
	return u.Update(func(s *AnnouncementUpsert) {
		s.UpdateTargeting()
	})
}

// ClearTargeting clears the value of the "targeting" field.
func (u *AnnouncementUpsertBulk) ClearTargeting() *AnnouncementUpsertBulk {
	return u.Update(func(s *AnnouncementUpsert) {
		s.ClearTargeting()
	})
}

// SetStartsAt sets the "starts_at" field.
func (u *AnnouncementUpsertBulk) SetStartsAt(v time.Time) *AnnouncementUpsertBulk {
	return u.Update(func(s *AnnouncementUpsert) {
		s.SetStartsAt(v)
	})
}

// UpdateStartsAt sets the "starts_at" field to the value that was provided on create.
func (u *AnnouncementUpsertBulk) UpdateStartsAt() *AnnouncementUpsertBulk {
	return u.Update(func(s *AnnouncementUpsert) {
		s.UpdateStartsAt()
	})
}

// ClearStartsAt clears the value of the "starts_at" field.
func (u *AnnouncementUpsertBulk) ClearStartsAt() *AnnouncementUpsertBulk {
	return u.Update(func(s *AnnouncementUpsert) {
		s.ClearStartsAt()
	})
}

// SetEndsAt sets the "ends_at" field.
func (u *AnnouncementUpsertBulk) SetEndsAt(v time.Time) *AnnouncementUpsertBulk {
	return u.Update(func(s *AnnouncementUpsert) {
		s.SetEndsAt(v)
	})
}

// UpdateEndsAt sets the "ends_at" field to the value that was provided on create.
func (u *AnnouncementUpsertBulk) UpdateEndsAt() *AnnouncementUpsertBulk {
	return u.Update(func(s *AnnouncementUpsert) {
		s.UpdateEndsAt()
	})
}

// ClearEndsAt clears the value of the "ends_at" field.
func (u *AnnouncementUpsertBulk) ClearEndsAt() *AnnouncementUpsertBulk {
	return u.Update(func(s *AnnouncementUpsert) {
		s.ClearEndsAt()
	})
}

// SetCreatedBy sets the "created_by" field.
func (u *AnnouncementUpsertBulk) SetCreatedBy(v int64) *AnnouncementUpsertBulk {
	return u.Update(func(s *AnnouncementUpsert) {
		s.SetCreatedBy(v)
	})
}

// AddCreatedBy adds v to the "created_by" field.
func (u *AnnouncementUpsertBulk) AddCreatedBy(v int64) *AnnouncementUpsertBulk {
	return u.Update(func(s *AnnouncementUpsert) {
		s.AddCreatedBy(v)
	})
}

// UpdateCreatedBy sets the "created_by" field to the value that was provided on create.
func (u *AnnouncementUpsertBulk) UpdateCreatedBy() *AnnouncementUpsertBulk {
	return u.Update(func(s *AnnouncementUpsert) {
		s.UpdateCreatedBy()
	})
}

// ClearCreatedBy clears the value of the "created_by" field.
func (u *AnnouncementUpsertBulk) ClearCreatedBy() *AnnouncementUpsertBulk {
	return u.Update(func(s *AnnouncementUpsert) {
		s.ClearCreatedBy()
	})
}

// SetUpdatedBy sets the "updated_by" field.
func (u *AnnouncementUpsertBulk) SetUpdatedBy(v int64) *AnnouncementUpsertBulk {
	return u.Update(func(s *AnnouncementUpsert) {
		s.SetUpdatedBy(v)
	})
}

// AddUpdatedBy adds v to the "updated_by" field.
func (u *AnnouncementUpsertBulk) AddUpdatedBy(v int64) *AnnouncementUpsertBulk {
	return u.Update(func(s *AnnouncementUpsert) {
		s.AddUpdatedBy(v)
	})
}

// UpdateUpdatedBy sets the "updated_by" field to the value that was provided on create.
func (u *AnnouncementUpsertBulk) UpdateUpdatedBy() *AnnouncementUpsertBulk {
	return u.Update(func(s *AnnouncementUpsert) {
		s.UpdateUpdatedBy()
	})
}

// ClearUpdatedBy clears the value of the "updated_by" field.
func (u *AnnouncementUpsertBulk) ClearUpdatedBy() *AnnouncementUpsertBulk {
	return u.Update(func(s *AnnouncementUpsert) {
		s.ClearUpdatedBy()
	})
}

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

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

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

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