// Code generated by ent, DO NOT EDIT.

package ent

import (
	"encoding/json"
	"fmt"
	"strings"

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

// TestResultBES is the model entity for the TestResultBES schema.
type TestResultBES struct {
	config `json:"-"`
	// ID of the ent.
	ID int `json:"id,omitempty"`
	// TestStatus holds the value of the "test_status" field.
	TestStatus testresultbes.TestStatus `json:"test_status,omitempty"`
	// StatusDetails holds the value of the "status_details" field.
	StatusDetails string `json:"status_details,omitempty"`
	// Label holds the value of the "label" field.
	Label string `json:"label,omitempty"`
	// Warning holds the value of the "warning" field.
	Warning []string `json:"warning,omitempty"`
	// CachedLocally holds the value of the "cached_locally" field.
	CachedLocally bool `json:"cached_locally,omitempty"`
	// TestAttemptStartMillisEpoch holds the value of the "test_attempt_start_millis_epoch" field.
	TestAttemptStartMillisEpoch int64 `json:"test_attempt_start_millis_epoch,omitempty"`
	// TestAttemptStart holds the value of the "test_attempt_start" field.
	TestAttemptStart string `json:"test_attempt_start,omitempty"`
	// TestAttemptDurationMillis holds the value of the "test_attempt_duration_millis" field.
	TestAttemptDurationMillis int64 `json:"test_attempt_duration_millis,omitempty"`
	// TestAttemptDuration holds the value of the "test_attempt_duration" field.
	TestAttemptDuration int64 `json:"test_attempt_duration,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the TestResultBESQuery when eager-loading is set.
	Edges                        TestResultBESEdges `json:"edges"`
	test_collection_test_results *int
	selectValues                 sql.SelectValues
}

// TestResultBESEdges holds the relations/edges for other nodes in the graph.
type TestResultBESEdges struct {
	// TestCollection holds the value of the test_collection edge.
	TestCollection *TestCollection `json:"test_collection,omitempty"`
	// TestActionOutput holds the value of the test_action_output edge.
	TestActionOutput []*TestFile `json:"test_action_output,omitempty"`
	// ExecutionInfo holds the value of the execution_info edge.
	ExecutionInfo *ExectionInfo `json:"execution_info,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

	namedTestActionOutput 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 TestResultBESEdges) 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"}
}

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

// ExecutionInfoOrErr returns the ExecutionInfo value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e TestResultBESEdges) ExecutionInfoOrErr() (*ExectionInfo, error) {
	if e.ExecutionInfo != nil {
		return e.ExecutionInfo, nil
	} else if e.loadedTypes[2] {
		return nil, &NotFoundError{label: exectioninfo.Label}
	}
	return nil, &NotLoadedError{edge: "execution_info"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*TestResultBES) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case testresultbes.FieldWarning:
			values[i] = new([]byte)
		case testresultbes.FieldCachedLocally:
			values[i] = new(sql.NullBool)
		case testresultbes.FieldID, testresultbes.FieldTestAttemptStartMillisEpoch, testresultbes.FieldTestAttemptDurationMillis, testresultbes.FieldTestAttemptDuration:
			values[i] = new(sql.NullInt64)
		case testresultbes.FieldTestStatus, testresultbes.FieldStatusDetails, testresultbes.FieldLabel, testresultbes.FieldTestAttemptStart:
			values[i] = new(sql.NullString)
		case testresultbes.ForeignKeys[0]: // test_collection_test_results
			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 TestResultBES fields.
