// Code generated by ent, DO NOT EDIT.

package ent

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

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

// APICreate is the builder for creating a Api entity.
type APICreate struct {
	config
	mutation *APIMutation
	hooks    []Hook
}

// SetName sets the "name" field.
func (ac *APICreate) SetName(s string) *APICreate {
	ac.mutation.SetName(s)
	return ac
}

// SetURL sets the "url" field.
func (ac *APICreate) SetURL(s string) *APICreate {
	ac.mutation.SetURL(s)
	return ac
}

// SetType sets the "type" field.
func (ac *APICreate) SetType(i int8) *APICreate {
	ac.mutation.SetType(i)
	return ac
}

// SetStatus sets the "status" field.
func (ac *APICreate) SetStatus(i int8) *APICreate {
	ac.mutation.SetStatus(i)
	return ac
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (ac *APICreate) SetNillableStatus(i *int8) *APICreate {
	if i != nil {
		ac.SetStatus(*i)
	}
	return ac
}

// SetHeaders sets the "headers" field.
func (ac *APICreate) SetHeaders(s string) *APICreate {
	ac.mutation.SetHeaders(s)
	return ac
}

// SetNillableHeaders sets the "headers" field if the given value is not nil.
func (ac *APICreate) SetNillableHeaders(s *string) *APICreate {
	if s != nil {
		ac.SetHeaders(*s)
	}
	return ac
}

// SetBody sets the "body" field.
func (ac *APICreate) SetBody(s string) *APICreate {
	ac.mutation.SetBody(s)
	return ac
}

// SetNillableBody sets the "body" field if the given value is not nil.
func (ac *APICreate) SetNillableBody(s *string) *APICreate {
	if s != nil {
		ac.SetBody(*s)
	}
	return ac
}

// SetLabel sets the "label" field.
func (ac *APICreate) SetLabel(s string) *APICreate {
	ac.mutation.SetLabel(s)
	return ac
}

// SetNillableLabel sets the "label" field if the given value is not nil.
func (ac *APICreate) SetNillableLabel(s *string) *APICreate {
	if s != nil {
		ac.SetLabel(*s)
	}
	return ac
}

// SetQueryParams sets the "query_params" field.
func (ac *APICreate) SetQueryParams(s string) *APICreate {
	ac.mutation.SetQueryParams(s)
	return ac
}

// SetNillableQueryParams sets the "query_params" field if the given value is not nil.
func (ac *APICreate) SetNillableQueryParams(s *string) *APICreate {
	if s != nil {
		ac.SetQueryParams(*s)
	}
	return ac
}

// SetResponse sets the "response" field.
func (ac *APICreate) SetResponse(s string) *APICreate {
	ac.mutation.SetResponse(s)
	return ac
}

// SetNillableResponse sets the "response" field if the given value is not nil.
func (ac *APICreate) SetNillableResponse(s *string) *APICreate {
	if s != nil {
		ac.SetResponse(*s)
	}
	return ac
}

// SetModule sets the "module" field.
func (ac *APICreate) SetModule(s string) *APICreate {
	ac.mutation.SetModule(s)
	return ac
}

// SetNillableModule sets the "module" field if the given value is not nil.
func (ac *APICreate) SetNillableModule(s *string) *APICreate {
	if s != nil {
		ac.SetModule(*s)
	}
	return ac
}

// SetDescription sets the "description" field.
func (ac *APICreate) SetDescription(s string) *APICreate {
	ac.mutation.SetDescription(s)
	return ac
}

// SetNillableDescription sets the "description" field if the given value is not nil.
func (ac *APICreate) SetNillableDescription(s *string) *APICreate {
	if s != nil {
		ac.SetDescription(*s)
	}
	return ac
}

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

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

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

// SetIncludeFiles sets the "include_files" field.
func (ac *APICreate) SetIncludeFiles(s string) *APICreate {
	ac.mutation.SetIncludeFiles(s)
	return ac
}

// SetNillableIncludeFiles sets the "include_files" field if the given value is not nil.
func (ac *APICreate) SetNillableIncludeFiles(s *string) *APICreate {
	if s != nil {
		ac.SetIncludeFiles(*s)
	}
	return ac
}

// SetUpdateAt sets the "update_at" field.
func (ac *APICreate) SetUpdateAt(t time.Time) *APICreate {
	ac.mutation.SetUpdateAt(t)
	return ac
}

// SetNillableUpdateAt sets the "update_at" field if the given value is not nil.
func (ac *APICreate) SetNillableUpdateAt(t *time.Time) *APICreate {
	if t != nil {
		ac.SetUpdateAt(*t)
	}
	return ac
}

// SetUpdateBy sets the "update_by" field.
func (ac *APICreate) SetUpdateBy(u uint32) *APICreate {
	ac.mutation.SetUpdateBy(u)
	return ac
}

// SetNillableUpdateBy sets the "update_by" field if the given value is not nil.
func (ac *APICreate) SetNillableUpdateBy(u *uint32) *APICreate {
	if u != nil {
		ac.SetUpdateBy(*u)
	}
	return ac
}

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

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

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

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

// SetID sets the "id" field.
func (ac *APICreate) SetID(i int32) *APICreate {
	ac.mutation.SetID(i)
	return ac
}

// Mutation returns the APIMutation object of the builder.
func (ac *APICreate) Mutation() *APIMutation {
	return ac.mutation
}

// Save creates the Api in the database.
func (ac *APICreate) Save(ctx context.Context) (*Api, error) {
	ac.defaults()
	return withHooks[*Api, APIMutation](ctx, ac.sqlSave, ac.mutation, ac.hooks)
}

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

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

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

// defaults sets the default values of the builder before save.
func (ac *APICreate) defaults() {
	if _, ok := ac.mutation.Status(); !ok {
		v := api.DefaultStatus
		ac.mutation.SetStatus(v)
	}
	if _, ok := ac.mutation.CreatedAt(); !ok {
		v := api.DefaultCreatedAt()
		ac.mutation.SetCreatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (ac *APICreate) check() error {
	if _, ok := ac.mutation.Name(); !ok {
		return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "Api.name"`)}
	}
	if v, ok := ac.mutation.Name(); ok {
		if err := api.NameValidator(v); err != nil {
			return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "Api.name": %w`, err)}
		}
	}
	if _, ok := ac.mutation.URL(); !ok {
		return &ValidationError{Name: "url", err: errors.New(`ent: missing required field "Api.url"`)}
	}
	if v, ok := ac.mutation.URL(); ok {
		if err := api.URLValidator(v); err != nil {
			return &ValidationError{Name: "url", err: fmt.Errorf(`ent: validator failed for field "Api.url": %w`, err)}
		}
	}
	if _, ok := ac.mutation.GetType(); !ok {
		return &ValidationError{Name: "type", err: errors.New(`ent: missing required field "Api.type"`)}
	}
	if v, ok := ac.mutation.GetType(); ok {
		if err := api.TypeValidator(v); err != nil {
			return &ValidationError{Name: "type", err: fmt.Errorf(`ent: validator failed for field "Api.type": %w`, err)}
		}
	}
	if _, ok := ac.mutation.Status(); !ok {
		return &ValidationError{Name: "status", err: errors.New(`ent: missing required field "Api.status"`)}
	}
	if _, ok := ac.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Api.created_at"`)}
	}
	if _, ok := ac.mutation.CreatedBy(); !ok {
		return &ValidationError{Name: "created_by", err: errors.New(`ent: missing required field "Api.created_by"`)}
	}
	return nil
}

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

