// Code generated by ent, DO NOT EDIT.

package ent

import (
	"PasswordManager/ent/additionalfield"
	"PasswordManager/ent/password"
	"PasswordManager/ent/url"
	"PasswordManager/ent/vault"
	"context"
	"errors"
	"fmt"

	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/google/uuid"
)

// PasswordCreate is the builder for creating a Password entity.
type PasswordCreate struct {
	config
	mutation *PasswordMutation
	hooks    []Hook
}

// SetName sets the "name" field.
func (pc *PasswordCreate) SetName(b []byte) *PasswordCreate {
	pc.mutation.SetName(b)
	return pc
}

// SetNameIv sets the "nameIv" field.
func (pc *PasswordCreate) SetNameIv(b []byte) *PasswordCreate {
	pc.mutation.SetNameIv(b)
	return pc
}

// SetUsername sets the "username" field.
func (pc *PasswordCreate) SetUsername(b []byte) *PasswordCreate {
	pc.mutation.SetUsername(b)
	return pc
}

// SetUsernameIv sets the "usernameIv" field.
func (pc *PasswordCreate) SetUsernameIv(b []byte) *PasswordCreate {
	pc.mutation.SetUsernameIv(b)
	return pc
}

// SetPassword sets the "password" field.
func (pc *PasswordCreate) SetPassword(b []byte) *PasswordCreate {
	pc.mutation.SetPassword(b)
	return pc
}

// SetPasswordIv sets the "passwordIv" field.
func (pc *PasswordCreate) SetPasswordIv(b []byte) *PasswordCreate {
	pc.mutation.SetPasswordIv(b)
	return pc
}

// SetColour sets the "colour" field.
func (pc *PasswordCreate) SetColour(b []byte) *PasswordCreate {
	pc.mutation.SetColour(b)
	return pc
}

// SetColourIv sets the "colourIv" field.
func (pc *PasswordCreate) SetColourIv(b []byte) *PasswordCreate {
	pc.mutation.SetColourIv(b)
	return pc
}

// SetID sets the "id" field.
func (pc *PasswordCreate) SetID(u uuid.UUID) *PasswordCreate {
	pc.mutation.SetID(u)
	return pc
}

// SetNillableID sets the "id" field if the given value is not nil.
func (pc *PasswordCreate) SetNillableID(u *uuid.UUID) *PasswordCreate {
	if u != nil {
		pc.SetID(*u)
	}
	return pc
}

// AddAdditionalFieldIDs adds the "additionalFields" edge to the AdditionalField entity by IDs.
func (pc *PasswordCreate) AddAdditionalFieldIDs(ids ...uuid.UUID) *PasswordCreate {
	pc.mutation.AddAdditionalFieldIDs(ids...)
	return pc
}

// AddAdditionalFields adds the "additionalFields" edges to the AdditionalField entity.
func (pc *PasswordCreate) AddAdditionalFields(a ...*AdditionalField) *PasswordCreate {
	ids := make([]uuid.UUID, len(a))
	for i := range a {
		ids[i] = a[i].ID
	}
	return pc.AddAdditionalFieldIDs(ids...)
}

// AddURLIDs adds the "urls" edge to the Url entity by IDs.
func (pc *PasswordCreate) AddURLIDs(ids ...uuid.UUID) *PasswordCreate {
	pc.mutation.AddURLIDs(ids...)
	return pc
}

// AddUrls adds the "urls" edges to the Url entity.
func (pc *PasswordCreate) AddUrls(u ...*Url) *PasswordCreate {
	ids := make([]uuid.UUID, len(u))
	for i := range u {
		ids[i] = u[i].ID
	}
	return pc.AddURLIDs(ids...)
}

// SetVaultID sets the "vault" edge to the Vault entity by ID.
func (pc *PasswordCreate) SetVaultID(id uuid.UUID) *PasswordCreate {
	pc.mutation.SetVaultID(id)
	return pc
}

