// Code generated by ent, DO NOT EDIT.

package ent

import (
	"fmt"
	"strings"
	"time"

	"entgo.io/ent"
	"entgo.io/ent/dialect/sql"
	"github.com/buildbarn/bb-portal/ent/gen/ent/bazelinvocation"
	"github.com/buildbarn/bb-portal/ent/gen/ent/testcollection"
	"github.com/buildbarn/bb-portal/ent/gen/ent/testsummary"
)

// TestCollection is the model entity for the TestCollection schema.
type TestCollection struct {
	config `json:"-"`
	// ID of the ent.
	ID int `json:"id,omitempty"`
	// Label holds the value of the "label" field.
	Label string `json:"label,omitempty"`
	// OverallStatus holds the value of the "overall_status" field.
	OverallStatus testcollection.OverallStatus `json:"overall_status,omitempty"`
	// Strategy holds the value of the "strategy" field.
	Strategy string `json:"strategy,omitempty"`
	// CachedLocally holds the value of the "cached_locally" field.
	CachedLocally bool `json:"cached_locally,omitempty"`
	// CachedRemotely holds the value of the "cached_remotely" field.
	CachedRemotely bool `json:"cached_remotely,omitempty"`
	// FirstSeen holds the value of the "first_seen" field.
	FirstSeen *time.Time `json:"first_seen,omitempty"`
	// DurationMs holds the value of the "duration_ms" field.
	DurationMs int64 `json:"duration_ms,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the TestCollectionQuery when eager-loading is set.
	Edges                            TestCollectionEdges `json:"edges"`
	bazel_invocation_test_collection *int
	selectValues                     sql.SelectValues
}

// TestCollectionEdges holds the relations/edges for other nodes in the graph.
type TestCollectionEdges struct {
	// BazelInvocation holds the value of the bazel_invocation edge.
	BazelInvocation *BazelInvocation `json:"bazel_invocation,omitempty"`
	// TestSummary holds the value of the test_summary edge.
	TestSummary *TestSummary `json:"test_summary,omitempty"`
	// TestResults holds the value of the test_results edge.
	TestResults []*TestResultBES `json:"test_results,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

	namedTestResults map[string][]*TestResultBES
}

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

// TestSummaryOrErr returns the TestSummary value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e TestCollectionEdges) TestSummaryOrErr() (*TestSummary, error) {
	if e.TestSummary != nil {
		return e.TestSummary, nil
	} else if e.loadedTypes[1] {
		return nil, &NotFoundError{label: testsummary.Label}
	}
	return nil, &NotLoadedError{edge: "test_summary"}
}

// TestResultsOrErr returns the TestResults value or an error if the edge
// was not loaded in eager-loading.
func (e TestCollectionEdges) TestResultsOrErr() ([]*TestResultBES, error) {
	if e.loadedTypes[2] {
		return e.TestResults, nil
	}
	return nil, &NotLoadedError{edge: "test_results"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*TestCollection) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case testcollection.FieldCachedLocally, testcollection.FieldCachedRemotely:
			values[i] = new(sql.NullBool)
		case testcollection.FieldID, testcollection.FieldDurationMs:
			values[i] = new(sql.NullInt64)
		case testcollection.FieldLabel, testcollection.FieldOverallStatus, testcollection.FieldStrategy:
			values[i] = new(sql.NullString)
		case testcollection.FieldFirstSeen:
			values[i] = new(sql.NullTime)
		case testcollection.ForeignKeys[0]: // bazel_invocation_test_collection
			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 TestCollection fields.
func (tc *TestCollection) 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 testcollection.FieldID:
			value, ok := values[i].(*sql.NullInt64)
			if !ok {
				return fmt.Errorf("unexpected type %T for field id", value)
			}
			tc.ID = int(value.Int64)
		case testcollection.FieldLabel:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field label", values[i])
			} else if value.Valid {
				tc.Label = value.String
			}
		case testcollection.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 {
				tc.OverallStatus = testcollection.OverallStatus(value.String)
			}
		case testcollection.FieldStrategy:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field strategy", values[i])
			} else if value.Valid {
				tc.Strategy = value.String
			}
		case testcollection.FieldCachedLocally:
			if value, ok := values[i].(*sql.NullBool); !ok {
				return fmt.Errorf("unexpected type %T for field cached_locally", values[i])
			} else if value.Valid {
				tc.CachedLocally = value.Bool
			}
		case testcollection.FieldCachedRemotely:
			if value, ok := values[i].(*sql.NullBool); !ok {
				return fmt.Errorf("unexpected type %T for field cached_remotely", values[i])
			} else if value.Valid {
				tc.CachedRemotely = value.Bool
			}
		case testcollection.FieldFirstSeen:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field first_seen", values[i])
			} else if value.Valid {
				tc.FirstSeen = new(time.Time)
				*tc.FirstSeen = value.Time
			}
		case testcollection.FieldDurationMs:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field duration_ms", values[i])
			} else if value.Valid {
				tc.DurationMs = value.Int64
			}
		case testcollection.ForeignKeys[0]:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for edge-field bazel_invocation_test_collection", value)
			} else if value.Valid {
				tc.bazel_invocation_test_collection = new(int)
				*tc.bazel_invocation_test_collection = int(value.Int64)
			}
		default:
			tc.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryBazelInvocation queries the "bazel_invocation" edge of the TestCollection entity.