func (ac *APICreate) createSpec() (*Api, *sqlgraph.CreateSpec) {
	var (
		_node = &Api{config: ac.config}
		_spec = sqlgraph.NewCreateSpec(api.Table, sqlgraph.NewFieldSpec(api.FieldID, field.TypeInt32))
	)
	if id, ok := ac.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := ac.mutation.Name(); ok {
		_spec.SetField(api.FieldName, field.TypeString, value)
		_node.Name = value
	}
	if value, ok := ac.mutation.URL(); ok {
		_spec.SetField(api.FieldURL, field.TypeString, value)
		_node.URL = value
	}
	if value, ok := ac.mutation.GetType(); ok {
		_spec.SetField(api.FieldType, field.TypeInt8, value)
		_node.Type = value
	}
	if value, ok := ac.mutation.Status(); ok {
		_spec.SetField(api.FieldStatus, field.TypeInt8, value)
		_node.Status = value
	}
	if value, ok := ac.mutation.Headers(); ok {
		_spec.SetField(api.FieldHeaders, field.TypeString, value)
		_node.Headers = value
	}
	if value, ok := ac.mutation.Body(); ok {
		_spec.SetField(api.FieldBody, field.TypeString, value)
		_node.Body = value
	}
	if value, ok := ac.mutation.Label(); ok {
		_spec.SetField(api.FieldLabel, field.TypeString, value)
		_node.Label = value
	}
	if value, ok := ac.mutation.QueryParams(); ok {
		_spec.SetField(api.FieldQueryParams, field.TypeString, value)
		_node.QueryParams = value
	}
	if value, ok := ac.mutation.Response(); ok {
		_spec.SetField(api.FieldResponse, field.TypeString, value)
		_node.Response = value
	}
	if value, ok := ac.mutation.Module(); ok {
		_spec.SetField(api.FieldModule, field.TypeString, value)
		_node.Module = value
	}
	if value, ok := ac.mutation.Description(); ok {
		_spec.SetField(api.FieldDescription, field.TypeString, value)
		_node.Description = value
	}
	if value, ok := ac.mutation.CreatedAt(); ok {
		_spec.SetField(api.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := ac.mutation.CreatedBy(); ok {
		_spec.SetField(api.FieldCreatedBy, field.TypeUint32, value)
		_node.CreatedBy = value
	}
	if value, ok := ac.mutation.IncludeFiles(); ok {
		_spec.SetField(api.FieldIncludeFiles, field.TypeString, value)
		_node.IncludeFiles = value
	}
	if value, ok := ac.mutation.UpdateAt(); ok {
		_spec.SetField(api.FieldUpdateAt, field.TypeTime, value)
		_node.UpdateAt = value
	}
	if value, ok := ac.mutation.UpdateBy(); ok {
		_spec.SetField(api.FieldUpdateBy, field.TypeUint32, value)
		_node.UpdateBy = value
	}
	if value, ok := ac.mutation.DeletedAt(); ok {
		_spec.SetField(api.FieldDeletedAt, field.TypeTime, value)
		_node.DeletedAt = value
	}
	if value, ok := ac.mutation.DeletedBy(); ok {
		_spec.SetField(api.FieldDeletedBy, field.TypeUint32, value)
		_node.DeletedBy = value
	}
	return _node, _spec
}

// APICreateBulk is the builder for creating many Api entities in bulk.
type APICreateBulk struct {
	config
	builders []*APICreate
}

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

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

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

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