// SetNillableVaultID sets the "vault" edge to the Vault entity by ID if the given value is not nil.
func (pc *PasswordCreate) SetNillableVaultID(id *uuid.UUID) *PasswordCreate {
	if id != nil {
		pc = pc.SetVaultID(*id)
	}
	return pc
}

// SetVault sets the "vault" edge to the Vault entity.
func (pc *PasswordCreate) SetVault(v *Vault) *PasswordCreate {
	return pc.SetVaultID(v.ID)
}

// Mutation returns the PasswordMutation object of the builder.
func (pc *PasswordCreate) Mutation() *PasswordMutation {
	return pc.mutation
}

// Save creates the Password in the database.
func (pc *PasswordCreate) Save(ctx context.Context) (*Password, error) {
	pc.defaults()
	return withHooks[*Password, PasswordMutation](ctx, pc.sqlSave, pc.mutation, pc.hooks)
}

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

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

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

// defaults sets the default values of the builder before save.
func (pc *PasswordCreate) defaults() {
	if _, ok := pc.mutation.ID(); !ok {
		v := password.DefaultID()
		pc.mutation.SetID(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (pc *PasswordCreate) check() error {
	if _, ok := pc.mutation.Name(); !ok {
		return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "Password.name"`)}
	}
	if v, ok := pc.mutation.Name(); ok {
		if err := password.NameValidator(v); err != nil {
			return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "Password.name": %w`, err)}
		}
	}
	if _, ok := pc.mutation.NameIv(); !ok {
		return &ValidationError{Name: "nameIv", err: errors.New(`ent: missing required field "Password.nameIv"`)}
	}
	if v, ok := pc.mutation.NameIv(); ok {
		if err := password.NameIvValidator(v); err != nil {
			return &ValidationError{Name: "nameIv", err: fmt.Errorf(`ent: validator failed for field "Password.nameIv": %w`, err)}
		}
	}
	if _, ok := pc.mutation.Username(); !ok {
		return &ValidationError{Name: "username", err: errors.New(`ent: missing required field "Password.username"`)}
	}
	if v, ok := pc.mutation.Username(); ok {
		if err := password.UsernameValidator(v); err != nil {
			return &ValidationError{Name: "username", err: fmt.Errorf(`ent: validator failed for field "Password.username": %w`, err)}
		}
	}
	if _, ok := pc.mutation.UsernameIv(); !ok {
		return &ValidationError{Name: "usernameIv", err: errors.New(`ent: missing required field "Password.usernameIv"`)}
	}
	if v, ok := pc.mutation.UsernameIv(); ok {
		if err := password.UsernameIvValidator(v); err != nil {
			return &ValidationError{Name: "usernameIv", err: fmt.Errorf(`ent: validator failed for field "Password.usernameIv": %w`, err)}
		}
	}
	if _, ok := pc.mutation.Password(); !ok {
		return &ValidationError{Name: "password", err: errors.New(`ent: missing required field "Password.password"`)}
	}
	if v, ok := pc.mutation.Password(); ok {
		if err := password.PasswordValidator(v); err != nil {
			return &ValidationError{Name: "password", err: fmt.Errorf(`ent: validator failed for field "Password.password": %w`, err)}
		}
	}
	if _, ok := pc.mutation.PasswordIv(); !ok {
		return &ValidationError{Name: "passwordIv", err: errors.New(`ent: missing required field "Password.passwordIv"`)}
	}
	if v, ok := pc.mutation.PasswordIv(); ok {
		if err := password.PasswordIvValidator(v); err != nil {
			return &ValidationError{Name: "passwordIv", err: fmt.Errorf(`ent: validator failed for field "Password.passwordIv": %w`, err)}
		}
	}
	if _, ok := pc.mutation.Colour(); !ok {
		return &ValidationError{Name: "colour", err: errors.New(`ent: missing required field "Password.colour"`)}
	}
	if v, ok := pc.mutation.Colour(); ok {
		if err := password.ColourValidator(v); err != nil {
			return &ValidationError{Name: "colour", err: fmt.Errorf(`ent: validator failed for field "Password.colour": %w`, err)}
		}
	}
	if _, ok := pc.mutation.ColourIv(); !ok {
		return &ValidationError{Name: "colourIv", err: errors.New(`ent: missing required field "Password.colourIv"`)}
	}
	if v, ok := pc.mutation.ColourIv(); ok {
		if err := password.ColourIvValidator(v); err != nil {
			return &ValidationError{Name: "colourIv", err: fmt.Errorf(`ent: validator failed for field "Password.colourIv": %w`, err)}
		}
	}
	return nil
}

