// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"time"

	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"git.dcentral.systems/toolz/goplt/internal/ent/auditlog"
)

// AuditLogCreate is the builder for creating a AuditLog entity.
type AuditLogCreate struct {
	config
	mutation *AuditLogMutation
	hooks    []Hook
}

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

// SetAction sets the "action" field.
func (_c *AuditLogCreate) SetAction(v string) *AuditLogCreate {
	_c.mutation.SetAction(v)
	return _c
}

// SetResource sets the "resource" field.
func (_c *AuditLogCreate) SetResource(v string) *AuditLogCreate {
	_c.mutation.SetResource(v)
	return _c
}

// SetNillableResource sets the "resource" field if the given value is not nil.
func (_c *AuditLogCreate) SetNillableResource(v *string) *AuditLogCreate {
	if v != nil {
		_c.SetResource(*v)
	}
	return _c
}

// SetResourceID sets the "resource_id" field.
func (_c *AuditLogCreate) SetResourceID(v string) *AuditLogCreate {
	_c.mutation.SetResourceID(v)
	return _c
}

// SetNillableResourceID sets the "resource_id" field if the given value is not nil.
func (_c *AuditLogCreate) SetNillableResourceID(v *string) *AuditLogCreate {
	if v != nil {
		_c.SetResourceID(*v)
	}
	return _c
}

// SetIPAddress sets the "ip_address" field.
func (_c *AuditLogCreate) SetIPAddress(v string) *AuditLogCreate {
	_c.mutation.SetIPAddress(v)
	return _c
}

// SetNillableIPAddress sets the "ip_address" field if the given value is not nil.
func (_c *AuditLogCreate) SetNillableIPAddress(v *string) *AuditLogCreate {
	if v != nil {
		_c.SetIPAddress(*v)
	}
	return _c
}

// SetUserAgent sets the "user_agent" field.
func (_c *AuditLogCreate) SetUserAgent(v string) *AuditLogCreate {
	_c.mutation.SetUserAgent(v)
	return _c
}

// SetNillableUserAgent sets the "user_agent" field if the given value is not nil.
func (_c *AuditLogCreate) SetNillableUserAgent(v *string) *AuditLogCreate {
	if v != nil {
		_c.SetUserAgent(*v)
	}
	return _c
}

// SetMetadata sets the "metadata" field.
func (_c *AuditLogCreate) SetMetadata(v map[string]interface{}) *AuditLogCreate {
	_c.mutation.SetMetadata(v)
	return _c
}

// SetTimestamp sets the "timestamp" field.
func (_c *AuditLogCreate) SetTimestamp(v time.Time) *AuditLogCreate {
	_c.mutation.SetTimestamp(v)
	return _c
}

// SetNillableTimestamp sets the "timestamp" field if the given value is not nil.
func (_c *AuditLogCreate) SetNillableTimestamp(v *time.Time) *AuditLogCreate {
	if v != nil {
		_c.SetTimestamp(*v)
	}
	return _c
}

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

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

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

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_c *AuditLogCreate) 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 *AuditLogCreate) defaults() {
	if _, ok := _c.mutation.Timestamp(); !ok {
		v := auditlog.DefaultTimestamp()
		_c.mutation.SetTimestamp(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_c *AuditLogCreate) check() error {
	if _, ok := _c.mutation.UserID(); !ok {
		return &ValidationError{Name: "user_id", err: errors.New(`ent: missing required field "AuditLog.user_id"`)}
	}
	if v, ok := _c.mutation.UserID(); ok {
		if err := auditlog.UserIDValidator(v); err != nil {
			return &ValidationError{Name: "user_id", err: fmt.Errorf(`ent: validator failed for field "AuditLog.user_id": %w`, err)}
		}
	}
	if _, ok := _c.mutation.Action(); !ok {
		return &ValidationError{Name: "action", err: errors.New(`ent: missing required field "AuditLog.action"`)}
	}
	if v, ok := _c.mutation.Action(); ok {
		if err := auditlog.ActionValidator(v); err != nil {
			return &ValidationError{Name: "action", err: fmt.Errorf(`ent: validator failed for field "AuditLog.action": %w`, err)}
		}
	}
	if _, ok := _c.mutation.Timestamp(); !ok {
		return &ValidationError{Name: "timestamp", err: errors.New(`ent: missing required field "AuditLog.timestamp"`)}
	}
	return nil
}

func (_c *AuditLogCreate) sqlSave(ctx context.Context) (*AuditLog, 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 != nil {
		if id, ok := _spec.ID.Value.(string); ok {
			_node.ID = id
		} else {
			return nil, fmt.Errorf("unexpected AuditLog.ID type: %T", _spec.ID.Value)
		}
	}
	_c.mutation.id = &_node.ID
	_c.mutation.done = true
	return _node, nil
}

func (_c *AuditLogCreate) createSpec() (*AuditLog, *sqlgraph.CreateSpec) {
	var (
		_node = &AuditLog{config: _c.config}
		_spec = sqlgraph.NewCreateSpec(auditlog.Table, sqlgraph.NewFieldSpec(auditlog.FieldID, field.TypeString))
	)
	if id, ok := _c.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := _c.mutation.UserID(); ok {
		_spec.SetField(auditlog.FieldUserID, field.TypeString, value)
		_node.UserID = value
	}
	if value, ok := _c.mutation.Action(); ok {
		_spec.SetField(auditlog.FieldAction, field.TypeString, value)
		_node.Action = value
	}
	if value, ok := _c.mutation.Resource(); ok {
		_spec.SetField(auditlog.FieldResource, field.TypeString, value)
		_node.Resource = value
	}
	if value, ok := _c.mutation.ResourceID(); ok {
		_spec.SetField(auditlog.FieldResourceID, field.TypeString, value)
		_node.ResourceID = value
	}
	if value, ok := _c.mutation.IPAddress(); ok {
		_spec.SetField(auditlog.FieldIPAddress, field.TypeString, value)
		_node.IPAddress = value
	}
	if value, ok := _c.mutation.UserAgent(); ok {
		_spec.SetField(auditlog.FieldUserAgent, field.TypeString, value)
		_node.UserAgent = value
	}
	if value, ok := _c.mutation.Metadata(); ok {
		_spec.SetField(auditlog.FieldMetadata, field.TypeJSON, value)
		_node.Metadata = value
	}
	if value, ok := _c.mutation.Timestamp(); ok {
		_spec.SetField(auditlog.FieldTimestamp, field.TypeTime, value)
		_node.Timestamp = value
	}
	return _node, _spec
}

// AuditLogCreateBulk is the builder for creating many AuditLog entities in bulk.
type AuditLogCreateBulk struct {
	config
	err      error
	builders []*AuditLogCreate
}

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

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

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