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

// TestSummary is the model entity for the TestSummary schema.
type TestSummary struct {
	config `json:"-"`
	// ID of the ent.
	ID int `json:"id,omitempty"`
	// OverallStatus holds the value of the "overall_status" field.
	OverallStatus testsummary.OverallStatus `json:"overall_status,omitempty"`
	// TotalRunCount holds the value of the "total_run_count" field.
	TotalRunCount int32 `json:"total_run_count,omitempty"`
	// RunCount holds the value of the "run_count" field.
	RunCount int32 `json:"run_count,omitempty"`
	// AttemptCount holds the value of the "attempt_count" field.
	AttemptCount int32 `json:"attempt_count,omitempty"`
	// ShardCount holds the value of the "shard_count" field.
	ShardCount int32 `json:"shard_count,omitempty"`
	// TotalNumCached holds the value of the "total_num_cached" field.
	TotalNumCached int32 `json:"total_num_cached,omitempty"`
	// FirstStartTime holds the value of the "first_start_time" field.
	FirstStartTime int64 `json:"first_start_time,omitempty"`
	// LastStopTime holds the value of the "last_stop_time" field.
	LastStopTime int64 `json:"last_stop_time,omitempty"`
	// TotalRunDuration holds the value of the "total_run_duration" field.
	TotalRunDuration int64 `json:"total_run_duration,omitempty"`
	// Label holds the value of the "label" field.
	Label string `json:"label,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the TestSummaryQuery when eager-loading is set.
	Edges                        TestSummaryEdges `json:"edges"`
	test_collection_test_summary *int
	selectValues                 sql.SelectValues
}

// TestSummaryEdges holds the relations/edges for other nodes in the graph.
type TestSummaryEdges struct {
	// TestCollection holds the value of the test_collection edge.
	TestCollection *TestCollection `json:"test_collection,omitempty"`
	// Passed holds the value of the passed edge.
	Passed []*TestFile `json:"passed,omitempty"`
	// Failed holds the value of the failed edge.
	Failed []*TestFile `json:"failed,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [3]bool
	// totalCount holds the count of the edges above.
	totalCount [3]map[string]int

	namedPassed map[string][]*TestFile
	namedFailed map[string][]*TestFile
}

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

// PassedOrErr returns the Passed value or an error if the edge
// was not loaded in eager-loading.
func (e TestSummaryEdges) PassedOrErr() ([]*TestFile, error) {
	if e.loadedTypes[1] {
		return e.Passed, nil
	}
	return nil, &NotLoadedError{edge: "passed"}
}

// FailedOrErr returns the Failed value or an error if the edge
// was not loaded in eager-loading.
func (e TestSummaryEdges) FailedOrErr() ([]*TestFile, error) {
	if e.loadedTypes[2] {
		return e.Failed, nil
	}
	return nil, &NotLoadedError{edge: "failed"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*TestSummary) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case testsummary.FieldID, testsummary.FieldTotalRunCount, testsummary.FieldRunCount, testsummary.FieldAttemptCount, testsummary.FieldShardCount, testsummary.FieldTotalNumCached, testsummary.FieldFirstStartTime, testsummary.FieldLastStopTime, testsummary.FieldTotalRunDuration:
			values[i] = new(sql.NullInt64)
		case testsummary.FieldOverallStatus, testsummary.FieldLabel:
			values[i] = new(sql.NullString)
		case testsummary.ForeignKeys[0]: // test_collection_test_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 TestSummary fields.
func (ts *TestSummary) 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 testsummary.FieldID:
			value, ok := values[i].(*sql.NullInt64)
			if !ok {
				return fmt.Errorf("unexpected type %T for field id", value)
			}
			ts.ID = int(value.Int64)
		case testsummary.FieldOverallStatus:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field overall_status", values[i])
			} else if value.Valid {
				ts.OverallStatus = testsummary.OverallStatus(value.String)
			}
		case testsummary.FieldTotalRunCount:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field total_run_count", values[i])
			} else if value.Valid {
				ts.TotalRunCount = int32(value.Int64)
			}
		case testsummary.FieldRunCount:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field run_count", values[i])
			} else if value.Valid {
				ts.RunCount = int32(value.Int64)
			}
		case testsummary.FieldAttemptCount:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field attempt_count", values[i])
			} else if value.Valid {
				ts.AttemptCount = int32(value.Int64)
			}
		case testsummary.FieldShardCount:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field shard_count", values[i])
			} else if value.Valid {
				ts.ShardCount = int32(value.Int64)
			}
		case testsummary.FieldTotalNumCached:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field total_num_cached", values[i])
			} else if value.Valid {
				ts.TotalNumCached = int32(value.Int64)
			}
		case testsummary.FieldFirstStartTime:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field first_start_time", values[i])
			} else if value.Valid {
				ts.FirstStartTime = value.Int64
			}
		case testsummary.FieldLastStopTime:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field last_stop_time", values[i])
			} else if value.Valid {
				ts.LastStopTime = value.Int64
			}
		case testsummary.FieldTotalRunDuration:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field total_run_duration", values[i])
			} else if value.Valid {
				ts.TotalRunDuration = value.Int64
			}
		case testsummary.FieldLabel:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field label", values[i])
			} else if value.Valid {
				ts.Label = value.String
			}
		case testsummary.ForeignKeys[0]:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for edge-field test_collection_test_summary", value)
			} else if value.Valid {
				ts.test_collection_test_summary = new(int)
				*ts.test_collection_test_summary = int(value.Int64)
			}
		default:
			ts.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryTestCollection queries the "test_collection" edge of the TestSummary entity.
