// 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/bazelinvocation"
	"github.com/buildbarn/bb-portal/ent/gen/ent/targetcomplete"
	"github.com/buildbarn/bb-portal/ent/gen/ent/targetconfigured"
	"github.com/buildbarn/bb-portal/ent/gen/ent/targetpair"
)

// TargetPair is the model entity for the TargetPair schema.
type TargetPair 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"`
	// DurationInMs holds the value of the "duration_in_ms" field.
	DurationInMs int64 `json:"duration_in_ms,omitempty"`
	// Success holds the value of the "success" field.
	Success bool `json:"success,omitempty"`
	// TargetKind holds the value of the "target_kind" field.
	TargetKind string `json:"target_kind,omitempty"`
	// TestSize holds the value of the "test_size" field.
	TestSize targetpair.TestSize `json:"test_size,omitempty"`
	// AbortReason holds the value of the "abort_reason" field.
	AbortReason targetpair.AbortReason `json:"abort_reason,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the TargetPairQuery when eager-loading is set.
	Edges                    TargetPairEdges `json:"edges"`
	bazel_invocation_targets *int
	selectValues             sql.SelectValues
}

// TargetPairEdges holds the relations/edges for other nodes in the graph.
type TargetPairEdges struct {
	// BazelInvocation holds the value of the bazel_invocation edge.
	BazelInvocation *BazelInvocation `json:"bazel_invocation,omitempty"`
	// Configuration holds the value of the configuration edge.
	Configuration *TargetConfigured `json:"configuration,omitempty"`
	// Completion holds the value of the completion edge.
	Completion *TargetComplete `json:"completion,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
}

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

// ConfigurationOrErr returns the Configuration value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e TargetPairEdges) ConfigurationOrErr() (*TargetConfigured, error) {
	if e.Configuration != nil {
		return e.Configuration, nil
	} else if e.loadedTypes[1] {
		return nil, &NotFoundError{label: targetconfigured.Label}
	}
	return nil, &NotLoadedError{edge: "configuration"}
}

// CompletionOrErr returns the Completion value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e TargetPairEdges) CompletionOrErr() (*TargetComplete, error) {
	if e.Completion != nil {
		return e.Completion, nil
	} else if e.loadedTypes[2] {
		return nil, &NotFoundError{label: targetcomplete.Label}
	}
	return nil, &NotLoadedError{edge: "completion"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*TargetPair) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case targetpair.FieldSuccess:
			values[i] = new(sql.NullBool)
		case targetpair.FieldID, targetpair.FieldDurationInMs:
			values[i] = new(sql.NullInt64)
		case targetpair.FieldLabel, targetpair.FieldTargetKind, targetpair.FieldTestSize, targetpair.FieldAbortReason:
			values[i] = new(sql.NullString)
		case targetpair.ForeignKeys[0]: // bazel_invocation_targets
			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 TargetPair fields.
func (tp *TargetPair) 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 targetpair.FieldID:
			value, ok := values[i].(*sql.NullInt64)
			if !ok {
				return fmt.Errorf("unexpected type %T for field id", value)
			}
			tp.ID = int(value.Int64)
		case targetpair.FieldLabel:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field label", values[i])
			} else if value.Valid {
				tp.Label = value.String
			}
		case targetpair.FieldDurationInMs:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field duration_in_ms", values[i])
			} else if value.Valid {
				tp.DurationInMs = value.Int64
			}
		case targetpair.FieldSuccess:
			if value, ok := values[i].(*sql.NullBool); !ok {
				return fmt.Errorf("unexpected type %T for field success", values[i])
			} else if value.Valid {
				tp.Success = value.Bool
			}
		case targetpair.FieldTargetKind:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field target_kind", values[i])
			} else if value.Valid {
				tp.TargetKind = value.String
			}
		case targetpair.FieldTestSize:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field test_size", values[i])
			} else if value.Valid {
				tp.TestSize = targetpair.TestSize(value.String)
			}
		case targetpair.FieldAbortReason:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field abort_reason", values[i])
			} else if value.Valid {
				tp.AbortReason = targetpair.AbortReason(value.String)
			}
		case targetpair.ForeignKeys[0]:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for edge-field bazel_invocation_targets", value)
			} else if value.Valid {
				tp.bazel_invocation_targets = new(int)
				*tp.bazel_invocation_targets = int(value.Int64)
			}
		default:
			tp.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryBazelInvocation queries the "bazel_invocation" edge of the TargetPair entity.
func (tp *TargetPair) QueryBazelInvocation() *BazelInvocationQuery {
	return NewTargetPairClient(tp.config).QueryBazelInvocation(tp)
}

// QueryConfiguration queries the "configuration" edge of the TargetPair entity.
func (tp *TargetPair) QueryConfiguration() *TargetConfiguredQuery {
	return NewTargetPairClient(tp.config).QueryConfiguration(tp)
}

// QueryCompletion queries the "completion" edge of the TargetPair entity.
func (tp *TargetPair) QueryCompletion() *TargetCompleteQuery {
	return NewTargetPairClient(tp.config).QueryCompletion(tp)
}

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

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

// String implements the fmt.Stringer.
func (tp *TargetPair) String() string {
	var builder strings.Builder
	builder.WriteString("TargetPair(")
	builder.WriteString(fmt.Sprintf("id=%v, ", tp.ID))
	builder.WriteString("label=")
	builder.WriteString(tp.Label)
	builder.WriteString(", ")
	builder.WriteString("duration_in_ms=")
	builder.WriteString(fmt.Sprintf("%v", tp.DurationInMs))
	builder.WriteString(", ")
	builder.WriteString("success=")
	builder.WriteString(fmt.Sprintf("%v", tp.Success))
	builder.WriteString(", ")
	builder.WriteString("target_kind=")
	builder.WriteString(tp.TargetKind)
	builder.WriteString(", ")
	builder.WriteString("test_size=")
	builder.WriteString(fmt.Sprintf("%v", tp.TestSize))
	builder.WriteString(", ")
	builder.WriteString("abort_reason=")
	builder.WriteString(fmt.Sprintf("%v", tp.AbortReason))
	builder.WriteByte(')')
	return builder.String()
}

// TargetPairs is a parsable slice of TargetPair.
type TargetPairs []*TargetPair
