// 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/crowdsecurity/crowdsec/pkg/database/ent/alert"
	"github.com/crowdsecurity/crowdsec/pkg/database/ent/decision"
	"github.com/crowdsecurity/crowdsec/pkg/database/ent/event"
	"github.com/crowdsecurity/crowdsec/pkg/database/ent/machine"
	"github.com/crowdsecurity/crowdsec/pkg/database/ent/meta"
)

// AlertCreate is the builder for creating a Alert entity.
type AlertCreate struct {
	config
	mutation *AlertMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

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

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

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

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

// SetScenario sets the "scenario" field.
func (_c *AlertCreate) SetScenario(v string) *AlertCreate {
	_c.mutation.SetScenario(v)
	return _c
}

// SetBucketId sets the "bucketId" field.
func (_c *AlertCreate) SetBucketId(v string) *AlertCreate {
	_c.mutation.SetBucketId(v)
	return _c
}

// SetNillableBucketId sets the "bucketId" field if the given value is not nil.
func (_c *AlertCreate) SetNillableBucketId(v *string) *AlertCreate {
	if v != nil {
		_c.SetBucketId(*v)
	}
	return _c
}

// SetMessage sets the "message" field.
func (_c *AlertCreate) SetMessage(v string) *AlertCreate {
	_c.mutation.SetMessage(v)
	return _c
}

// SetNillableMessage sets the "message" field if the given value is not nil.
func (_c *AlertCreate) SetNillableMessage(v *string) *AlertCreate {
	if v != nil {
		_c.SetMessage(*v)
	}
	return _c
}

// SetEventsCount sets the "eventsCount" field.
func (_c *AlertCreate) SetEventsCount(v int32) *AlertCreate {
	_c.mutation.SetEventsCount(v)
	return _c
}

// SetNillableEventsCount sets the "eventsCount" field if the given value is not nil.
func (_c *AlertCreate) SetNillableEventsCount(v *int32) *AlertCreate {
	if v != nil {
		_c.SetEventsCount(*v)
	}
	return _c
}

// SetStartedAt sets the "startedAt" field.
func (_c *AlertCreate) SetStartedAt(v time.Time) *AlertCreate {
	_c.mutation.SetStartedAt(v)
	return _c
}

// SetNillableStartedAt sets the "startedAt" field if the given value is not nil.
func (_c *AlertCreate) SetNillableStartedAt(v *time.Time) *AlertCreate {
	if v != nil {
		_c.SetStartedAt(*v)
	}
	return _c
}

// SetStoppedAt sets the "stoppedAt" field.
func (_c *AlertCreate) SetStoppedAt(v time.Time) *AlertCreate {
	_c.mutation.SetStoppedAt(v)
	return _c
}

// SetNillableStoppedAt sets the "stoppedAt" field if the given value is not nil.
func (_c *AlertCreate) SetNillableStoppedAt(v *time.Time) *AlertCreate {
	if v != nil {
		_c.SetStoppedAt(*v)
	}
	return _c
}

// SetSourceIp sets the "sourceIp" field.
func (_c *AlertCreate) SetSourceIp(v string) *AlertCreate {
	_c.mutation.SetSourceIp(v)
	return _c
}

// SetNillableSourceIp sets the "sourceIp" field if the given value is not nil.
func (_c *AlertCreate) SetNillableSourceIp(v *string) *AlertCreate {
	if v != nil {
		_c.SetSourceIp(*v)
	}
	return _c
}

// SetSourceRange sets the "sourceRange" field.
func (_c *AlertCreate) SetSourceRange(v string) *AlertCreate {
	_c.mutation.SetSourceRange(v)
	return _c
}

// SetNillableSourceRange sets the "sourceRange" field if the given value is not nil.
func (_c *AlertCreate) SetNillableSourceRange(v *string) *AlertCreate {
	if v != nil {
		_c.SetSourceRange(*v)
	}
	return _c
}

// SetSourceAsNumber sets the "sourceAsNumber" field.
func (_c *AlertCreate) SetSourceAsNumber(v string) *AlertCreate {
	_c.mutation.SetSourceAsNumber(v)
	return _c
}

// SetNillableSourceAsNumber sets the "sourceAsNumber" field if the given value is not nil.
func (_c *AlertCreate) SetNillableSourceAsNumber(v *string) *AlertCreate {
	if v != nil {
		_c.SetSourceAsNumber(*v)
	}
	return _c
}

// SetSourceAsName sets the "sourceAsName" field.
func (_c *AlertCreate) SetSourceAsName(v string) *AlertCreate {
	_c.mutation.SetSourceAsName(v)
	return _c
}

// SetNillableSourceAsName sets the "sourceAsName" field if the given value is not nil.
func (_c *AlertCreate) SetNillableSourceAsName(v *string) *AlertCreate {
	if v != nil {
		_c.SetSourceAsName(*v)
	}
	return _c
}

// SetSourceCountry sets the "sourceCountry" field.
func (_c *AlertCreate) SetSourceCountry(v string) *AlertCreate {
	_c.mutation.SetSourceCountry(v)
	return _c
}

// SetNillableSourceCountry sets the "sourceCountry" field if the given value is not nil.
func (_c *AlertCreate) SetNillableSourceCountry(v *string) *AlertCreate {
	if v != nil {
		_c.SetSourceCountry(*v)
	}
	return _c
}

// SetSourceLatitude sets the "sourceLatitude" field.
func (_c *AlertCreate) SetSourceLatitude(v float32) *AlertCreate {
	_c.mutation.SetSourceLatitude(v)
	return _c
}

// SetNillableSourceLatitude sets the "sourceLatitude" field if the given value is not nil.
func (_c *AlertCreate) SetNillableSourceLatitude(v *float32) *AlertCreate {
	if v != nil {
		_c.SetSourceLatitude(*v)
	}
	return _c
}

// SetSourceLongitude sets the "sourceLongitude" field.
func (_c *AlertCreate) SetSourceLongitude(v float32) *AlertCreate {
	_c.mutation.SetSourceLongitude(v)
	return _c
}

// SetNillableSourceLongitude sets the "sourceLongitude" field if the given value is not nil.
func (_c *AlertCreate) SetNillableSourceLongitude(v *float32) *AlertCreate {
	if v != nil {
		_c.SetSourceLongitude(*v)
	}
	return _c
}

// SetSourceScope sets the "sourceScope" field.
func (_c *AlertCreate) SetSourceScope(v string) *AlertCreate {
	_c.mutation.SetSourceScope(v)
	return _c
}

// SetNillableSourceScope sets the "sourceScope" field if the given value is not nil.
func (_c *AlertCreate) SetNillableSourceScope(v *string) *AlertCreate {
	if v != nil {
		_c.SetSourceScope(*v)
	}
	return _c
}

// SetSourceValue sets the "sourceValue" field.
func (_c *AlertCreate) SetSourceValue(v string) *AlertCreate {
	_c.mutation.SetSourceValue(v)
	return _c
}

// SetNillableSourceValue sets the "sourceValue" field if the given value is not nil.
func (_c *AlertCreate) SetNillableSourceValue(v *string) *AlertCreate {
	if v != nil {
		_c.SetSourceValue(*v)
	}
	return _c
}

// SetCapacity sets the "capacity" field.
func (_c *AlertCreate) SetCapacity(v int32) *AlertCreate {
	_c.mutation.SetCapacity(v)
	return _c
}

// SetNillableCapacity sets the "capacity" field if the given value is not nil.
func (_c *AlertCreate) SetNillableCapacity(v *int32) *AlertCreate {
	if v != nil {
		_c.SetCapacity(*v)
	}
	return _c
}

// SetLeakSpeed sets the "leakSpeed" field.
func (_c *AlertCreate) SetLeakSpeed(v string) *AlertCreate {
	_c.mutation.SetLeakSpeed(v)
	return _c
}

// SetNillableLeakSpeed sets the "leakSpeed" field if the given value is not nil.
func (_c *AlertCreate) SetNillableLeakSpeed(v *string) *AlertCreate {
	if v != nil {
		_c.SetLeakSpeed(*v)
	}
	return _c
}

// SetScenarioVersion sets the "scenarioVersion" field.
func (_c *AlertCreate) SetScenarioVersion(v string) *AlertCreate {
	_c.mutation.SetScenarioVersion(v)
	return _c
}

// SetNillableScenarioVersion sets the "scenarioVersion" field if the given value is not nil.
func (_c *AlertCreate) SetNillableScenarioVersion(v *string) *AlertCreate {
	if v != nil {
		_c.SetScenarioVersion(*v)
	}
	return _c
}

// SetScenarioHash sets the "scenarioHash" field.
func (_c *AlertCreate) SetScenarioHash(v string) *AlertCreate {
	_c.mutation.SetScenarioHash(v)
	return _c
}

// SetNillableScenarioHash sets the "scenarioHash" field if the given value is not nil.
func (_c *AlertCreate) SetNillableScenarioHash(v *string) *AlertCreate {
	if v != nil {
		_c.SetScenarioHash(*v)
	}
	return _c
}

// SetSimulated sets the "simulated" field.
func (_c *AlertCreate) SetSimulated(v bool) *AlertCreate {
	_c.mutation.SetSimulated(v)
	return _c
}

// SetNillableSimulated sets the "simulated" field if the given value is not nil.
func (_c *AlertCreate) SetNillableSimulated(v *bool) *AlertCreate {
	if v != nil {
		_c.SetSimulated(*v)
	}
	return _c
}

// SetUUID sets the "uuid" field.
func (_c *AlertCreate) SetUUID(v string) *AlertCreate {
	_c.mutation.SetUUID(v)
	return _c
}

// SetNillableUUID sets the "uuid" field if the given value is not nil.
func (_c *AlertCreate) SetNillableUUID(v *string) *AlertCreate {
	if v != nil {
		_c.SetUUID(*v)
	}
	return _c
}

// SetRemediation sets the "remediation" field.
func (_c *AlertCreate) SetRemediation(v bool) *AlertCreate {
	_c.mutation.SetRemediation(v)
	return _c
}

// SetNillableRemediation sets the "remediation" field if the given value is not nil.
func (_c *AlertCreate) SetNillableRemediation(v *bool) *AlertCreate {
	if v != nil {
		_c.SetRemediation(*v)
	}
	return _c
}

// SetKind sets the "kind" field.
func (_c *AlertCreate) SetKind(v string) *AlertCreate {
	_c.mutation.SetKind(v)
	return _c
}

// SetNillableKind sets the "kind" field if the given value is not nil.
func (_c *AlertCreate) SetNillableKind(v *string) *AlertCreate {
	if v != nil {
		_c.SetKind(*v)
	}
	return _c
}

// SetOwnerID sets the "owner" edge to the Machine entity by ID.
func (_c *AlertCreate) SetOwnerID(id int) *AlertCreate {
	_c.mutation.SetOwnerID(id)
	return _c
}

// SetNillableOwnerID sets the "owner" edge to the Machine entity by ID if the given value is not nil.
func (_c *AlertCreate) SetNillableOwnerID(id *int) *AlertCreate {
	if id != nil {
		_c = _c.SetOwnerID(*id)
	}
	return _c
}

// SetOwner sets the "owner" edge to the Machine entity.
func (_c *AlertCreate) SetOwner(v *Machine) *AlertCreate {
	return _c.SetOwnerID(v.ID)
}

// AddDecisionIDs adds the "decisions" edge to the Decision entity by IDs.
func (_c *AlertCreate) AddDecisionIDs(ids ...int) *AlertCreate {
	_c.mutation.AddDecisionIDs(ids...)
	return _c
}

// AddDecisions adds the "decisions" edges to the Decision entity.
func (_c *AlertCreate) AddDecisions(v ...*Decision) *AlertCreate {
	ids := make([]int, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _c.AddDecisionIDs(ids...)
}

// AddEventIDs adds the "events" edge to the Event entity by IDs.
func (_c *AlertCreate) AddEventIDs(ids ...int) *AlertCreate {
	_c.mutation.AddEventIDs(ids...)
	return _c
}

// AddEvents adds the "events" edges to the Event entity.
func (_c *AlertCreate) AddEvents(v ...*Event) *AlertCreate {
	ids := make([]int, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _c.AddEventIDs(ids...)
}

// AddMetaIDs adds the "metas" edge to the Meta entity by IDs.
func (_c *AlertCreate) AddMetaIDs(ids ...int) *AlertCreate {
	_c.mutation.AddMetaIDs(ids...)
	return _c
}

// AddMetas adds the "metas" edges to the Meta entity.
func (_c *AlertCreate) AddMetas(v ...*Meta) *AlertCreate {
	ids := make([]int, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _c.AddMetaIDs(ids...)
}

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

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

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_c *AlertCreate) 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 *AlertCreate) defaults() {
	if _, ok := _c.mutation.CreatedAt(); !ok {
		v := alert.DefaultCreatedAt()
		_c.mutation.SetCreatedAt(v)
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		v := alert.DefaultUpdatedAt()
		_c.mutation.SetUpdatedAt(v)
	}
	if _, ok := _c.mutation.BucketId(); !ok {
		v := alert.DefaultBucketId
		_c.mutation.SetBucketId(v)
	}
	if _, ok := _c.mutation.Message(); !ok {
		v := alert.DefaultMessage
		_c.mutation.SetMessage(v)
	}
	if _, ok := _c.mutation.EventsCount(); !ok {
		v := alert.DefaultEventsCount
		_c.mutation.SetEventsCount(v)
	}
	if _, ok := _c.mutation.StartedAt(); !ok {
		v := alert.DefaultStartedAt()
		_c.mutation.SetStartedAt(v)
	}
	if _, ok := _c.mutation.StoppedAt(); !ok {
		v := alert.DefaultStoppedAt()
		_c.mutation.SetStoppedAt(v)
	}
	if _, ok := _c.mutation.Simulated(); !ok {
		v := alert.DefaultSimulated
		_c.mutation.SetSimulated(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_c *AlertCreate) check() error {
	if _, ok := _c.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Alert.created_at"`)}
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "Alert.updated_at"`)}
	}
	if _, ok := _c.mutation.Scenario(); !ok {
		return &ValidationError{Name: "scenario", err: errors.New(`ent: missing required field "Alert.scenario"`)}
	}
	if _, ok := _c.mutation.Simulated(); !ok {
		return &ValidationError{Name: "simulated", err: errors.New(`ent: missing required field "Alert.simulated"`)}
	}
	return nil
}

func (_c *AlertCreate) sqlSave(ctx context.Context) (*Alert, 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 = int(id)
	_c.mutation.id = &_node.ID
	_c.mutation.done = true
	return _node, nil
}

func (_c *AlertCreate) createSpec() (*Alert, *sqlgraph.CreateSpec) {
	var (
		_node = &Alert{config: _c.config}
		_spec = sqlgraph.NewCreateSpec(alert.Table, sqlgraph.NewFieldSpec(alert.FieldID, field.TypeInt))
	)
	_spec.OnConflict = _c.conflict
	if value, ok := _c.mutation.CreatedAt(); ok {
		_spec.SetField(alert.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := _c.mutation.UpdatedAt(); ok {
		_spec.SetField(alert.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	if value, ok := _c.mutation.Scenario(); ok {
		_spec.SetField(alert.FieldScenario, field.TypeString, value)
		_node.Scenario = value
	}
	if value, ok := _c.mutation.BucketId(); ok {
		_spec.SetField(alert.FieldBucketId, field.TypeString, value)
		_node.BucketId = value
	}
	if value, ok := _c.mutation.Message(); ok {
		_spec.SetField(alert.FieldMessage, field.TypeString, value)
		_node.Message = value
	}
	if value, ok := _c.mutation.EventsCount(); ok {
		_spec.SetField(alert.FieldEventsCount, field.TypeInt32, value)
		_node.EventsCount = value
	}
	if value, ok := _c.mutation.StartedAt(); ok {
		_spec.SetField(alert.FieldStartedAt, field.TypeTime, value)
		_node.StartedAt = value
	}
	if value, ok := _c.mutation.StoppedAt(); ok {
		_spec.SetField(alert.FieldStoppedAt, field.TypeTime, value)
		_node.StoppedAt = value
	}
	if value, ok := _c.mutation.SourceIp(); ok {
		_spec.SetField(alert.FieldSourceIp, field.TypeString, value)
		_node.SourceIp = value
	}
	if value, ok := _c.mutation.SourceRange(); ok {
		_spec.SetField(alert.FieldSourceRange, field.TypeString, value)
		_node.SourceRange = value
	}
	if value, ok := _c.mutation.SourceAsNumber(); ok {
		_spec.SetField(alert.FieldSourceAsNumber, field.TypeString, value)
		_node.SourceAsNumber = value
	}
	if value, ok := _c.mutation.SourceAsName(); ok {
		_spec.SetField(alert.FieldSourceAsName, field.TypeString, value)
		_node.SourceAsName = value
	}
	if value, ok := _c.mutation.SourceCountry(); ok {
		_spec.SetField(alert.FieldSourceCountry, field.TypeString, value)
		_node.SourceCountry = value
	}
	if value, ok := _c.mutation.SourceLatitude(); ok {
		_spec.SetField(alert.FieldSourceLatitude, field.TypeFloat32, value)
		_node.SourceLatitude = value
	}
	if value, ok := _c.mutation.SourceLongitude(); ok {
		_spec.SetField(alert.FieldSourceLongitude, field.TypeFloat32, value)
		_node.SourceLongitude = value
	}
	if value, ok := _c.mutation.SourceScope(); ok {
		_spec.SetField(alert.FieldSourceScope, field.TypeString, value)
		_node.SourceScope = value
	}
	if value, ok := _c.mutation.SourceValue(); ok {
		_spec.SetField(alert.FieldSourceValue, field.TypeString, value)
		_node.SourceValue = value
	}
	if value, ok := _c.mutation.Capacity(); ok {
		_spec.SetField(alert.FieldCapacity, field.TypeInt32, value)
		_node.Capacity = value
	}
	if value, ok := _c.mutation.LeakSpeed(); ok {
		_spec.SetField(alert.FieldLeakSpeed, field.TypeString, value)
		_node.LeakSpeed = value
	}
	if value, ok := _c.mutation.ScenarioVersion(); ok {
		_spec.SetField(alert.FieldScenarioVersion, field.TypeString, value)
		_node.ScenarioVersion = value
	}
	if value, ok := _c.mutation.ScenarioHash(); ok {
		_spec.SetField(alert.FieldScenarioHash, field.TypeString, value)
		_node.ScenarioHash = value
	}
	if value, ok := _c.mutation.Simulated(); ok {
		_spec.SetField(alert.FieldSimulated, field.TypeBool, value)
		_node.Simulated = value
	}
	if value, ok := _c.mutation.UUID(); ok {
		_spec.SetField(alert.FieldUUID, field.TypeString, value)
		_node.UUID = value
	}
	if value, ok := _c.mutation.Remediation(); ok {
		_spec.SetField(alert.FieldRemediation, field.TypeBool, value)
		_node.Remediation = value
	}
	if value, ok := _c.mutation.Kind(); ok {
		_spec.SetField(alert.FieldKind, field.TypeString, value)
		_node.Kind = value
	}
	if nodes := _c.mutation.OwnerIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   alert.OwnerTable,
			Columns: []string{alert.OwnerColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(machine.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.machine_alerts = &nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := _c.mutation.DecisionsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   alert.DecisionsTable,
			Columns: []string{alert.DecisionsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(decision.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := _c.mutation.EventsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   alert.EventsTable,
			Columns: []string{alert.EventsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(event.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := _c.mutation.MetasIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   alert.MetasTable,
			Columns: []string{alert.MetasColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(meta.FieldID, field.TypeInt),
			},
		}
		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.Alert.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.AlertUpsert) {
//			SetCreatedAt(v+v).
//		}).
//		Exec(ctx)
func (_c *AlertCreate) OnConflict(opts ...sql.ConflictOption) *AlertUpsertOne {
	_c.conflict = opts
	return &AlertUpsertOne{
		create: _c,
	}
}

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

type (
	// AlertUpsertOne is the builder for "upsert"-ing
	//  one Alert node.
	AlertUpsertOne struct {
		create *AlertCreate
	}

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

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

// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *AlertUpsert) UpdateUpdatedAt() *AlertUpsert {
	u.SetExcluded(alert.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.Alert.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//		).
//		Exec(ctx)
func (u *AlertUpsertOne) UpdateNewValues() *AlertUpsertOne {
	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(alert.FieldCreatedAt)
		}
		if _, exists := u.create.mutation.Scenario(); exists {
			s.SetIgnore(alert.FieldScenario)
		}
		if _, exists := u.create.mutation.BucketId(); exists {
			s.SetIgnore(alert.FieldBucketId)
		}
		if _, exists := u.create.mutation.Message(); exists {
			s.SetIgnore(alert.FieldMessage)
		}
		if _, exists := u.create.mutation.EventsCount(); exists {
			s.SetIgnore(alert.FieldEventsCount)
		}
		if _, exists := u.create.mutation.StartedAt(); exists {
			s.SetIgnore(alert.FieldStartedAt)
		}
		if _, exists := u.create.mutation.StoppedAt(); exists {
			s.SetIgnore(alert.FieldStoppedAt)
		}
		if _, exists := u.create.mutation.SourceIp(); exists {
			s.SetIgnore(alert.FieldSourceIp)
		}
		if _, exists := u.create.mutation.SourceRange(); exists {
			s.SetIgnore(alert.FieldSourceRange)
		}
		if _, exists := u.create.mutation.SourceAsNumber(); exists {
			s.SetIgnore(alert.FieldSourceAsNumber)
		}
		if _, exists := u.create.mutation.SourceAsName(); exists {
			s.SetIgnore(alert.FieldSourceAsName)
		}
		if _, exists := u.create.mutation.SourceCountry(); exists {
			s.SetIgnore(alert.FieldSourceCountry)
		}
		if _, exists := u.create.mutation.SourceLatitude(); exists {
			s.SetIgnore(alert.FieldSourceLatitude)
		}
		if _, exists := u.create.mutation.SourceLongitude(); exists {
			s.SetIgnore(alert.FieldSourceLongitude)
		}
		if _, exists := u.create.mutation.SourceScope(); exists {
			s.SetIgnore(alert.FieldSourceScope)
		}
		if _, exists := u.create.mutation.SourceValue(); exists {
			s.SetIgnore(alert.FieldSourceValue)
		}
		if _, exists := u.create.mutation.Capacity(); exists {
			s.SetIgnore(alert.FieldCapacity)
		}
		if _, exists := u.create.mutation.LeakSpeed(); exists {
			s.SetIgnore(alert.FieldLeakSpeed)
		}
		if _, exists := u.create.mutation.ScenarioVersion(); exists {
			s.SetIgnore(alert.FieldScenarioVersion)
		}
		if _, exists := u.create.mutation.ScenarioHash(); exists {
			s.SetIgnore(alert.FieldScenarioHash)
		}
		if _, exists := u.create.mutation.Simulated(); exists {
			s.SetIgnore(alert.FieldSimulated)
		}
		if _, exists := u.create.mutation.UUID(); exists {
			s.SetIgnore(alert.FieldUUID)
		}
		if _, exists := u.create.mutation.Remediation(); exists {
			s.SetIgnore(alert.FieldRemediation)
		}
		if _, exists := u.create.mutation.Kind(); exists {
			s.SetIgnore(alert.FieldKind)
		}
	}))
	return u
}

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

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

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

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

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

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

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

// Save creates the Alert entities in the database.
func (_c *AlertCreateBulk) Save(ctx context.Context) ([]*Alert, error) {
	if _c.err != nil {
		return nil, _c.err
	}
	specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
	nodes := make([]*Alert, 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.(*AlertMutation)
				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 = int(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 *AlertCreateBulk) SaveX(ctx context.Context) []*Alert {
	v, err := _c.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

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

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

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

// AlertUpsertBulk is the builder for "upsert"-ing
// a bulk of Alert nodes.
type AlertUpsertBulk struct {
	create *AlertCreateBulk
}

// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
//	client.Alert.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//		).
//		Exec(ctx)
func (u *AlertUpsertBulk) UpdateNewValues() *AlertUpsertBulk {
	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(alert.FieldCreatedAt)
			}
			if _, exists := b.mutation.Scenario(); exists {
				s.SetIgnore(alert.FieldScenario)
			}
			if _, exists := b.mutation.BucketId(); exists {
				s.SetIgnore(alert.FieldBucketId)
			}
			if _, exists := b.mutation.Message(); exists {
				s.SetIgnore(alert.FieldMessage)
			}
			if _, exists := b.mutation.EventsCount(); exists {
				s.SetIgnore(alert.FieldEventsCount)
			}
			if _, exists := b.mutation.StartedAt(); exists {
				s.SetIgnore(alert.FieldStartedAt)
			}
			if _, exists := b.mutation.StoppedAt(); exists {
				s.SetIgnore(alert.FieldStoppedAt)
			}
			if _, exists := b.mutation.SourceIp(); exists {
				s.SetIgnore(alert.FieldSourceIp)
			}
			if _, exists := b.mutation.SourceRange(); exists {
				s.SetIgnore(alert.FieldSourceRange)
			}
			if _, exists := b.mutation.SourceAsNumber(); exists {
				s.SetIgnore(alert.FieldSourceAsNumber)
			}
			if _, exists := b.mutation.SourceAsName(); exists {
				s.SetIgnore(alert.FieldSourceAsName)
			}
			if _, exists := b.mutation.SourceCountry(); exists {
				s.SetIgnore(alert.FieldSourceCountry)
			}
			if _, exists := b.mutation.SourceLatitude(); exists {
				s.SetIgnore(alert.FieldSourceLatitude)
			}
			if _, exists := b.mutation.SourceLongitude(); exists {
				s.SetIgnore(alert.FieldSourceLongitude)
			}
			if _, exists := b.mutation.SourceScope(); exists {
				s.SetIgnore(alert.FieldSourceScope)
			}
			if _, exists := b.mutation.SourceValue(); exists {
				s.SetIgnore(alert.FieldSourceValue)
			}
			if _, exists := b.mutation.Capacity(); exists {
				s.SetIgnore(alert.FieldCapacity)
			}
			if _, exists := b.mutation.LeakSpeed(); exists {
				s.SetIgnore(alert.FieldLeakSpeed)
			}
			if _, exists := b.mutation.ScenarioVersion(); exists {
				s.SetIgnore(alert.FieldScenarioVersion)
			}
			if _, exists := b.mutation.ScenarioHash(); exists {
				s.SetIgnore(alert.FieldScenarioHash)
			}
			if _, exists := b.mutation.Simulated(); exists {
				s.SetIgnore(alert.FieldSimulated)
			}
			if _, exists := b.mutation.UUID(); exists {
				s.SetIgnore(alert.FieldUUID)
			}
			if _, exists := b.mutation.Remediation(); exists {
				s.SetIgnore(alert.FieldRemediation)
			}
			if _, exists := b.mutation.Kind(); exists {
				s.SetIgnore(alert.FieldKind)
			}
		}
	}))
	return u
}

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

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

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

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

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

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