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

// ActionSummary is the model entity for the ActionSummary schema.
type ActionSummary struct {
	config `json:"-"`
	// ID of the ent.
	ID int `json:"id,omitempty"`
	// ActionsCreated holds the value of the "actions_created" field.
	ActionsCreated int64 `json:"actions_created,omitempty"`
	// ActionsCreatedNotIncludingAspects holds the value of the "actions_created_not_including_aspects" field.
	ActionsCreatedNotIncludingAspects int64 `json:"actions_created_not_including_aspects,omitempty"`
	// ActionsExecuted holds the value of the "actions_executed" field.
	ActionsExecuted int64 `json:"actions_executed,omitempty"`
	// RemoteCacheHits holds the value of the "remote_cache_hits" field.
	RemoteCacheHits int64 `json:"remote_cache_hits,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the ActionSummaryQuery when eager-loading is set.
	Edges                  ActionSummaryEdges `json:"edges"`
	metrics_action_summary *int
	selectValues           sql.SelectValues
}

// ActionSummaryEdges holds the relations/edges for other nodes in the graph.
type ActionSummaryEdges struct {
	// Metrics holds the value of the metrics edge.
	Metrics *Metrics `json:"metrics,omitempty"`
	// ActionData holds the value of the action_data edge.
	ActionData []*ActionData `json:"action_data,omitempty"`
	// RunnerCount holds the value of the runner_count edge.
	RunnerCount []*RunnerCount `json:"runner_count,omitempty"`
	// ActionCacheStatistics holds the value of the action_cache_statistics edge.
	ActionCacheStatistics *ActionCacheStatistics `json:"action_cache_statistics,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [4]bool
	// totalCount holds the count of the edges above.
	totalCount [4]map[string]int

	namedActionData  map[string][]*ActionData
	namedRunnerCount map[string][]*RunnerCount
}

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

// ActionDataOrErr returns the ActionData value or an error if the edge
// was not loaded in eager-loading.
func (e ActionSummaryEdges) ActionDataOrErr() ([]*ActionData, error) {
	if e.loadedTypes[1] {
		return e.ActionData, nil
	}
	return nil, &NotLoadedError{edge: "action_data"}
}

// RunnerCountOrErr returns the RunnerCount value or an error if the edge
// was not loaded in eager-loading.
func (e ActionSummaryEdges) RunnerCountOrErr() ([]*RunnerCount, error) {
	if e.loadedTypes[2] {
		return e.RunnerCount, nil
	}
	return nil, &NotLoadedError{edge: "runner_count"}
}

// ActionCacheStatisticsOrErr returns the ActionCacheStatistics value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e ActionSummaryEdges) ActionCacheStatisticsOrErr() (*ActionCacheStatistics, error) {
	if e.ActionCacheStatistics != nil {
		return e.ActionCacheStatistics, nil
	} else if e.loadedTypes[3] {
		return nil, &NotFoundError{label: actioncachestatistics.Label}
	}
	return nil, &NotLoadedError{edge: "action_cache_statistics"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*ActionSummary) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case actionsummary.FieldID, actionsummary.FieldActionsCreated, actionsummary.FieldActionsCreatedNotIncludingAspects, actionsummary.FieldActionsExecuted, actionsummary.FieldRemoteCacheHits:
			values[i] = new(sql.NullInt64)
		case actionsummary.ForeignKeys[0]: // metrics_action_summary
			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 ActionSummary fields.
func (as *ActionSummary) 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 actionsummary.FieldID:
			value, ok := values[i].(*sql.NullInt64)
			if !ok {
				return fmt.Errorf("unexpected type %T for field id", value)
			}
			as.ID = int(value.Int64)
		case actionsummary.FieldActionsCreated:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field actions_created", values[i])
			} else if value.Valid {
				as.ActionsCreated = value.Int64
			}
		case actionsummary.FieldActionsCreatedNotIncludingAspects:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field actions_created_not_including_aspects", values[i])
			} else if value.Valid {
				as.ActionsCreatedNotIncludingAspects = value.Int64
			}
		case actionsummary.FieldActionsExecuted:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field actions_executed", values[i])
			} else if value.Valid {
				as.ActionsExecuted = value.Int64
			}
		case actionsummary.FieldRemoteCacheHits:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field remote_cache_hits", values[i])
			} else if value.Valid {
				as.RemoteCacheHits = value.Int64
			}
		case actionsummary.ForeignKeys[0]:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for edge-field metrics_action_summary", value)
			} else if value.Valid {
				as.metrics_action_summary = new(int)
				*as.metrics_action_summary = int(value.Int64)
			}
		default:
			as.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryMetrics queries the "metrics" edge of the ActionSummary entity.
