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

// BazelInvocationProblem is the model entity for the BazelInvocationProblem schema.
type BazelInvocationProblem struct {
	config `json:"-"`
	// ID of the ent.
	ID int `json:"id,omitempty"`
	// ProblemType holds the value of the "problem_type" field.
	ProblemType string `json:"problem_type,omitempty"`
	// Label holds the value of the "label" field.
	Label string `json:"label,omitempty"`
	// BepEvents holds the value of the "bep_events" field.
	BepEvents json.RawMessage `json:"bep_events,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the BazelInvocationProblemQuery when eager-loading is set.
	Edges                     BazelInvocationProblemEdges `json:"edges"`
	bazel_invocation_problems *int
	selectValues              sql.SelectValues
}

// BazelInvocationProblemEdges holds the relations/edges for other nodes in the graph.
type BazelInvocationProblemEdges struct {
	// BazelInvocation holds the value of the bazel_invocation edge.
	BazelInvocation *BazelInvocation `json:"bazel_invocation,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
}

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

// scanValues returns the types for scanning values from sql.Rows.
func (*BazelInvocationProblem) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case bazelinvocationproblem.FieldBepEvents:
			values[i] = new([]byte)
		case bazelinvocationproblem.FieldID:
			values[i] = new(sql.NullInt64)
		case bazelinvocationproblem.FieldProblemType, bazelinvocationproblem.FieldLabel:
			values[i] = new(sql.NullString)
		case bazelinvocationproblem.ForeignKeys[0]: // bazel_invocation_problems
			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 BazelInvocationProblem fields.
func (bip *BazelInvocationProblem) 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 bazelinvocationproblem.FieldID:
			value, ok := values[i].(*sql.NullInt64)
			if !ok {
				return fmt.Errorf("unexpected type %T for field id", value)
			}
			bip.ID = int(value.Int64)
		case bazelinvocationproblem.FieldProblemType:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field problem_type", values[i])
			} else if value.Valid {
				bip.ProblemType = value.String
			}
		case bazelinvocationproblem.FieldLabel:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field label", values[i])
			} else if value.Valid {
				bip.Label = value.String
			}
		case bazelinvocationproblem.FieldBepEvents:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field bep_events", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &bip.BepEvents); err != nil {
					return fmt.Errorf("unmarshal field bep_events: %w", err)
				}
			}
		case bazelinvocationproblem.ForeignKeys[0]:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for edge-field bazel_invocation_problems", value)
			} else if value.Valid {
				bip.bazel_invocation_problems = new(int)
				*bip.bazel_invocation_problems = int(value.Int64)
			}
		default:
			bip.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryBazelInvocation queries the "bazel_invocation" edge of the BazelInvocationProblem entity.
func (bip *BazelInvocationProblem) QueryBazelInvocation() *BazelInvocationQuery {
	return NewBazelInvocationProblemClient(bip.config).QueryBazelInvocation(bip)
}

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

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

// String implements the fmt.Stringer.
func (bip *BazelInvocationProblem) String() string {
	var builder strings.Builder
	builder.WriteString("BazelInvocationProblem(")
	builder.WriteString(fmt.Sprintf("id=%v, ", bip.ID))
	builder.WriteString("problem_type=")
	builder.WriteString(bip.ProblemType)
	builder.WriteString(", ")
	builder.WriteString("label=")
	builder.WriteString(bip.Label)
	builder.WriteString(", ")
	builder.WriteString("bep_events=")
	builder.WriteString(fmt.Sprintf("%v", bip.BepEvents))
	builder.WriteByte(')')
	return builder.String()
}

// BazelInvocationProblems is a parsable slice of BazelInvocationProblem.
type BazelInvocationProblems []*BazelInvocationProblem
