// Code generated by ent, DO NOT EDIT.

package models

import (
	"context"
	"errors"
	"fmt"
	"juwan-backend/app/users/rpc/internal/models/schema"
	"juwan-backend/app/users/rpc/internal/models/users"
	"juwan-backend/pkg/types"
	"time"

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

// UsersCreate is the builder for creating a Users entity.
type UsersCreate struct {
	config
	mutation *UsersMutation
	hooks    []Hook
}

// SetUsername sets the "username" field.
func (_c *UsersCreate) SetUsername(v string) *UsersCreate {
	_c.mutation.SetUsername(v)
	return _c
}

// SetPasswordHash sets the "password_hash" field.
func (_c *UsersCreate) SetPasswordHash(v string) *UsersCreate {
	_c.mutation.SetPasswordHash(v)
	return _c
}

// SetEmail sets the "email" field.
func (_c *UsersCreate) SetEmail(v string) *UsersCreate {
	_c.mutation.SetEmail(v)
	return _c
}

// SetPhone sets the "phone" field.
func (_c *UsersCreate) SetPhone(v string) *UsersCreate {
	_c.mutation.SetPhone(v)
	return _c
}

// SetNickname sets the "nickname" field.
func (_c *UsersCreate) SetNickname(v string) *UsersCreate {
	_c.mutation.SetNickname(v)
	return _c
}

// SetNillableNickname sets the "nickname" field if the given value is not nil.
func (_c *UsersCreate) SetNillableNickname(v *string) *UsersCreate {
	if v != nil {
		_c.SetNickname(*v)
	}
	return _c
}

// SetAvatar sets the "avatar" field.
func (_c *UsersCreate) SetAvatar(v string) *UsersCreate {
	_c.mutation.SetAvatar(v)
	return _c
}

// SetNillableAvatar sets the "avatar" field if the given value is not nil.
func (_c *UsersCreate) SetNillableAvatar(v *string) *UsersCreate {
	if v != nil {
		_c.SetAvatar(*v)
	}
	return _c
}

// SetBio sets the "bio" field.
func (_c *UsersCreate) SetBio(v string) *UsersCreate {
	_c.mutation.SetBio(v)
	return _c
}

// SetNillableBio sets the "bio" field if the given value is not nil.
func (_c *UsersCreate) SetNillableBio(v *string) *UsersCreate {
	if v != nil {
		_c.SetBio(*v)
	}
	return _c
}

// SetCurrentRole sets the "current_role" field.
func (_c *UsersCreate) SetCurrentRole(v string) *UsersCreate {
	_c.mutation.SetCurrentRole(v)
	return _c
}

// SetNillableCurrentRole sets the "current_role" field if the given value is not nil.
func (_c *UsersCreate) SetNillableCurrentRole(v *string) *UsersCreate {
	if v != nil {
		_c.SetCurrentRole(*v)
	}
	return _c
}

// SetVerificationStatus sets the "verificationStatus" field.
func (_c *UsersCreate) SetVerificationStatus(v schema.VerificationStatusStruct) *UsersCreate {
	_c.mutation.SetVerificationStatus(v)
	return _c
}

// SetNillableVerificationStatus sets the "verificationStatus" field if the given value is not nil.
func (_c *UsersCreate) SetNillableVerificationStatus(v *schema.VerificationStatusStruct) *UsersCreate {
	if v != nil {
		_c.SetVerificationStatus(*v)
	}
	return _c
}

// SetIsAdmin sets the "is_admin" field.
func (_c *UsersCreate) SetIsAdmin(v bool) *UsersCreate {
	_c.mutation.SetIsAdmin(v)
	return _c
}

// SetNillableIsAdmin sets the "is_admin" field if the given value is not nil.
func (_c *UsersCreate) SetNillableIsAdmin(v *bool) *UsersCreate {
	if v != nil {
		_c.SetIsAdmin(*v)
	}
	return _c
}

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

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

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

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

// SetDeletedAt sets the "deleted_at" field.
func (_c *UsersCreate) SetDeletedAt(v time.Time) *UsersCreate {
	_c.mutation.SetDeletedAt(v)
	return _c
}

// SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
func (_c *UsersCreate) SetNillableDeletedAt(v *time.Time) *UsersCreate {
	if v != nil {
		_c.SetDeletedAt(*v)
	}
	return _c
}

// SetVerifiedRoles sets the "verified_roles" field.
func (_c *UsersCreate) SetVerifiedRoles(v types.TextArray) *UsersCreate {
	_c.mutation.SetVerifiedRoles(v)
	return _c
}

// SetNillableVerifiedRoles sets the "verified_roles" field if the given value is not nil.
func (_c *UsersCreate) SetNillableVerifiedRoles(v *types.TextArray) *UsersCreate {
	if v != nil {
		_c.SetVerifiedRoles(*v)
	}
	return _c
}

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

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

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

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_c *UsersCreate) 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 *UsersCreate) defaults() {
	if _, ok := _c.mutation.Nickname(); !ok {
		v := users.DefaultNickname
		_c.mutation.SetNickname(v)
	}
	if _, ok := _c.mutation.Avatar(); !ok {
		v := users.DefaultAvatar
		_c.mutation.SetAvatar(v)
	}
	if _, ok := _c.mutation.Bio(); !ok {
		v := users.DefaultBio
		_c.mutation.SetBio(v)
	}
	if _, ok := _c.mutation.CurrentRole(); !ok {
		v := users.DefaultCurrentRole
		_c.mutation.SetCurrentRole(v)
	}
	if _, ok := _c.mutation.IsAdmin(); !ok {
		v := users.DefaultIsAdmin
		_c.mutation.SetIsAdmin(v)
	}
	if _, ok := _c.mutation.CreatedAt(); !ok {
		v := users.DefaultCreatedAt()
		_c.mutation.SetCreatedAt(v)
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		v := users.DefaultUpdatedAt()
		_c.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_c *UsersCreate) check() error {
	if _, ok := _c.mutation.Username(); !ok {
		return &ValidationError{Name: "username", err: errors.New(`models: missing required field "Users.username"`)}
	}
	if _, ok := _c.mutation.PasswordHash(); !ok {
		return &ValidationError{Name: "password_hash", err: errors.New(`models: missing required field "Users.password_hash"`)}
	}
	if _, ok := _c.mutation.Email(); !ok {
		return &ValidationError{Name: "email", err: errors.New(`models: missing required field "Users.email"`)}
	}
	if _, ok := _c.mutation.Phone(); !ok {
		return &ValidationError{Name: "phone", err: errors.New(`models: missing required field "Users.phone"`)}
	}
	if _, ok := _c.mutation.Nickname(); !ok {
		return &ValidationError{Name: "nickname", err: errors.New(`models: missing required field "Users.nickname"`)}
	}
	if _, ok := _c.mutation.Avatar(); !ok {
		return &ValidationError{Name: "avatar", err: errors.New(`models: missing required field "Users.avatar"`)}
	}
	if _, ok := _c.mutation.Bio(); !ok {
		return &ValidationError{Name: "bio", err: errors.New(`models: missing required field "Users.bio"`)}
	}
	if _, ok := _c.mutation.CurrentRole(); !ok {
		return &ValidationError{Name: "current_role", err: errors.New(`models: missing required field "Users.current_role"`)}
	}
	if v, ok := _c.mutation.CurrentRole(); ok {
		if err := users.CurrentRoleValidator(v); err != nil {
			return &ValidationError{Name: "current_role", err: fmt.Errorf(`models: validator failed for field "Users.current_role": %w`, err)}
		}
	}
	if _, ok := _c.mutation.IsAdmin(); !ok {
		return &ValidationError{Name: "is_admin", err: errors.New(`models: missing required field "Users.is_admin"`)}
	}
	if _, ok := _c.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`models: missing required field "Users.created_at"`)}
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`models: missing required field "Users.updated_at"`)}
	}
	return nil
}

