// 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/metrics"
	"github.com/buildbarn/bb-portal/ent/gen/ent/packagemetrics"
)

// PackageMetrics is the model entity for the PackageMetrics schema.
type PackageMetrics struct {
	config `json:"-"`
	// ID of the ent.
	ID int `json:"id,omitempty"`
	// PackagesLoaded holds the value of the "packages_loaded" field.
	PackagesLoaded int64 `json:"packages_loaded,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the PackageMetricsQuery when eager-loading is set.
	Edges                   PackageMetricsEdges `json:"edges"`
	metrics_package_metrics *int
	selectValues            sql.SelectValues
}

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

	namedPackageLoadMetrics map[string][]*PackageLoadMetrics
}

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

// PackageLoadMetricsOrErr returns the PackageLoadMetrics value or an error if the edge
// was not loaded in eager-loading.
func (e PackageMetricsEdges) PackageLoadMetricsOrErr() ([]*PackageLoadMetrics, error) {
	if e.loadedTypes[1] {
		return e.PackageLoadMetrics, nil
	}
	return nil, &NotLoadedError{edge: "package_load_metrics"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*PackageMetrics) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case packagemetrics.FieldID, packagemetrics.FieldPackagesLoaded:
			values[i] = new(sql.NullInt64)
		case packagemetrics.ForeignKeys[0]: // metrics_package_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 PackageMetrics fields.
func (pm *PackageMetrics) 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 packagemetrics.FieldID:
			value, ok := values[i].(*sql.NullInt64)
			if !ok {
				return fmt.Errorf("unexpected type %T for field id", value)
			}
			pm.ID = int(value.Int64)
		case packagemetrics.FieldPackagesLoaded:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field packages_loaded", values[i])
			} else if value.Valid {
				pm.PackagesLoaded = value.Int64
			}
		case packagemetrics.ForeignKeys[0]:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for edge-field metrics_package_metrics", value)
			} else if value.Valid {
				pm.metrics_package_metrics = new(int)
				*pm.metrics_package_metrics = int(value.Int64)
			}
		default:
			pm.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryMetrics queries the "metrics" edge of the PackageMetrics entity.
func (pm *PackageMetrics) QueryMetrics() *MetricsQuery {
	return NewPackageMetricsClient(pm.config).QueryMetrics(pm)
}

// QueryPackageLoadMetrics queries the "package_load_metrics" edge of the PackageMetrics entity.
func (pm *PackageMetrics) QueryPackageLoadMetrics() *PackageLoadMetricsQuery {
	return NewPackageMetricsClient(pm.config).QueryPackageLoadMetrics(pm)
}

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

// Unwrap unwraps the PackageMetrics 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 (pm *PackageMetrics) Unwrap() *PackageMetrics {
	_tx, ok := pm.config.driver.(*txDriver)
	if !ok {
		panic("ent: PackageMetrics is not a transactional entity")
	}
	pm.config.driver = _tx.drv
	return pm
}

// String implements the fmt.Stringer.
func (pm *PackageMetrics) String() string {
	var builder strings.Builder
	builder.WriteString("PackageMetrics(")
	builder.WriteString(fmt.Sprintf("id=%v, ", pm.ID))
	builder.WriteString("packages_loaded=")
	builder.WriteString(fmt.Sprintf("%v", pm.PackagesLoaded))
	builder.WriteByte(')')
	return builder.String()
}

// NamedPackageLoadMetrics returns the PackageLoadMetrics named value or an error if the edge was not
// loaded in eager-loading with this name.
func (pm *PackageMetrics) NamedPackageLoadMetrics(name string) ([]*PackageLoadMetrics, error) {
	if pm.Edges.namedPackageLoadMetrics == nil {
		return nil, &NotLoadedError{edge: name}
	}
	nodes, ok := pm.Edges.namedPackageLoadMetrics[name]
	if !ok {
		return nil, &NotLoadedError{edge: name}
	}
	return nodes, nil
}

func (pm *PackageMetrics) appendNamedPackageLoadMetrics(name string, edges ...*PackageLoadMetrics) {
	if pm.Edges.namedPackageLoadMetrics == nil {
		pm.Edges.namedPackageLoadMetrics = make(map[string][]*PackageLoadMetrics)
	}
	if len(edges) == 0 {
		pm.Edges.namedPackageLoadMetrics[name] = []*PackageLoadMetrics{}
	} else {
		pm.Edges.namedPackageLoadMetrics[name] = append(pm.Edges.namedPackageLoadMetrics[name], edges...)
	}
}

// PackageMetricsSlice is a parsable slice of PackageMetrics.
type PackageMetricsSlice []*PackageMetrics
