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

// TargetConfigured is the model entity for the TargetConfigured schema.
type TargetConfigured struct {
	config `json:"-"`
	// ID of the ent.
	ID int `json:"id,omitempty"`
	// Tag holds the value of the "tag" field.
	Tag []string `json:"tag,omitempty"`
	// TargetKind holds the value of the "target_kind" field.
	TargetKind string `json:"target_kind,omitempty"`
	// StartTimeInMs holds the value of the "start_time_in_ms" field.
	StartTimeInMs int64 `json:"start_time_in_ms,omitempty"`
	// TestSize holds the value of the "test_size" field.
	TestSize targetconfigured.TestSize `json:"test_size,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the TargetConfiguredQuery when eager-loading is set.
	Edges                     TargetConfiguredEdges `json:"edges"`
	target_pair_configuration *int
	selectValues              sql.SelectValues
}

// TargetConfiguredEdges holds the relations/edges for other nodes in the graph.
type TargetConfiguredEdges struct {
	// TargetPair holds the value of the target_pair edge.
	TargetPair *TargetPair `json:"target_pair,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [1]bool
	// totalCount holds the count of the edges above.
	totalCount [1]map[string]int
}

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

// scanValues returns the types for scanning values from sql.Rows.
func (*TargetConfigured) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case targetconfigured.FieldTag:
			values[i] = new([]byte)
		case targetconfigured.FieldID, targetconfigured.FieldStartTimeInMs:
			values[i] = new(sql.NullInt64)
		case targetconfigured.FieldTargetKind, targetconfigured.FieldTestSize:
			values[i] = new(sql.NullString)
		case targetconfigured.ForeignKeys[0]: // target_pair_configuration
			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 TargetConfigured fields.
func (tc *TargetConfigured) 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 targetconfigured.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 targetconfigured.FieldTag:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field tag", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &tc.Tag); err != nil {
					return fmt.Errorf("unmarshal field tag: %w", err)
				}
			}
		case targetconfigured.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 {
				tc.TargetKind = value.String
			}
		case targetconfigured.FieldStartTimeInMs:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field start_time_in_ms", values[i])
			} else if value.Valid {
				tc.StartTimeInMs = value.Int64
			}
		case targetconfigured.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 {
				tc.TestSize = targetconfigured.TestSize(value.String)
			}
		case targetconfigured.ForeignKeys[0]:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for edge-field target_pair_configuration", value)
			} else if value.Valid {
				tc.target_pair_configuration = new(int)
				*tc.target_pair_configuration = 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 TargetConfigured.
// This includes values selected through modifiers, order, etc.
func (tc *TargetConfigured) Value(name string) (ent.Value, error) {
	return tc.selectValues.Get(name)
}

// QueryTargetPair queries the "target_pair" edge of the TargetConfigured entity.
func (tc *TargetConfigured) QueryTargetPair() *TargetPairQuery {
	return NewTargetConfiguredClient(tc.config).QueryTargetPair(tc)
}

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

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

// String implements the fmt.Stringer.
func (tc *TargetConfigured) String() string {
	var builder strings.Builder
	builder.WriteString("TargetConfigured(")
	builder.WriteString(fmt.Sprintf("id=%v, ", tc.ID))
	builder.WriteString("tag=")
	builder.WriteString(fmt.Sprintf("%v", tc.Tag))
	builder.WriteString(", ")
	builder.WriteString("target_kind=")
	builder.WriteString(tc.TargetKind)
	builder.WriteString(", ")
	builder.WriteString("start_time_in_ms=")
	builder.WriteString(fmt.Sprintf("%v", tc.StartTimeInMs))
	builder.WriteString(", ")
	builder.WriteString("test_size=")
	builder.WriteString(fmt.Sprintf("%v", tc.TestSize))
	builder.WriteByte(')')
	return builder.String()
}

// TargetConfigureds is a parsable slice of TargetConfigured.
type TargetConfigureds []*TargetConfigured
