// Code generated by ent, DO NOT EDIT.

package ent

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

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

// ApiStatisticsCreate is the builder for creating a ApiStatistics entity.
type ApiStatisticsCreate struct {
	config
	mutation *ApiStatisticsMutation
	hooks    []Hook
}

// SetCallCount sets the "call_count" field.
func (asc *ApiStatisticsCreate) SetCallCount(i int32) *ApiStatisticsCreate {
	asc.mutation.SetCallCount(i)
	return asc
}

// SetSuccessCount sets the "success_count" field.
func (asc *ApiStatisticsCreate) SetSuccessCount(i int32) *ApiStatisticsCreate {
	asc.mutation.SetSuccessCount(i)
	return asc
}

// SetFailureCount sets the "failure_count" field.
func (asc *ApiStatisticsCreate) SetFailureCount(i int32) *ApiStatisticsCreate {
	asc.mutation.SetFailureCount(i)
	return asc
}

// SetAvgResponseTime sets the "avg_response_time" field.
func (asc *ApiStatisticsCreate) SetAvgResponseTime(f float64) *ApiStatisticsCreate {
	asc.mutation.SetAvgResponseTime(f)
	return asc
}

// SetMaxResponseTime sets the "max_response_time" field.
func (asc *ApiStatisticsCreate) SetMaxResponseTime(f float64) *ApiStatisticsCreate {
	asc.mutation.SetMaxResponseTime(f)
	return asc
}

// SetMinResponseTime sets the "min_response_time" field.
func (asc *ApiStatisticsCreate) SetMinResponseTime(f float64) *ApiStatisticsCreate {
	asc.mutation.SetMinResponseTime(f)
	return asc
}

// SetAvgTraffic sets the "avg_traffic" field.
func (asc *ApiStatisticsCreate) SetAvgTraffic(f float64) *ApiStatisticsCreate {
	asc.mutation.SetAvgTraffic(f)
	return asc
}

// SetMaxTraffic sets the "max_traffic" field.
func (asc *ApiStatisticsCreate) SetMaxTraffic(f float64) *ApiStatisticsCreate {
	asc.mutation.SetMaxTraffic(f)
	return asc
}

// SetMinTraffic sets the "min_traffic" field.
func (asc *ApiStatisticsCreate) SetMinTraffic(f float64) *ApiStatisticsCreate {
	asc.mutation.SetMinTraffic(f)
	return asc
}

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

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

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

// SetAPIID sets the "api_id" field.
func (asc *ApiStatisticsCreate) SetAPIID(i int32) *ApiStatisticsCreate {
	asc.mutation.SetAPIID(i)
	return asc
}

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

// Mutation returns the ApiStatisticsMutation object of the builder.
func (asc *ApiStatisticsCreate) Mutation() *ApiStatisticsMutation {
	return asc.mutation
}

// Save creates the ApiStatistics in the database.
func (asc *ApiStatisticsCreate) Save(ctx context.Context) (*ApiStatistics, error) {
	return withHooks[*ApiStatistics, ApiStatisticsMutation](ctx, asc.sqlSave, asc.mutation, asc.hooks)
}

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

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

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

