// Code generated by ent, DO NOT EDIT.

package models

import (
	"context"
	"errors"
	"fmt"
	"juwan-backend/app/user_verifications/rpc/internal/models/schema"
	"juwan-backend/app/user_verifications/rpc/internal/models/userverifications"
	"time"

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

// UserVerificationsCreate is the builder for creating a UserVerifications entity.
type UserVerificationsCreate struct {
	config
	mutation *UserVerificationsMutation
	hooks    []Hook
}

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

// SetRole sets the "role" field.
func (_c *UserVerificationsCreate) SetRole(v string) *UserVerificationsCreate {
	_c.mutation.SetRole(v)
	return _c
}

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

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

// SetMaterials sets the "materials" field.
func (_c *UserVerificationsCreate) SetMaterials(v schema.MaterialStruct) *UserVerificationsCreate {
	_c.mutation.SetMaterials(v)
	return _c
}

// SetRejectReason sets the "reject_reason" field.
func (_c *UserVerificationsCreate) SetRejectReason(v string) *UserVerificationsCreate {
	_c.mutation.SetRejectReason(v)
	return _c
}

// SetNillableRejectReason sets the "reject_reason" field if the given value is not nil.
func (_c *UserVerificationsCreate) SetNillableRejectReason(v *string) *UserVerificationsCreate {
	if v != nil {
		_c.SetRejectReason(*v)
	}
	return _c
}

// SetReviewedBy sets the "reviewed_by" field.
func (_c *UserVerificationsCreate) SetReviewedBy(v int64) *UserVerificationsCreate {
	_c.mutation.SetReviewedBy(v)
	return _c
}

// SetNillableReviewedBy sets the "reviewed_by" field if the given value is not nil.
func (_c *UserVerificationsCreate) SetNillableReviewedBy(v *int64) *UserVerificationsCreate {
	if v != nil {
		_c.SetReviewedBy(*v)
	}
	return _c
}

// SetReviewedAt sets the "reviewed_at" field.
func (_c *UserVerificationsCreate) SetReviewedAt(v time.Time) *UserVerificationsCreate {
	_c.mutation.SetReviewedAt(v)
	return _c
}

// SetNillableReviewedAt sets the "reviewed_at" field if the given value is not nil.
func (_c *UserVerificationsCreate) SetNillableReviewedAt(v *time.Time) *UserVerificationsCreate {
	if v != nil {
		_c.SetReviewedAt(*v)
	}
	return _c
}

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

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

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

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

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

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (_c *UserVerificationsCreate) check() error {
	if _, ok := _c.mutation.UserID(); !ok {
		return &ValidationError{Name: "user_id", err: errors.New(`models: missing required field "UserVerifications.user_id"`)}
	}
	if _, ok := _c.mutation.Role(); !ok {
		return &ValidationError{Name: "role", err: errors.New(`models: missing required field "UserVerifications.role"`)}
	}
	if _, ok := _c.mutation.Status(); !ok {
		return &ValidationError{Name: "status", err: errors.New(`models: missing required field "UserVerifications.status"`)}
	}
	if _, ok := _c.mutation.Materials(); !ok {
		return &ValidationError{Name: "materials", err: errors.New(`models: missing required field "UserVerifications.materials"`)}
	}
	return nil
}

func (_c *UserVerificationsCreate) sqlSave(ctx context.Context) (*UserVerifications, 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 *UserVerificationsCreate) createSpec() (*UserVerifications, *sqlgraph.CreateSpec) {
	var (
		_node = &UserVerifications{config: _c.config}
		_spec = sqlgraph.NewCreateSpec(userverifications.Table, sqlgraph.NewFieldSpec(userverifications.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(userverifications.FieldUserID, field.TypeInt64, value)
		_node.UserID = value
	}
	if value, ok := _c.mutation.Role(); ok {
		_spec.SetField(userverifications.FieldRole, field.TypeString, value)
		_node.Role = value
	}
	if value, ok := _c.mutation.Status(); ok {
		_spec.SetField(userverifications.FieldStatus, field.TypeString, value)
		_node.Status = value
	}
	if value, ok := _c.mutation.Materials(); ok {
		_spec.SetField(userverifications.FieldMaterials, field.TypeJSON, value)
		_node.Materials = value
	}
	if value, ok := _c.mutation.RejectReason(); ok {
		_spec.SetField(userverifications.FieldRejectReason, field.TypeString, value)
		_node.RejectReason = &value
	}
	if value, ok := _c.mutation.ReviewedBy(); ok {
		_spec.SetField(userverifications.FieldReviewedBy, field.TypeInt64, value)
		_node.ReviewedBy = &value
	}
	if value, ok := _c.mutation.ReviewedAt(); ok {
		_spec.SetField(userverifications.FieldReviewedAt, field.TypeTime, value)
		_node.ReviewedAt = &value
	}
	if value, ok := _c.mutation.CreatedAt(); ok {
		_spec.SetField(userverifications.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := _c.mutation.UpdatedAt(); ok {
		_spec.SetField(userverifications.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	return _node, _spec
}

// UserVerificationsCreateBulk is the builder for creating many UserVerifications entities in bulk.
type UserVerificationsCreateBulk struct {
	config
	err      error
	builders []*UserVerificationsCreate
}

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

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

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