// Code generated by ent, DO NOT EDIT.

package ent

import (
	"fmt"
	"strings"

	"entgo.io/ent"
	"entgo.io/ent/dialect/sql"
	"github.com/buildbarn/bb-portal/ent/gen/ent/packageloadmetrics"
	"github.com/buildbarn/bb-portal/ent/gen/ent/packagemetrics"
)

// PackageLoadMetrics is the model entity for the PackageLoadMetrics schema.
type PackageLoadMetrics struct {
	config `json:"-"`
	// ID of the ent.
	ID int `json:"id,omitempty"`
	// Name holds the value of the "name" field.
	Name string `json:"name,omitempty"`
	// LoadDuration holds the value of the "load_duration" field.
	LoadDuration int64 `json:"load_duration,omitempty"`
	// NumTargets holds the value of the "num_targets" field.
	NumTargets uint64 `json:"num_targets,omitempty"`
	// ComputationSteps holds the value of the "computation_steps" field.
	ComputationSteps uint64 `json:"computation_steps,omitempty"`
	// NumTransitiveLoads holds the value of the "num_transitive_loads" field.
	NumTransitiveLoads uint64 `json:"num_transitive_loads,omitempty"`
	// PackageOverhead holds the value of the "package_overhead" field.
	PackageOverhead uint64 `json:"package_overhead,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the PackageLoadMetricsQuery when eager-loading is set.
	Edges                                PackageLoadMetricsEdges `json:"edges"`
	package_metrics_package_load_metrics *int
	selectValues                         sql.SelectValues
}

// PackageLoadMetricsEdges holds the relations/edges for other nodes in the graph.
type PackageLoadMetricsEdges struct {
	// PackageMetrics holds the value of the package_metrics edge.
	PackageMetrics *PackageMetrics `json:"package_metrics,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [1]bool
	// totalCount holds the count of the edges above.
	totalCount [1]map[string]int
}

// PackageMetricsOrErr returns the PackageMetrics value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e PackageLoadMetricsEdges) PackageMetricsOrErr() (*PackageMetrics, error) {
	if e.PackageMetrics != nil {
		return e.PackageMetrics, nil
	} else if e.loadedTypes[0] {
		return nil, &NotFoundError{label: packagemetrics.Label}
	}
	return nil, &NotLoadedError{edge: "package_metrics"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*PackageLoadMetrics) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case packageloadmetrics.FieldID, packageloadmetrics.FieldLoadDuration, packageloadmetrics.FieldNumTargets, packageloadmetrics.FieldComputationSteps, packageloadmetrics.FieldNumTransitiveLoads, packageloadmetrics.FieldPackageOverhead:
			values[i] = new(sql.NullInt64)
		case packageloadmetrics.FieldName:
			values[i] = new(sql.NullString)
		case packageloadmetrics.ForeignKeys[0]: // package_metrics_package_load_metrics
			values[i] = new(sql.NullInt64)
		default:
			values[i] = new(sql.UnknownType)
		}
	}
	return values, nil
}

// assignValues assigns the values that were returned from sql.Rows (after scanning)
// to the PackageLoadMetrics fields.
func (plm *PackageLoadMetrics) assignValues(columns []string, values []any) error {
	if m, n := len(values), len(columns); m < n {
		return fmt.Errorf("mismatch number of scan values: %d != %d", m, n)
	}
	for i := range columns {
		switch columns[i] {
		case packageloadmetrics.FieldID:
			value, ok := values[i].(*sql.NullInt64)
			if !ok {
				return fmt.Errorf("unexpected type %T for field id", value)
			}
			plm.ID = int(value.Int64)
		case packageloadmetrics.FieldName:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field name", values[i])
			} else if value.Valid {
				plm.Name = value.String
			}
		case packageloadmetrics.FieldLoadDuration:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field load_duration", values[i])
			} else if value.Valid {
				plm.LoadDuration = value.Int64
			}
		case packageloadmetrics.FieldNumTargets:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field num_targets", values[i])
			} else if value.Valid {
				plm.NumTargets = uint64(value.Int64)
			}
		case packageloadmetrics.FieldComputationSteps:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field computation_steps", values[i])
			} else if value.Valid {
				plm.ComputationSteps = uint64(value.Int64)
			}
		case packageloadmetrics.FieldNumTransitiveLoads:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field num_transitive_loads", values[i])
			} else if value.Valid {
				plm.NumTransitiveLoads = uint64(value.Int64)
			}
		case packageloadmetrics.FieldPackageOverhead:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field package_overhead", values[i])
			} else if value.Valid {
				plm.PackageOverhead = uint64(value.Int64)
			}
		case packageloadmetrics.ForeignKeys[0]:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for edge-field package_metrics_package_load_metrics", value)
			} else if value.Valid {
				plm.package_metrics_package_load_metrics = new(int)
				*plm.package_metrics_package_load_metrics = int(value.Int64)
			}
		default:
			plm.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

// Value returns the ent.Value that was dynamically selected and assigned to the PackageLoadMetrics.
// This includes values selected through modifiers, order, etc.
func (plm *PackageLoadMetrics) Value(name string) (ent.Value, error) {
	return plm.selectValues.Get(name)
}

// QueryPackageMetrics queries the "package_metrics" edge of the PackageLoadMetrics entity.
func (plm *PackageLoadMetrics) QueryPackageMetrics() *PackageMetricsQuery {
	return NewPackageLoadMetricsClient(plm.config).QueryPackageMetrics(plm)
}

// Update returns a builder for updating this PackageLoadMetrics.
// Note that you need to call PackageLoadMetrics.Unwrap() before calling this method if this PackageLoadMetrics
// was returned from a transaction, and the transaction was committed or rolled back.
func (plm *PackageLoadMetrics) Update() *PackageLoadMetricsUpdateOne {
	return NewPackageLoadMetricsClient(plm.config).UpdateOne(plm)
}

// Unwrap unwraps the PackageLoadMetrics entity that was returned from a transaction after it was closed,
// so that all future queries will be executed through the driver which created the transaction.
func (plm *PackageLoadMetrics) Unwrap() *PackageLoadMetrics {
	_tx, ok := plm.config.driver.(*txDriver)
	if !ok {
		panic("ent: PackageLoadMetrics is not a transactional entity")
	}
	plm.config.driver = _tx.drv
	return plm
}

// String implements the fmt.Stringer.
func (plm *PackageLoadMetrics) String() string {
	var builder strings.Builder
	builder.WriteString("PackageLoadMetrics(")
	builder.WriteString(fmt.Sprintf("id=%v, ", plm.ID))
	builder.WriteString("name=")
	builder.WriteString(plm.Name)
	builder.WriteString(", ")
	builder.WriteString("load_duration=")
	builder.WriteString(fmt.Sprintf("%v", plm.LoadDuration))
	builder.WriteString(", ")
	builder.WriteString("num_targets=")
	builder.WriteString(fmt.Sprintf("%v", plm.NumTargets))
	builder.WriteString(", ")
	builder.WriteString("computation_steps=")
	builder.WriteString(fmt.Sprintf("%v", plm.ComputationSteps))
	builder.WriteString(", ")
	builder.WriteString("num_transitive_loads=")
	builder.WriteString(fmt.Sprintf("%v", plm.NumTransitiveLoads))
	builder.WriteString(", ")
	builder.WriteString("package_overhead=")
	builder.WriteString(fmt.Sprintf("%v", plm.PackageOverhead))
	builder.WriteByte(')')
	return builder.String()
}

// PackageLoadMetricsSlice is a parsable slice of PackageLoadMetrics.
type PackageLoadMetricsSlice []*PackageLoadMetrics