func (as *ActionSummary) QueryMetrics() *MetricsQuery {
	return NewActionSummaryClient(as.config).QueryMetrics(as)
}

// QueryActionData queries the "action_data" edge of the ActionSummary entity.
func (as *ActionSummary) QueryActionData() *ActionDataQuery {
	return NewActionSummaryClient(as.config).QueryActionData(as)
}

// QueryRunnerCount queries the "runner_count" edge of the ActionSummary entity.
func (as *ActionSummary) QueryRunnerCount() *RunnerCountQuery {
	return NewActionSummaryClient(as.config).QueryRunnerCount(as)
}

// QueryActionCacheStatistics queries the "action_cache_statistics" edge of the ActionSummary entity.
func (as *ActionSummary) QueryActionCacheStatistics() *ActionCacheStatisticsQuery {
	return NewActionSummaryClient(as.config).QueryActionCacheStatistics(as)
}

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

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

// String implements the fmt.Stringer.
func (as *ActionSummary) String() string {
	var builder strings.Builder
	builder.WriteString("ActionSummary(")
	builder.WriteString(fmt.Sprintf("id=%v, ", as.ID))
	builder.WriteString("actions_created=")
	builder.WriteString(fmt.Sprintf("%v", as.ActionsCreated))
	builder.WriteString(", ")
	builder.WriteString("actions_created_not_including_aspects=")
	builder.WriteString(fmt.Sprintf("%v", as.ActionsCreatedNotIncludingAspects))
	builder.WriteString(", ")
	builder.WriteString("actions_executed=")
	builder.WriteString(fmt.Sprintf("%v", as.ActionsExecuted))
	builder.WriteString(", ")
	builder.WriteString("remote_cache_hits=")
	builder.WriteString(fmt.Sprintf("%v", as.RemoteCacheHits))
	builder.WriteByte(')')
	return builder.String()
}

// NamedActionData returns the ActionData named value or an error if the edge was not
// loaded in eager-loading with this name.
func (as *ActionSummary) NamedActionData(name string) ([]*ActionData, error) {
	if as.Edges.namedActionData == nil {
		return nil, &NotLoadedError{edge: name}
	}
	nodes, ok := as.Edges.namedActionData[name]
	if !ok {
		return nil, &NotLoadedError{edge: name}
	}
	return nodes, nil
}

func (as *ActionSummary) appendNamedActionData(name string, edges ...*ActionData) {
	if as.Edges.namedActionData == nil {
		as.Edges.namedActionData = make(map[string][]*ActionData)
	}
	if len(edges) == 0 {
		as.Edges.namedActionData[name] = []*ActionData{}
	} else {
		as.Edges.namedActionData[name] = append(as.Edges.namedActionData[name], edges...)
	}
}

// NamedRunnerCount returns the RunnerCount named value or an error if the edge was not
// loaded in eager-loading with this name.
func (as *ActionSummary) NamedRunnerCount(name string) ([]*RunnerCount, error) {
	if as.Edges.namedRunnerCount == nil {
		return nil, &NotLoadedError{edge: name}
	}
	nodes, ok := as.Edges.namedRunnerCount[name]
	if !ok {
		return nil, &NotLoadedError{edge: name}
	}
	return nodes, nil
}

func (as *ActionSummary) appendNamedRunnerCount(name string, edges ...*RunnerCount) {
	if as.Edges.namedRunnerCount == nil {
		as.Edges.namedRunnerCount = make(map[string][]*RunnerCount)
	}
	if len(edges) == 0 {
		as.Edges.namedRunnerCount[name] = []*RunnerCount{}
	} else {
		as.Edges.namedRunnerCount[name] = append(as.Edges.namedRunnerCount[name], edges...)
	}
}

// ActionSummaries is a parsable slice of ActionSummary.
type ActionSummaries []*ActionSummary