func (_c *UsersCreate) sqlSave(ctx context.Context) (*Users, 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 *UsersCreate) createSpec() (*Users, *sqlgraph.CreateSpec) {
	var (
		_node = &Users{config: _c.config}
		_spec = sqlgraph.NewCreateSpec(users.Table, sqlgraph.NewFieldSpec(users.FieldID, field.TypeInt64))
	)
	if id, ok := _c.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := _c.mutation.Username(); ok {
		_spec.SetField(users.FieldUsername, field.TypeString, value)
		_node.Username = value
	}
	if value, ok := _c.mutation.PasswordHash(); ok {
		_spec.SetField(users.FieldPasswordHash, field.TypeString, value)
		_node.PasswordHash = value
	}
	if value, ok := _c.mutation.Email(); ok {
		_spec.SetField(users.FieldEmail, field.TypeString, value)
		_node.Email = value
	}
	if value, ok := _c.mutation.Phone(); ok {
		_spec.SetField(users.FieldPhone, field.TypeString, value)
		_node.Phone = value
	}
	if value, ok := _c.mutation.Nickname(); ok {
		_spec.SetField(users.FieldNickname, field.TypeString, value)
		_node.Nickname = value
	}
	if value, ok := _c.mutation.Avatar(); ok {
		_spec.SetField(users.FieldAvatar, field.TypeString, value)
		_node.Avatar = value
	}
	if value, ok := _c.mutation.Bio(); ok {
		_spec.SetField(users.FieldBio, field.TypeString, value)
		_node.Bio = value
	}
	if value, ok := _c.mutation.CurrentRole(); ok {
		_spec.SetField(users.FieldCurrentRole, field.TypeString, value)
		_node.CurrentRole = value
	}
	if value, ok := _c.mutation.VerificationStatus(); ok {
		_spec.SetField(users.FieldVerificationStatus, field.TypeJSON, value)
		_node.VerificationStatus = value
	}
	if value, ok := _c.mutation.IsAdmin(); ok {
		_spec.SetField(users.FieldIsAdmin, field.TypeBool, value)
		_node.IsAdmin = value
	}
	if value, ok := _c.mutation.CreatedAt(); ok {
		_spec.SetField(users.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := _c.mutation.UpdatedAt(); ok {
		_spec.SetField(users.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	if value, ok := _c.mutation.DeletedAt(); ok {
		_spec.SetField(users.FieldDeletedAt, field.TypeTime, value)
		_node.DeletedAt = value
	}
	if value, ok := _c.mutation.VerifiedRoles(); ok {
		_spec.SetField(users.FieldVerifiedRoles, field.TypeOther, value)
		_node.VerifiedRoles = value
	}
	return _node, _spec
}

// UsersCreateBulk is the builder for creating many Users entities in bulk.
type UsersCreateBulk struct {
	config
	err      error
	builders []*UsersCreate
}

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

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

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