// 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"
	"github.com/TSELab/astra/internal/store/ent/artifact"
)

// ArtifactCreate is the builder for creating a Artifact entity.
type ArtifactCreate struct {
	config
	mutation *ArtifactMutation
	hooks    []Hook
}

// SetAstraID sets the "astra_id" field.
func (_c *ArtifactCreate) SetAstraID(v string) *ArtifactCreate {
	_c.mutation.SetAstraID(v)
	return _c
}

// SetName sets the "name" field.
func (_c *ArtifactCreate) SetName(v string) *ArtifactCreate {
	_c.mutation.SetName(v)
	return _c
}

// SetNillableName sets the "name" field if the given value is not nil.
func (_c *ArtifactCreate) SetNillableName(v *string) *ArtifactCreate {
	if v != nil {
		_c.SetName(*v)
	}
	return _c
}

// SetKind sets the "kind" field.
func (_c *ArtifactCreate) SetKind(v string) *ArtifactCreate {
	_c.mutation.SetKind(v)
	return _c
}

// SetNillableKind sets the "kind" field if the given value is not nil.
func (_c *ArtifactCreate) SetNillableKind(v *string) *ArtifactCreate {
	if v != nil {
		_c.SetKind(*v)
	}
	return _c
}

// SetURI sets the "uri" field.
func (_c *ArtifactCreate) SetURI(v string) *ArtifactCreate {
	_c.mutation.SetURI(v)
	return _c
}

// SetNillableURI sets the "uri" field if the given value is not nil.
func (_c *ArtifactCreate) SetNillableURI(v *string) *ArtifactCreate {
	if v != nil {
		_c.SetURI(*v)
	}
	return _c
}

// SetVersion sets the "version" field.
func (_c *ArtifactCreate) SetVersion(v string) *ArtifactCreate {
	_c.mutation.SetVersion(v)
	return _c
}

// SetNillableVersion sets the "version" field if the given value is not nil.
func (_c *ArtifactCreate) SetNillableVersion(v *string) *ArtifactCreate {
	if v != nil {
		_c.SetVersion(*v)
	}
	return _c
}

// SetHash sets the "hash" field.
func (_c *ArtifactCreate) SetHash(v string) *ArtifactCreate {
	_c.mutation.SetHash(v)
	return _c
}

// SetNillableHash sets the "hash" field if the given value is not nil.
func (_c *ArtifactCreate) SetNillableHash(v *string) *ArtifactCreate {
	if v != nil {
		_c.SetHash(*v)
	}
	return _c
}

// SetSize sets the "size" field.
func (_c *ArtifactCreate) SetSize(v int64) *ArtifactCreate {
	_c.mutation.SetSize(v)
	return _c
}

// SetNillableSize sets the "size" field if the given value is not nil.
func (_c *ArtifactCreate) SetNillableSize(v *int64) *ArtifactCreate {
	if v != nil {
		_c.SetSize(*v)
	}
	return _c
}

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

// SetCompleteness sets the "completeness" field.
func (_c *ArtifactCreate) SetCompleteness(v artifact.Completeness) *ArtifactCreate {
	_c.mutation.SetCompleteness(v)
	return _c
}