// check runs all checks and user-defined validators on the builder.
func (asc *ApiStatisticsCreate) check() error {
	if _, ok := asc.mutation.CallCount(); !ok {
		return &ValidationError{Name: "call_count", err: errors.New(`ent: missing required field "ApiStatistics.call_count"`)}
	}
	if _, ok := asc.mutation.SuccessCount(); !ok {
		return &ValidationError{Name: "success_count", err: errors.New(`ent: missing required field "ApiStatistics.success_count"`)}
	}
	if _, ok := asc.mutation.FailureCount(); !ok {
		return &ValidationError{Name: "failure_count", err: errors.New(`ent: missing required field "ApiStatistics.failure_count"`)}
	}
	if _, ok := asc.mutation.AvgResponseTime(); !ok {
		return &ValidationError{Name: "avg_response_time", err: errors.New(`ent: missing required field "ApiStatistics.avg_response_time"`)}
	}
	if _, ok := asc.mutation.MaxResponseTime(); !ok {
		return &ValidationError{Name: "max_response_time", err: errors.New(`ent: missing required field "ApiStatistics.max_response_time"`)}
	}
	if _, ok := asc.mutation.MinResponseTime(); !ok {
		return &ValidationError{Name: "min_response_time", err: errors.New(`ent: missing required field "ApiStatistics.min_response_time"`)}
	}
	if _, ok := asc.mutation.AvgTraffic(); !ok {
		return &ValidationError{Name: "avg_traffic", err: errors.New(`ent: missing required field "ApiStatistics.avg_traffic"`)}
	}
	if _, ok := asc.mutation.MaxTraffic(); !ok {
		return &ValidationError{Name: "max_traffic", err: errors.New(`ent: missing required field "ApiStatistics.max_traffic"`)}
	}
	if _, ok := asc.mutation.MinTraffic(); !ok {
		return &ValidationError{Name: "min_traffic", err: errors.New(`ent: missing required field "ApiStatistics.min_traffic"`)}
	}
	if _, ok := asc.mutation.Description(); !ok {
		return &ValidationError{Name: "description", err: errors.New(`ent: missing required field "ApiStatistics.description"`)}
	}
	if _, ok := asc.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "ApiStatistics.created_at"`)}
	}
	if _, ok := asc.mutation.UpdateAt(); !ok {
		return &ValidationError{Name: "update_at", err: errors.New(`ent: missing required field "ApiStatistics.update_at"`)}
	}
	if _, ok := asc.mutation.APIID(); !ok {
		return &ValidationError{Name: "api_id", err: errors.New(`ent: missing required field "ApiStatistics.api_id"`)}
	}
	if v, ok := asc.mutation.APIID(); ok {
		if err := apistatistics.APIIDValidator(v); err != nil {
			return &ValidationError{Name: "api_id", err: fmt.Errorf(`ent: validator failed for field "ApiStatistics.api_id": %w`, err)}
		}
	}
	return nil
}

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

func (asc *ApiStatisticsCreate) createSpec() (*ApiStatistics, *sqlgraph.CreateSpec) {
	var (
		_node = &ApiStatistics{config: asc.config}
		_spec = sqlgraph.NewCreateSpec(apistatistics.Table, sqlgraph.NewFieldSpec(apistatistics.FieldID, field.TypeInt32))
	)
	if id, ok := asc.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := asc.mutation.CallCount(); ok {
		_spec.SetField(apistatistics.FieldCallCount, field.TypeInt32, value)
		_node.CallCount = value
	}
	if value, ok := asc.mutation.SuccessCount(); ok {
		_spec.SetField(apistatistics.FieldSuccessCount, field.TypeInt32, value)
		_node.SuccessCount = value
	}
	if value, ok := asc.mutation.FailureCount(); ok {
		_spec.SetField(apistatistics.FieldFailureCount, field.TypeInt32, value)
		_node.FailureCount = value
	}
	if value, ok := asc.mutation.AvgResponseTime(); ok {
		_spec.SetField(apistatistics.FieldAvgResponseTime, field.TypeFloat64, value)
		_node.AvgResponseTime = value
	}
	if value, ok := asc.mutation.MaxResponseTime(); ok {
		_spec.SetField(apistatistics.FieldMaxResponseTime, field.TypeFloat64, value)
		_node.MaxResponseTime = value
	}
	if value, ok := asc.mutation.MinResponseTime(); ok {
		_spec.SetField(apistatistics.FieldMinResponseTime, field.TypeFloat64, value)
		_node.MinResponseTime = value
	}
	if value, ok := asc.mutation.AvgTraffic(); ok {
		_spec.SetField(apistatistics.FieldAvgTraffic, field.TypeFloat64, value)
		_node.AvgTraffic = value
	}
	if value, ok := asc.mutation.MaxTraffic(); ok {
		_spec.SetField(apistatistics.FieldMaxTraffic, field.TypeFloat64, value)
		_node.MaxTraffic = value
	}
	if value, ok := asc.mutation.MinTraffic(); ok {
		_spec.SetField(apistatistics.FieldMinTraffic, field.TypeFloat64, value)
		_node.MinTraffic = value
	}
	if value, ok := asc.mutation.Description(); ok {
		_spec.SetField(apistatistics.FieldDescription, field.TypeString, value)
		_node.Description = value
	}
	if value, ok := asc.mutation.CreatedAt(); ok {
		_spec.SetField(apistatistics.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := asc.mutation.UpdateAt(); ok {
		_spec.SetField(apistatistics.FieldUpdateAt, field.TypeTime, value)
		_node.UpdateAt = value
	}
	if value, ok := asc.mutation.APIID(); ok {
		_spec.SetField(apistatistics.FieldAPIID, field.TypeInt32, value)
		_node.APIID = value
	}
	return _node, _spec
}

// ApiStatisticsCreateBulk is the builder for creating many ApiStatistics entities in bulk.
type ApiStatisticsCreateBulk struct {
	config
	builders []*ApiStatisticsCreate
}

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

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

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

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