func (pc *PasswordCreate) sqlSave(ctx context.Context) (*Password, error) {
	if err := pc.check(); err != nil {
		return nil, err
	}
	_node, _spec := pc.createSpec()
	if err := sqlgraph.CreateNode(ctx, pc.driver, _spec); err != nil {
		if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	if _spec.ID.Value != nil {
		if id, ok := _spec.ID.Value.(*uuid.UUID); ok {
			_node.ID = *id
		} else if err := _node.ID.Scan(_spec.ID.Value); err != nil {
			return nil, err
		}
	}
	pc.mutation.id = &_node.ID
	pc.mutation.done = true
	return _node, nil
}

func (pc *PasswordCreate) createSpec() (*Password, *sqlgraph.CreateSpec) {
	var (
		_node = &Password{config: pc.config}
		_spec = sqlgraph.NewCreateSpec(password.Table, sqlgraph.NewFieldSpec(password.FieldID, field.TypeUUID))
	)
	if id, ok := pc.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = &id
	}
	if value, ok := pc.mutation.Name(); ok {
		_spec.SetField(password.FieldName, field.TypeBytes, value)
		_node.Name = value
	}
	if value, ok := pc.mutation.NameIv(); ok {
		_spec.SetField(password.FieldNameIv, field.TypeBytes, value)
		_node.NameIv = value
	}
	if value, ok := pc.mutation.Username(); ok {
		_spec.SetField(password.FieldUsername, field.TypeBytes, value)
		_node.Username = value
	}
	if value, ok := pc.mutation.UsernameIv(); ok {
		_spec.SetField(password.FieldUsernameIv, field.TypeBytes, value)
		_node.UsernameIv = value
	}
	if value, ok := pc.mutation.Password(); ok {
		_spec.SetField(password.FieldPassword, field.TypeBytes, value)
		_node.Password = value
	}
	if value, ok := pc.mutation.PasswordIv(); ok {
		_spec.SetField(password.FieldPasswordIv, field.TypeBytes, value)
		_node.PasswordIv = value
	}
	if value, ok := pc.mutation.Colour(); ok {
		_spec.SetField(password.FieldColour, field.TypeBytes, value)
		_node.Colour = value
	}
	if value, ok := pc.mutation.ColourIv(); ok {
		_spec.SetField(password.FieldColourIv, field.TypeBytes, value)
		_node.ColourIv = value
	}
	if nodes := pc.mutation.AdditionalFieldsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   password.AdditionalFieldsTable,
			Columns: []string{password.AdditionalFieldsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(additionalfield.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := pc.mutation.UrlsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   password.UrlsTable,
			Columns: []string{password.UrlsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(url.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := pc.mutation.VaultIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   password.VaultTable,
			Columns: []string{password.VaultColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(vault.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.vault_passwords = &nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	return _node, _spec
}

// PasswordCreateBulk is the builder for creating many Password entities in bulk.
type PasswordCreateBulk struct {
	config
	builders []*PasswordCreate
}

// Save creates the Password entities in the database.
func (pcb *PasswordCreateBulk) Save(ctx context.Context) ([]*Password, error) {
	specs := make([]*sqlgraph.CreateSpec, len(pcb.builders))
	nodes := make([]*Password, len(pcb.builders))
	mutators := make([]Mutator, len(pcb.builders))
	for i := range pcb.builders {
		func(i int, root context.Context) {
			builder := pcb.builders[i]
			builder.defaults()
			var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
				mutation, ok := m.(*PasswordMutation)
				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, pcb.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, pcb.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
				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, pcb.builders[0].mutation); err != nil {
			return nil, err
		}
	}
	return nodes, nil
}

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

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

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