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

// FilesMetric is the model entity for the FilesMetric schema.
type FilesMetric struct {
	config `json:"-"`
	// ID of the ent.
	ID int `json:"id,omitempty"`
	// SizeInBytes holds the value of the "size_in_bytes" field.
	SizeInBytes int64 `json:"size_in_bytes,omitempty"`
	// Count holds the value of the "count" field.
	Count int32 `json:"count,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the FilesMetricQuery when eager-loading is set.
	Edges                                FilesMetricEdges `json:"edges"`
	artifact_metrics_top_level_artifacts *int
	selectValues                         sql.SelectValues
}

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

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

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

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

// QueryArtifactMetrics queries the "artifact_metrics" edge of the FilesMetric entity.
func (fm *FilesMetric) QueryArtifactMetrics() *ArtifactMetricsQuery {
	return NewFilesMetricClient(fm.config).QueryArtifactMetrics(fm)
}

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

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

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

// FilesMetrics is a parsable slice of FilesMetric.
type FilesMetrics []*FilesMetric
