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

// EvaluationStat is the model entity for the EvaluationStat schema.
type EvaluationStat struct {
	config `json:"-"`
	// ID of the ent.
	ID int `json:"id,omitempty"`
	// SkyfunctionName holds the value of the "skyfunction_name" field.
	SkyfunctionName string `json:"skyfunction_name,omitempty"`
	// Count holds the value of the "count" field.
	Count int64 `json:"count,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the EvaluationStatQuery when eager-loading is set.
	Edges                                EvaluationStatEdges `json:"edges"`
	build_graph_metrics_evaluated_values *int
	selectValues                         sql.SelectValues
}

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

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

// scanValues returns the types for scanning values from sql.Rows.
func (*EvaluationStat) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case evaluationstat.FieldID, evaluationstat.FieldCount:
			values[i] = new(sql.NullInt64)
		case evaluationstat.FieldSkyfunctionName:
			values[i] = new(sql.NullString)
		case evaluationstat.ForeignKeys[0]: // build_graph_metrics_evaluated_values
			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 EvaluationStat fields.
func (es *EvaluationStat) 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 evaluationstat.FieldID:
			value, ok := values[i].(*sql.NullInt64)
			if !ok {
				return fmt.Errorf("unexpected type %T for field id", value)
			}
			es.ID = int(value.Int64)
		case evaluationstat.FieldSkyfunctionName:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field skyfunction_name", values[i])
			} else if value.Valid {
				es.SkyfunctionName = value.String
			}
		case evaluationstat.FieldCount:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field count", values[i])
			} else if value.Valid {
				es.Count = value.Int64
			}
		case evaluationstat.ForeignKeys[0]:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for edge-field build_graph_metrics_evaluated_values", value)
			} else if value.Valid {
				es.build_graph_metrics_evaluated_values = new(int)
				*es.build_graph_metrics_evaluated_values = int(value.Int64)
			}
		default:
			es.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryBuildGraphMetrics queries the "build_graph_metrics" edge of the EvaluationStat entity.
func (es *EvaluationStat) QueryBuildGraphMetrics() *BuildGraphMetricsQuery {
	return NewEvaluationStatClient(es.config).QueryBuildGraphMetrics(es)
}

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

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

// String implements the fmt.Stringer.
func (es *EvaluationStat) String() string {
	var builder strings.Builder
	builder.WriteString("EvaluationStat(")
	builder.WriteString(fmt.Sprintf("id=%v, ", es.ID))
	builder.WriteString("skyfunction_name=")
	builder.WriteString(es.SkyfunctionName)
	builder.WriteString(", ")
	builder.WriteString("count=")
	builder.WriteString(fmt.Sprintf("%v", es.Count))
	builder.WriteByte(')')
	return builder.String()
}

// EvaluationStats is a parsable slice of EvaluationStat.
type EvaluationStats []*EvaluationStat
