// Code generated by ent, DO NOT EDIT.

package models

import (
	"context"
	"errors"
	"fmt"
	"juwan-backend/app/notification/rpc/internal/models/notifications"
	"time"

	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
)

// NotificationsCreate is the builder for creating a Notifications entity.
type NotificationsCreate struct {
	config
	mutation *NotificationsMutation
	hooks    []Hook
}

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

// SetType sets the "type" field.
func (_c *NotificationsCreate) SetType(v string) *NotificationsCreate {
	_c.mutation.SetType(v)
	return _c
}

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

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

// SetRead sets the "read" field.
func (_c *NotificationsCreate) SetRead(v bool) *NotificationsCreate {
	_c.mutation.SetRead(v)
	return _c
}

// SetNillableRead sets the "read" field if the given value is not nil.
func (_c *NotificationsCreate) SetNillableRead(v *bool) *NotificationsCreate {
	if v != nil {
		_c.SetRead(*v)
	}
	return _c
}

// SetLink sets the "link" field.
func (_c *NotificationsCreate) SetLink(v string) *NotificationsCreate {
	_c.mutation.SetLink(v)
	return _c
}

// SetNillableLink sets the "link" field if the given value is not nil.
func (_c *NotificationsCreate) SetNillableLink(v *string) *NotificationsCreate {
	if v != nil {
		_c.SetLink(*v)
	}
	return _c
}

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

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

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

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

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

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (_c *NotificationsCreate) check() error {
	if _, ok := _c.mutation.UserID(); !ok {
		return &ValidationError{Name: "user_id", err: errors.New(`models: missing required field "Notifications.user_id"`)}
	}
	if _, ok := _c.mutation.GetType(); !ok {
		return &ValidationError{Name: "type", err: errors.New(`models: missing required field "Notifications.type"`)}
	}
	if v, ok := _c.mutation.GetType(); ok {
		if err := notifications.TypeValidator(v); err != nil {
			return &ValidationError{Name: "type", err: fmt.Errorf(`models: validator failed for field "Notifications.type": %w`, err)}
		}
	}
	if _, ok := _c.mutation.Title(); !ok {
		return &ValidationError{Name: "title", err: errors.New(`models: missing required field "Notifications.title"`)}
	}
	if v, ok := _c.mutation.Title(); ok {
		if err := notifications.TitleValidator(v); err != nil {
			return &ValidationError{Name: "title", err: fmt.Errorf(`models: validator failed for field "Notifications.title": %w`, err)}
		}
	}
	if _, ok := _c.mutation.Content(); !ok {
		return &ValidationError{Name: "content", err: errors.New(`models: missing required field "Notifications.content"`)}
	}
	if _, ok := _c.mutation.Read(); !ok {
		return &ValidationError{Name: "read", err: errors.New(`models: missing required field "Notifications.read"`)}
	}
	if v, ok := _c.mutation.Link(); ok {
		if err := notifications.LinkValidator(v); err != nil {
			return &ValidationError{Name: "link", err: fmt.Errorf(`models: validator failed for field "Notifications.link": %w`, err)}
		}
	}
	if _, ok := _c.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`models: missing required field "Notifications.created_at"`)}
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`models: missing required field "Notifications.updated_at"`)}
	}
	return nil
}

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

func (_c *NotificationsCreate) createSpec() (*Notifications, *sqlgraph.CreateSpec) {
	var (
		_node = &Notifications{config: _c.config}
		_spec = sqlgraph.NewCreateSpec(notifications.Table, sqlgraph.NewFieldSpec(notifications.FieldID, field.TypeInt64))
	)
	if id, ok := _c.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := _c.mutation.UserID(); ok {
		_spec.SetField(notifications.FieldUserID, field.TypeInt64, value)
		_node.UserID = value
	}
	if value, ok := _c.mutation.GetType(); ok {
		_spec.SetField(notifications.FieldType, field.TypeString, value)
		_node.Type = value
	}
	if value, ok := _c.mutation.Title(); ok {
		_spec.SetField(notifications.FieldTitle, field.TypeString, value)
		_node.Title = value
	}
	if value, ok := _c.mutation.Content(); ok {
		_spec.SetField(notifications.FieldContent, field.TypeString, value)
		_node.Content = value
	}
	if value, ok := _c.mutation.Read(); ok {
		_spec.SetField(notifications.FieldRead, field.TypeBool, value)
		_node.Read = value
	}
	if value, ok := _c.mutation.Link(); ok {
		_spec.SetField(notifications.FieldLink, field.TypeString, value)
		_node.Link = &value
	}
	if value, ok := _c.mutation.CreatedAt(); ok {
		_spec.SetField(notifications.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := _c.mutation.UpdatedAt(); ok {
		_spec.SetField(notifications.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	return _node, _spec
}

// NotificationsCreateBulk is the builder for creating many Notifications entities in bulk.
type NotificationsCreateBulk struct {
	config
	err      error
	builders []*NotificationsCreate
}

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

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

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