func (tc *TestCollection) QueryBazelInvocation() *BazelInvocationQuery {
	return NewTestCollectionClient(tc.config).QueryBazelInvocation(tc)
}

// QueryTestSummary queries the "test_summary" edge of the TestCollection entity.
func (tc *TestCollection) QueryTestSummary() *TestSummaryQuery {
	return NewTestCollectionClient(tc.config).QueryTestSummary(tc)
}

// QueryTestResults queries the "test_results" edge of the TestCollection entity.
func (tc *TestCollection) QueryTestResults() *TestResultBESQuery {
	return NewTestCollectionClient(tc.config).QueryTestResults(tc)
}

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

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

// String implements the fmt.Stringer.
func (tc *TestCollection) String() string {
	var builder strings.Builder
	builder.WriteString("TestCollection(")
	builder.WriteString(fmt.Sprintf("id=%v, ", tc.ID))
	builder.WriteString("label=")
	builder.WriteString(tc.Label)
	builder.WriteString(", ")
	builder.WriteString("overall_status=")
	builder.WriteString(fmt.Sprintf("%v", tc.OverallStatus))
	builder.WriteString(", ")
	builder.WriteString("strategy=")
	builder.WriteString(tc.Strategy)
	builder.WriteString(", ")
	builder.WriteString("cached_locally=")
	builder.WriteString(fmt.Sprintf("%v", tc.CachedLocally))
	builder.WriteString(", ")
	builder.WriteString("cached_remotely=")
	builder.WriteString(fmt.Sprintf("%v", tc.CachedRemotely))
	builder.WriteString(", ")
	if v := tc.FirstSeen; v != nil {
		builder.WriteString("first_seen=")
		builder.WriteString(v.Format(time.ANSIC))
	}
	builder.WriteString(", ")
	builder.WriteString("duration_ms=")
	builder.WriteString(fmt.Sprintf("%v", tc.DurationMs))
	builder.WriteByte(')')
	return builder.String()
}

// NamedTestResults returns the TestResults named value or an error if the edge was not
// loaded in eager-loading with this name.
func (tc *TestCollection) NamedTestResults(name string) ([]*TestResultBES, error) {
	if tc.Edges.namedTestResults == nil {
		return nil, &NotLoadedError{edge: name}
	}
	nodes, ok := tc.Edges.namedTestResults[name]
	if !ok {
		return nil, &NotLoadedError{edge: name}
	}
	return nodes, nil
}

func (tc *TestCollection) appendNamedTestResults(name string, edges ...*TestResultBES) {
	if tc.Edges.namedTestResults == nil {
		tc.Edges.namedTestResults = make(map[string][]*TestResultBES)
	}
	if len(edges) == 0 {
		tc.Edges.namedTestResults[name] = []*TestResultBES{}
	} else {
		tc.Edges.namedTestResults[name] = append(tc.Edges.namedTestResults[name], edges...)
	}
}

// TestCollections is a parsable slice of TestCollection.
type TestCollections []*TestCollection