func (trb *TestResultBES) 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 testresultbes.FieldID:
			value, ok := values[i].(*sql.NullInt64)
			if !ok {
				return fmt.Errorf("unexpected type %T for field id", value)
			}
			trb.ID = int(value.Int64)
		case testresultbes.FieldTestStatus:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field test_status", values[i])
			} else if value.Valid {
				trb.TestStatus = testresultbes.TestStatus(value.String)
			}
		case testresultbes.FieldStatusDetails:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field status_details", values[i])
			} else if value.Valid {
				trb.StatusDetails = value.String
			}
		case testresultbes.FieldLabel:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field label", values[i])
			} else if value.Valid {
				trb.Label = value.String
			}
		case testresultbes.FieldWarning:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field warning", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &trb.Warning); err != nil {
					return fmt.Errorf("unmarshal field warning: %w", err)
				}
			}
		case testresultbes.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 {
				trb.CachedLocally = value.Bool
			}
		case testresultbes.FieldTestAttemptStartMillisEpoch:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field test_attempt_start_millis_epoch", values[i])
			} else if value.Valid {
				trb.TestAttemptStartMillisEpoch = value.Int64
			}
		case testresultbes.FieldTestAttemptStart:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field test_attempt_start", values[i])
			} else if value.Valid {
				trb.TestAttemptStart = value.String
			}
		case testresultbes.FieldTestAttemptDurationMillis:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field test_attempt_duration_millis", values[i])
			} else if value.Valid {
				trb.TestAttemptDurationMillis = value.Int64
			}
		case testresultbes.FieldTestAttemptDuration:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field test_attempt_duration", values[i])
			} else if value.Valid {
				trb.TestAttemptDuration = value.Int64
			}
		case testresultbes.ForeignKeys[0]:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for edge-field test_collection_test_results", value)
			} else if value.Valid {
				trb.test_collection_test_results = new(int)
				*trb.test_collection_test_results = int(value.Int64)
			}
		default:
			trb.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryTestCollection queries the "test_collection" edge of the TestResultBES entity.
func (trb *TestResultBES) QueryTestCollection() *TestCollectionQuery {
	return NewTestResultBESClient(trb.config).QueryTestCollection(trb)
}

// QueryTestActionOutput queries the "test_action_output" edge of the TestResultBES entity.
func (trb *TestResultBES) QueryTestActionOutput() *TestFileQuery {
	return NewTestResultBESClient(trb.config).QueryTestActionOutput(trb)
}

// QueryExecutionInfo queries the "execution_info" edge of the TestResultBES entity.
func (trb *TestResultBES) QueryExecutionInfo() *ExectionInfoQuery {
	return NewTestResultBESClient(trb.config).QueryExecutionInfo(trb)
}

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

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

// String implements the fmt.Stringer.
func (trb *TestResultBES) String() string {
	var builder strings.Builder
	builder.WriteString("TestResultBES(")
	builder.WriteString(fmt.Sprintf("id=%v, ", trb.ID))
	builder.WriteString("test_status=")
	builder.WriteString(fmt.Sprintf("%v", trb.TestStatus))
	builder.WriteString(", ")
	builder.WriteString("status_details=")
	builder.WriteString(trb.StatusDetails)
	builder.WriteString(", ")
	builder.WriteString("label=")
	builder.WriteString(trb.Label)
	builder.WriteString(", ")
	builder.WriteString("warning=")
	builder.WriteString(fmt.Sprintf("%v", trb.Warning))
	builder.WriteString(", ")
	builder.WriteString("cached_locally=")
	builder.WriteString(fmt.Sprintf("%v", trb.CachedLocally))
	builder.WriteString(", ")
	builder.WriteString("test_attempt_start_millis_epoch=")
	builder.WriteString(fmt.Sprintf("%v", trb.TestAttemptStartMillisEpoch))
	builder.WriteString(", ")
	builder.WriteString("test_attempt_start=")
	builder.WriteString(trb.TestAttemptStart)
	builder.WriteString(", ")
	builder.WriteString("test_attempt_duration_millis=")
	builder.WriteString(fmt.Sprintf("%v", trb.TestAttemptDurationMillis))
	builder.WriteString(", ")
	builder.WriteString("test_attempt_duration=")
	builder.WriteString(fmt.Sprintf("%v", trb.TestAttemptDuration))
	builder.WriteByte(')')
	return builder.String()
}

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

func (trb *TestResultBES) appendNamedTestActionOutput(name string, edges ...*TestFile) {
	if trb.Edges.namedTestActionOutput == nil {
		trb.Edges.namedTestActionOutput = make(map[string][]*TestFile)
	}
	if len(edges) == 0 {
		trb.Edges.namedTestActionOutput[name] = []*TestFile{}
	} else {
		trb.Edges.namedTestActionOutput[name] = append(trb.Edges.namedTestActionOutput[name], edges...)
	}
}

// TestResultBESs is a parsable slice of TestResultBES.
type TestResultBESs []*TestResultBES
