// 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/targetmetrics"
)

// TargetMetrics is the model entity for the TargetMetrics schema.
type TargetMetrics struct {
	config `json:"-"`
	// ID of the ent.
	ID int `json:"id,omitempty"`
	// TargetsLoaded holds the value of the "targets_loaded" field.
	TargetsLoaded int64 `json:"targets_loaded,omitempty"`
	// TargetsConfigured holds the value of the "targets_configured" field.
	TargetsConfigured int64 `json:"targets_configured,omitempty"`
	// TargetsConfiguredNotIncludingAspects holds the value of the "targets_configured_not_including_aspects" field.
	TargetsConfiguredNotIncludingAspects int64 `json:"targets_configured_not_including_aspects,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the TargetMetricsQuery when eager-loading is set.
	Edges                  TargetMetricsEdges `json:"edges"`
	metrics_target_metrics *int
	selectValues           sql.SelectValues
}

// TargetMetricsEdges holds the relations/edges for other nodes in the graph.
type TargetMetricsEdges struct {
	// Metrics holds the value of the metrics edge.
	Metrics *Metrics `json:"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
}

// 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 TargetMetricsEdges) 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"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*TargetMetrics) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case targetmetrics.FieldID, targetmetrics.FieldTargetsLoaded, targetmetrics.FieldTargetsConfigured, targetmetrics.FieldTargetsConfiguredNotIncludingAspects:
			values[i] = new(sql.NullInt64)
		case targetmetrics.ForeignKeys[0]: // metrics_target_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 TargetMetrics fields.
func (tm *TargetMetrics) 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 targetmetrics.FieldID:
			value, ok := values[i].(*sql.NullInt64)
			if !ok {
				return fmt.Errorf("unexpected type %T for field id", value)
			}
			tm.ID = int(value.Int64)
		case targetmetrics.FieldTargetsLoaded:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field targets_loaded", values[i])
			} else if value.Valid {
				tm.TargetsLoaded = value.Int64
			}
		case targetmetrics.FieldTargetsConfigured:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field targets_configured", values[i])
			} else if value.Valid {
				tm.TargetsConfigured = value.Int64
			}
		case targetmetrics.FieldTargetsConfiguredNotIncludingAspects:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field targets_configured_not_including_aspects", values[i])
			} else if value.Valid {
				tm.TargetsConfiguredNotIncludingAspects = value.Int64
			}
		case targetmetrics.ForeignKeys[0]:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for edge-field metrics_target_metrics", value)
			} else if value.Valid {
				tm.metrics_target_metrics = new(int)
				*tm.metrics_target_metrics = int(value.Int64)
			}
		default:
			tm.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryMetrics queries the "metrics" edge of the TargetMetrics entity.
func (tm *TargetMetrics) QueryMetrics() *MetricsQuery {
	return NewTargetMetricsClient(tm.config).QueryMetrics(tm)
}

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

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

// String implements the fmt.Stringer.
func (tm *TargetMetrics) String() string {
	var builder strings.Builder
	builder.WriteString("TargetMetrics(")
	builder.WriteString(fmt.Sprintf("id=%v, ", tm.ID))
	builder.WriteString("targets_loaded=")
	builder.WriteString(fmt.Sprintf("%v", tm.TargetsLoaded))
	builder.WriteString(", ")
	builder.WriteString("targets_configured=")
	builder.WriteString(fmt.Sprintf("%v", tm.TargetsConfigured))
	builder.WriteString(", ")
	builder.WriteString("targets_configured_not_including_aspects=")
	builder.WriteString(fmt.Sprintf("%v", tm.TargetsConfiguredNotIncludingAspects))
	builder.WriteByte(')')
	return builder.String()
}

// TargetMetricsSlice is a parsable slice of TargetMetrics.
type TargetMetricsSlice []*TargetMetrics