func (ts *TestSummary) QueryTestCollection() *TestCollectionQuery {
	return NewTestSummaryClient(ts.config).QueryTestCollection(ts)
}

// QueryPassed queries the "passed" edge of the TestSummary entity.
func (ts *TestSummary) QueryPassed() *TestFileQuery {
	return NewTestSummaryClient(ts.config).QueryPassed(ts)
}

// QueryFailed queries the "failed" edge of the TestSummary entity.
func (ts *TestSummary) QueryFailed() *TestFileQuery {
	return NewTestSummaryClient(ts.config).QueryFailed(ts)
}

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

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

// String implements the fmt.Stringer.
func (ts *TestSummary) String() string {
	var builder strings.Builder
	builder.WriteString("TestSummary(")
	builder.WriteString(fmt.Sprintf("id=%v, ", ts.ID))
	builder.WriteString("overall_status=")
	builder.WriteString(fmt.Sprintf("%v", ts.OverallStatus))
	builder.WriteString(", ")
	builder.WriteString("total_run_count=")
	builder.WriteString(fmt.Sprintf("%v", ts.TotalRunCount))
	builder.WriteString(", ")
	builder.WriteString("run_count=")
	builder.WriteString(fmt.Sprintf("%v", ts.RunCount))
	builder.WriteString(", ")
	builder.WriteString("attempt_count=")
	builder.WriteString(fmt.Sprintf("%v", ts.AttemptCount))
	builder.WriteString(", ")
	builder.WriteString("shard_count=")
	builder.WriteString(fmt.Sprintf("%v", ts.ShardCount))
	builder.WriteString(", ")
	builder.WriteString("total_num_cached=")
	builder.WriteString(fmt.Sprintf("%v", ts.TotalNumCached))
	builder.WriteString(", ")
	builder.WriteString("first_start_time=")
	builder.WriteString(fmt.Sprintf("%v", ts.FirstStartTime))
	builder.WriteString(", ")
	builder.WriteString("last_stop_time=")
	builder.WriteString(fmt.Sprintf("%v", ts.LastStopTime))
	builder.WriteString(", ")
	builder.WriteString("total_run_duration=")
	builder.WriteString(fmt.Sprintf("%v", ts.TotalRunDuration))
	builder.WriteString(", ")
	builder.WriteString("label=")
	builder.WriteString(ts.Label)
	builder.WriteByte(')')
	return builder.String()
}

// NamedPassed returns the Passed named value or an error if the edge was not
// loaded in eager-loading with this name.
func (ts *TestSummary) NamedPassed(name string) ([]*TestFile, error) {
	if ts.Edges.namedPassed == nil {
		return nil, &NotLoadedError{edge: name}
	}
	nodes, ok := ts.Edges.namedPassed[name]
	if !ok {
		return nil, &NotLoadedError{edge: name}
	}
	return nodes, nil
}

func (ts *TestSummary) appendNamedPassed(name string, edges ...*TestFile) {
	if ts.Edges.namedPassed == nil {
		ts.Edges.namedPassed = make(map[string][]*TestFile)
	}
	if len(edges) == 0 {
		ts.Edges.namedPassed[name] = []*TestFile{}
	} else {
		ts.Edges.namedPassed[name] = append(ts.Edges.namedPassed[name], edges...)
	}
}

// NamedFailed returns the Failed named value or an error if the edge was not
// loaded in eager-loading with this name.
func (ts *TestSummary) NamedFailed(name string) ([]*TestFile, error) {
	if ts.Edges.namedFailed == nil {
		return nil, &NotLoadedError{edge: name}
	}
	nodes, ok := ts.Edges.namedFailed[name]
	if !ok {
		return nil, &NotLoadedError{edge: name}
	}
	return nodes, nil
}

func (ts *TestSummary) appendNamedFailed(name string, edges ...*TestFile) {
	if ts.Edges.namedFailed == nil {
		ts.Edges.namedFailed = make(map[string][]*TestFile)
	}
	if len(edges) == 0 {
		ts.Edges.namedFailed[name] = []*TestFile{}
	} else {
		ts.Edges.namedFailed[name] = append(ts.Edges.namedFailed[name], edges...)
	}
}

// TestSummaries is a parsable slice of TestSummary.
type TestSummaries []*TestSummary