// SetNillableCompleteness sets the "completeness" field if the given value is not nil.
func (_c *ArtifactCreate) SetNillableCompleteness(v *artifact.Completeness) *ArtifactCreate {
	if v != nil {
		_c.SetCompleteness(*v)
	}
	return _c
}

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

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

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

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

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_c *ArtifactCreate) 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 *ArtifactCreate) defaults() {
	if _, ok := _c.mutation.Name(); !ok {
		v := artifact.DefaultName
		_c.mutation.SetName(v)
	}
	if _, ok := _c.mutation.Kind(); !ok {
		v := artifact.DefaultKind
		_c.mutation.SetKind(v)
	}
	if _, ok := _c.mutation.URI(); !ok {
		v := artifact.DefaultURI
		_c.mutation.SetURI(v)
	}
	if _, ok := _c.mutation.Version(); !ok {
		v := artifact.DefaultVersion
		_c.mutation.SetVersion(v)
	}
	if _, ok := _c.mutation.Hash(); !ok {
		v := artifact.DefaultHash
		_c.mutation.SetHash(v)
	}
	if _, ok := _c.mutation.Size(); !ok {
		v := artifact.DefaultSize
		_c.mutation.SetSize(v)
	}
	if _, ok := _c.mutation.Completeness(); !ok {
		v := artifact.DefaultCompleteness
		_c.mutation.SetCompleteness(v)
	}
	if _, ok := _c.mutation.CreatedAt(); !ok {
		v := artifact.DefaultCreatedAt()
		_c.mutation.SetCreatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_c *ArtifactCreate) check() error {
	if _, ok := _c.mutation.AstraID(); !ok {
		return &ValidationError{Name: "astra_id", err: errors.New(`ent: missing required field "Artifact.astra_id"`)}
	}
	if v, ok := _c.mutation.AstraID(); ok {
		if err := artifact.AstraIDValidator(v); err != nil {
			return &ValidationError{Name: "astra_id", err: fmt.Errorf(`ent: validator failed for field "Artifact.astra_id": %w`, err)}
		}
	}
	if _, ok := _c.mutation.Name(); !ok {
		return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "Artifact.name"`)}
	}
	if _, ok := _c.mutation.Kind(); !ok {
		return &ValidationError{Name: "kind", err: errors.New(`ent: missing required field "Artifact.kind"`)}
	}
	if _, ok := _c.mutation.URI(); !ok {
		return &ValidationError{Name: "uri", err: errors.New(`ent: missing required field "Artifact.uri"`)}
	}
	if _, ok := _c.mutation.Version(); !ok {
		return &ValidationError{Name: "version", err: errors.New(`ent: missing required field "Artifact.version"`)}
	}
	if _, ok := _c.mutation.Hash(); !ok {
		return &ValidationError{Name: "hash", err: errors.New(`ent: missing required field "Artifact.hash"`)}
	}
	if _, ok := _c.mutation.Size(); !ok {
		return &ValidationError{Name: "size", err: errors.New(`ent: missing required field "Artifact.size"`)}
	}
	if _, ok := _c.mutation.Completeness(); !ok {
		return &ValidationError{Name: "completeness", err: errors.New(`ent: missing required field "Artifact.completeness"`)}
	}
	if v, ok := _c.mutation.Completeness(); ok {
		if err := artifact.CompletenessValidator(v); err != nil {
			return &ValidationError{Name: "completeness", err: fmt.Errorf(`ent: validator failed for field "Artifact.completeness": %w`, err)}
		}
	}
	if _, ok := _c.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Artifact.created_at"`)}
	}
	return nil
}

func (_c *ArtifactCreate) sqlSave(ctx context.Context) (*Artifact, 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
	}
	id := _spec.ID.Value.(int64)
	_node.ID = int(id)
	_c.mutation.id = &_node.ID
	_c.mutation.done = true
	return _node, nil
}

func (_c *ArtifactCreate) createSpec() (*Artifact, *sqlgraph.CreateSpec) {
	var (
		_node = &Artifact{config: _c.config}
		_spec = sqlgraph.NewCreateSpec(artifact.Table, sqlgraph.NewFieldSpec(artifact.FieldID, field.TypeInt))
	)
	if value, ok := _c.mutation.AstraID(); ok {
		_spec.SetField(artifact.FieldAstraID, field.TypeString, value)
		_node.AstraID = value
	}
	if value, ok := _c.mutation.Name(); ok {
		_spec.SetField(artifact.FieldName, field.TypeString, value)
		_node.Name = value
	}
	if value, ok := _c.mutation.Kind(); ok {
		_spec.SetField(artifact.FieldKind, field.TypeString, value)
		_node.Kind = value
	}
	if value, ok := _c.mutation.URI(); ok {
		_spec.SetField(artifact.FieldURI, field.TypeString, value)
		_node.URI = value
	}
	if value, ok := _c.mutation.Version(); ok {
		_spec.SetField(artifact.FieldVersion, field.TypeString, value)
		_node.Version = value
	}
	if value, ok := _c.mutation.Hash(); ok {
		_spec.SetField(artifact.FieldHash, field.TypeString, value)
		_node.Hash = value
	}
	if value, ok := _c.mutation.Size(); ok {
		_spec.SetField(artifact.FieldSize, field.TypeInt64, value)
		_node.Size = value
	}
	if value, ok := _c.mutation.Metadata(); ok {
		_spec.SetField(artifact.FieldMetadata, field.TypeJSON, value)
		_node.Metadata = value
	}
	if value, ok := _c.mutation.Completeness(); ok {
		_spec.SetField(artifact.FieldCompleteness, field.TypeEnum, value)
		_node.Completeness = value
	}
	if value, ok := _c.mutation.CreatedAt(); ok {
		_spec.SetField(artifact.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	return _node, _spec
}

// ArtifactCreateBulk is the builder for creating many Artifact entities in bulk.
type ArtifactCreateBulk struct {
	config
	err      error
	builders []*ArtifactCreate
}

// Save creates the Artifact entities in the database.
func (_c *ArtifactCreateBulk) Save(ctx context.Context) ([]*Artifact, error) {
	if _c.err != nil {
		return nil, _c.err
	}
	specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
	nodes := make([]*Artifact, 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.(*ArtifactMutation)
				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 {
					id := specs[i].ID.Value.(int64)
					nodes[i].ID = int(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 *ArtifactCreateBulk) SaveX(ctx context.Context) []*Artifact {
	v, err := _c.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

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

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