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

// GarbageMetrics is the model entity for the GarbageMetrics schema.
type GarbageMetrics struct {
	config `json:"-"`
	// ID of the ent.
	ID int `json:"id,omitempty"`
	// Type holds the value of the "type" field.
	Type string `json:"type,omitempty"`
	// GarbageCollected holds the value of the "garbage_collected" field.
	GarbageCollected int64 `json:"garbage_collected,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the GarbageMetricsQuery when eager-loading is set.
	Edges                          GarbageMetricsEdges `json:"edges"`
	memory_metrics_garbage_metrics *int
	selectValues                   sql.SelectValues
}

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

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

// scanValues returns the types for scanning values from sql.Rows.
func (*GarbageMetrics) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case garbagemetrics.FieldID, garbagemetrics.FieldGarbageCollected:
			values[i] = new(sql.NullInt64)
		case garbagemetrics.FieldType:
			values[i] = new(sql.NullString)
		case garbagemetrics.ForeignKeys[0]: // memory_metrics_garbage_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 GarbageMetrics fields.
func (gm *GarbageMetrics) 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 garbagemetrics.FieldID:
			value, ok := values[i].(*sql.NullInt64)
			if !ok {
				return fmt.Errorf("unexpected type %T for field id", value)
			}
			gm.ID = int(value.Int64)
		case garbagemetrics.FieldType:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field type", values[i])
			} else if value.Valid {
				gm.Type = value.String
			}
		case garbagemetrics.FieldGarbageCollected:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field garbage_collected", values[i])
			} else if value.Valid {
				gm.GarbageCollected = value.Int64
			}
		case garbagemetrics.ForeignKeys[0]:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for edge-field memory_metrics_garbage_metrics", value)
			} else if value.Valid {
				gm.memory_metrics_garbage_metrics = new(int)
				*gm.memory_metrics_garbage_metrics = int(value.Int64)
			}
		default:
			gm.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryMemoryMetrics queries the "memory_metrics" edge of the GarbageMetrics entity.
func (gm *GarbageMetrics) QueryMemoryMetrics() *MemoryMetricsQuery {
	return NewGarbageMetricsClient(gm.config).QueryMemoryMetrics(gm)
}

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

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

// String implements the fmt.Stringer.
func (gm *GarbageMetrics) String() string {
	var builder strings.Builder
	builder.WriteString("GarbageMetrics(")
	builder.WriteString(fmt.Sprintf("id=%v, ", gm.ID))
	builder.WriteString("type=")
	builder.WriteString(gm.Type)
	builder.WriteString(", ")
	builder.WriteString("garbage_collected=")
	builder.WriteString(fmt.Sprintf("%v", gm.GarbageCollected))
	builder.WriteByte(')')
	return builder.String()
}

// GarbageMetricsSlice is a parsable slice of GarbageMetrics.
type GarbageMetricsSlice []*GarbageMetrics
