// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"galileo/ent/groupmember"
	"time"

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

// GroupMemberCreate is the builder for creating a GroupMember entity.
type GroupMemberCreate struct {
	config
	mutation *GroupMemberMutation
	hooks    []Hook
}

// SetGroupID sets the "group_id" field.
func (gmc *GroupMemberCreate) SetGroupID(i int64) *GroupMemberCreate {
	gmc.mutation.SetGroupID(i)
	return gmc
}

// SetUserID sets the "user_id" field.
func (gmc *GroupMemberCreate) SetUserID(u uint32) *GroupMemberCreate {
	gmc.mutation.SetUserID(u)
	return gmc
}

// SetRole sets the "role" field.
func (gmc *GroupMemberCreate) SetRole(u uint8) *GroupMemberCreate {
	gmc.mutation.SetRole(u)
	return gmc
}

// SetNillableRole sets the "role" field if the given value is not nil.
func (gmc *GroupMemberCreate) SetNillableRole(u *uint8) *GroupMemberCreate {
	if u != nil {
		gmc.SetRole(*u)
	}
	return gmc
}

// SetCreatedAt sets the "created_at" field.
func (gmc *GroupMemberCreate) SetCreatedAt(t time.Time) *GroupMemberCreate {
	gmc.mutation.SetCreatedAt(t)
	return gmc
}

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

// SetCreatedBy sets the "created_by" field.
func (gmc *GroupMemberCreate) SetCreatedBy(u uint32) *GroupMemberCreate {
	gmc.mutation.SetCreatedBy(u)
	return gmc
}

// SetNillableCreatedBy sets the "created_by" field if the given value is not nil.
func (gmc *GroupMemberCreate) SetNillableCreatedBy(u *uint32) *GroupMemberCreate {
	if u != nil {
		gmc.SetCreatedBy(*u)
	}
	return gmc
}

// SetDeletedAt sets the "deleted_at" field.
func (gmc *GroupMemberCreate) SetDeletedAt(t time.Time) *GroupMemberCreate {
	gmc.mutation.SetDeletedAt(t)
	return gmc
}

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

// SetDeletedBy sets the "deleted_by" field.
func (gmc *GroupMemberCreate) SetDeletedBy(u uint32) *GroupMemberCreate {
	gmc.mutation.SetDeletedBy(u)
	return gmc
}

// SetNillableDeletedBy sets the "deleted_by" field if the given value is not nil.
func (gmc *GroupMemberCreate) SetNillableDeletedBy(u *uint32) *GroupMemberCreate {
	if u != nil {
		gmc.SetDeletedBy(*u)
	}
	return gmc
}

// SetID sets the "id" field.
func (gmc *GroupMemberCreate) SetID(i int64) *GroupMemberCreate {
	gmc.mutation.SetID(i)
	return gmc
}

// Mutation returns the GroupMemberMutation object of the builder.
func (gmc *GroupMemberCreate) Mutation() *GroupMemberMutation {
	return gmc.mutation
}

// Save creates the GroupMember in the database.
func (gmc *GroupMemberCreate) Save(ctx context.Context) (*GroupMember, error) {
	gmc.defaults()
	return withHooks[*GroupMember, GroupMemberMutation](ctx, gmc.sqlSave, gmc.mutation, gmc.hooks)
}

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

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

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

