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

// ArtifactMetrics is the model entity for the ArtifactMetrics schema.
type ArtifactMetrics struct {
	config
	// ID of the ent.
	ID int `json:"id,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the ArtifactMetricsQuery when eager-loading is set.
	Edges                                               ArtifactMetricsEdges `json:"edges"`
	artifact_metrics_source_artifacts_read              *int
	artifact_metrics_output_artifacts_seen              *int
	artifact_metrics_output_artifacts_from_action_cache *int
	metrics_artifact_metrics                            *int
	selectValues                                        sql.SelectValues
}

// ArtifactMetricsEdges holds the relations/edges for other nodes in the graph.
type ArtifactMetricsEdges struct {
	// Metrics holds the value of the metrics edge.
	Metrics *Metrics `json:"metrics,omitempty"`
	// SourceArtifactsRead holds the value of the source_artifacts_read edge.
	SourceArtifactsRead *FilesMetric `json:"source_artifacts_read,omitempty"`
	// OutputArtifactsSeen holds the value of the output_artifacts_seen edge.
	OutputArtifactsSeen *FilesMetric `json:"output_artifacts_seen,omitempty"`
	// OutputArtifactsFromActionCache holds the value of the output_artifacts_from_action_cache edge.
	OutputArtifactsFromActionCache *FilesMetric `json:"output_artifacts_from_action_cache,omitempty"`
	// TopLevelArtifacts holds the value of the top_level_artifacts edge.
	TopLevelArtifacts *FilesMetric `json:"top_level_artifacts,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [5]bool
	// totalCount holds the count of the edges above.
	totalCount [5]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 ArtifactMetricsEdges) 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"}
}

// SourceArtifactsReadOrErr returns the SourceArtifactsRead value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e ArtifactMetricsEdges) SourceArtifactsReadOrErr() (*FilesMetric, error) {
	if e.SourceArtifactsRead != nil {
		return e.SourceArtifactsRead, nil
	} else if e.loadedTypes[1] {
		return nil, &NotFoundError{label: filesmetric.Label}
	}
	return nil, &NotLoadedError{edge: "source_artifacts_read"}
}

// OutputArtifactsSeenOrErr returns the OutputArtifactsSeen value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e ArtifactMetricsEdges) OutputArtifactsSeenOrErr() (*FilesMetric, error) {
	if e.OutputArtifactsSeen != nil {
		return e.OutputArtifactsSeen, nil
	} else if e.loadedTypes[2] {
		return nil, &NotFoundError{label: filesmetric.Label}
	}
	return nil, &NotLoadedError{edge: "output_artifacts_seen"}
}

// OutputArtifactsFromActionCacheOrErr returns the OutputArtifactsFromActionCache value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e ArtifactMetricsEdges) OutputArtifactsFromActionCacheOrErr() (*FilesMetric, error) {
	if e.OutputArtifactsFromActionCache != nil {
		return e.OutputArtifactsFromActionCache, nil
	} else if e.loadedTypes[3] {
		return nil, &NotFoundError{label: filesmetric.Label}
	}
	return nil, &NotLoadedError{edge: "output_artifacts_from_action_cache"}
}

// TopLevelArtifactsOrErr returns the TopLevelArtifacts value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e ArtifactMetricsEdges) TopLevelArtifactsOrErr() (*FilesMetric, error) {
	if e.TopLevelArtifacts != nil {
		return e.TopLevelArtifacts, nil
	} else if e.loadedTypes[4] {
		return nil, &NotFoundError{label: filesmetric.Label}
	}
	return nil, &NotLoadedError{edge: "top_level_artifacts"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*ArtifactMetrics) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case artifactmetrics.FieldID:
			values[i] = new(sql.NullInt64)
		case artifactmetrics.ForeignKeys[0]: // artifact_metrics_source_artifacts_read
			values[i] = new(sql.NullInt64)
		case artifactmetrics.ForeignKeys[1]: // artifact_metrics_output_artifacts_seen
			values[i] = new(sql.NullInt64)
		case artifactmetrics.ForeignKeys[2]: // artifact_metrics_output_artifacts_from_action_cache
			values[i] = new(sql.NullInt64)
		case artifactmetrics.ForeignKeys[3]: // metrics_artifact_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 ArtifactMetrics fields.
func (am *ArtifactMetrics) 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 artifactmetrics.FieldID:
			value, ok := values[i].(*sql.NullInt64)
			if !ok {
				return fmt.Errorf("unexpected type %T for field id", value)
			}
			am.ID = int(value.Int64)
		case artifactmetrics.ForeignKeys[0]:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for edge-field artifact_metrics_source_artifacts_read", value)
			} else if value.Valid {
				am.artifact_metrics_source_artifacts_read = new(int)
				*am.artifact_metrics_source_artifacts_read = int(value.Int64)
			}
		case artifactmetrics.ForeignKeys[1]:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for edge-field artifact_metrics_output_artifacts_seen", value)
			} else if value.Valid {
				am.artifact_metrics_output_artifacts_seen = new(int)
				*am.artifact_metrics_output_artifacts_seen = int(value.Int64)
			}
		case artifactmetrics.ForeignKeys[2]:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for edge-field artifact_metrics_output_artifacts_from_action_cache", value)
			} else if value.Valid {
				am.artifact_metrics_output_artifacts_from_action_cache = new(int)
				*am.artifact_metrics_output_artifacts_from_action_cache = int(value.Int64)
			}
		case artifactmetrics.ForeignKeys[3]:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for edge-field metrics_artifact_metrics", value)
			} else if value.Valid {
				am.metrics_artifact_metrics = new(int)
				*am.metrics_artifact_metrics = int(value.Int64)
			}
		default:
			am.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryMetrics queries the "metrics" edge of the ArtifactMetrics entity.
func (am *ArtifactMetrics) QueryMetrics() *MetricsQuery {
	return NewArtifactMetricsClient(am.config).QueryMetrics(am)
}

// QuerySourceArtifactsRead queries the "source_artifacts_read" edge of the ArtifactMetrics entity.
func (am *ArtifactMetrics) QuerySourceArtifactsRead() *FilesMetricQuery {
	return NewArtifactMetricsClient(am.config).QuerySourceArtifactsRead(am)
}

// QueryOutputArtifactsSeen queries the "output_artifacts_seen" edge of the ArtifactMetrics entity.
func (am *ArtifactMetrics) QueryOutputArtifactsSeen() *FilesMetricQuery {
	return NewArtifactMetricsClient(am.config).QueryOutputArtifactsSeen(am)
}

// QueryOutputArtifactsFromActionCache queries the "output_artifacts_from_action_cache" edge of the ArtifactMetrics entity.
func (am *ArtifactMetrics) QueryOutputArtifactsFromActionCache() *FilesMetricQuery {
	return NewArtifactMetricsClient(am.config).QueryOutputArtifactsFromActionCache(am)
}

// QueryTopLevelArtifacts queries the "top_level_artifacts" edge of the ArtifactMetrics entity.
func (am *ArtifactMetrics) QueryTopLevelArtifacts() *FilesMetricQuery {
	return NewArtifactMetricsClient(am.config).QueryTopLevelArtifacts(am)
}

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

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

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

// ArtifactMetricsSlice is a parsable slice of ArtifactMetrics.
type ArtifactMetricsSlice []*ArtifactMetrics