// defaults sets the default values of the builder before save.
func (gmc *GroupMemberCreate) defaults() {
	if _, ok := gmc.mutation.Role(); !ok {
		v := groupmember.DefaultRole
		gmc.mutation.SetRole(v)
	}
	if _, ok := gmc.mutation.CreatedAt(); !ok {
		v := groupmember.DefaultCreatedAt()
		gmc.mutation.SetCreatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (gmc *GroupMemberCreate) check() error {
	if _, ok := gmc.mutation.GroupID(); !ok {
		return &ValidationError{Name: "group_id", err: errors.New(`ent: missing required field "GroupMember.group_id"`)}
	}
	if _, ok := gmc.mutation.UserID(); !ok {
		return &ValidationError{Name: "user_id", err: errors.New(`ent: missing required field "GroupMember.user_id"`)}
	}
	if _, ok := gmc.mutation.Role(); !ok {
		return &ValidationError{Name: "role", err: errors.New(`ent: missing required field "GroupMember.role"`)}
	}
	if _, ok := gmc.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "GroupMember.created_at"`)}
	}
	return nil
}

func (gmc *GroupMemberCreate) sqlSave(ctx context.Context) (*GroupMember, error) {
	if err := gmc.check(); err != nil {
		return nil, err
	}
	_node, _spec := gmc.createSpec()
	if err := sqlgraph.CreateNode(ctx, gmc.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)
	}
	gmc.mutation.id = &_node.ID
	gmc.mutation.done = true
	return _node, nil
}

func (gmc *GroupMemberCreate) createSpec() (*GroupMember, *sqlgraph.CreateSpec) {
	var (
		_node = &GroupMember{config: gmc.config}
		_spec = sqlgraph.NewCreateSpec(groupmember.Table, sqlgraph.NewFieldSpec(groupmember.FieldID, field.TypeInt64))
	)
	if id, ok := gmc.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := gmc.mutation.GroupID(); ok {
		_spec.SetField(groupmember.FieldGroupID, field.TypeInt64, value)
		_node.GroupID = value
	}
	if value, ok := gmc.mutation.UserID(); ok {
		_spec.SetField(groupmember.FieldUserID, field.TypeUint32, value)
		_node.UserID = value
	}
	if value, ok := gmc.mutation.Role(); ok {
		_spec.SetField(groupmember.FieldRole, field.TypeUint8, value)
		_node.Role = value
	}
	if value, ok := gmc.mutation.CreatedAt(); ok {
		_spec.SetField(groupmember.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := gmc.mutation.CreatedBy(); ok {
		_spec.SetField(groupmember.FieldCreatedBy, field.TypeUint32, value)
		_node.CreatedBy = value
	}
	if value, ok := gmc.mutation.DeletedAt(); ok {
		_spec.SetField(groupmember.FieldDeletedAt, field.TypeTime, value)
		_node.DeletedAt = value
	}
	if value, ok := gmc.mutation.DeletedBy(); ok {
		_spec.SetField(groupmember.FieldDeletedBy, field.TypeUint32, value)
		_node.DeletedBy = value
	}
	return _node, _spec
}

// GroupMemberCreateBulk is the builder for creating many GroupMember entities in bulk.
type GroupMemberCreateBulk struct {
	config
	builders []*GroupMemberCreate
}

// Save creates the GroupMember entities in the database.
func (gmcb *GroupMemberCreateBulk) Save(ctx context.Context) ([]*GroupMember, error) {
	specs := make([]*sqlgraph.CreateSpec, len(gmcb.builders))
	nodes := make([]*GroupMember, len(gmcb.builders))
	mutators := make([]Mutator, len(gmcb.builders))
	for i := range gmcb.builders {
		func(i int, root context.Context) {
			builder := gmcb.builders[i]
			builder.defaults()
			var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
				mutation, ok := m.(*GroupMemberMutation)
				if !ok {
					return nil, fmt.Errorf("unexpected mutation type %T", m)
				}
				if err := builder.check(); err != nil {
					return nil, err
				}
				builder.mutation = mutation
				nodes[i], specs[i] = builder.createSpec()
				var err error
				if i < len(mutators)-1 {
					_, err = mutators[i+1].Mutate(root, gmcb.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, gmcb.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, gmcb.builders[0].mutation); err != nil {
			return nil, err
		}
	}
	return nodes, nil
}

// SaveX is like Save, but panics if an error occurs.
func (gmcb *GroupMemberCreateBulk) SaveX(ctx context.Context) []*GroupMember {
	v, err := gmcb.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

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

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