// Code generated by ent, DO NOT EDIT.

package ent

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

	"entgo.io/ent"
	"entgo.io/ent/dialect/sql"
	"github.com/buildbarn/bb-portal/ent/gen/ent/build"
	"github.com/google/uuid"
)

// Build is the model entity for the Build schema.
type Build struct {
	config `json:"-"`
	// ID of the ent.
	ID int `json:"id,omitempty"`
	// BuildURL holds the value of the "build_url" field.
	BuildURL string `json:"build_url,omitempty"`
	// BuildUUID holds the value of the "build_uuid" field.
	BuildUUID uuid.UUID `json:"build_uuid,omitempty"`
	// Env holds the value of the "env" field.
	Env map[string]string `json:"env,omitempty"`
	// Timestamp holds the value of the "timestamp" field.
	Timestamp time.Time `json:"timestamp,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the BuildQuery when eager-loading is set.
	Edges        BuildEdges `json:"edges"`
	selectValues sql.SelectValues
}

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

	namedInvocations map[string][]*BazelInvocation
}

// InvocationsOrErr returns the Invocations value or an error if the edge
// was not loaded in eager-loading.
func (e BuildEdges) InvocationsOrErr() ([]*BazelInvocation, error) {
	if e.loadedTypes[0] {
		return e.Invocations, nil
	}
	return nil, &NotLoadedError{edge: "invocations"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*Build) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case build.FieldEnv:
			values[i] = new([]byte)
		case build.FieldID:
			values[i] = new(sql.NullInt64)
		case build.FieldBuildURL:
			values[i] = new(sql.NullString)
		case build.FieldTimestamp:
			values[i] = new(sql.NullTime)
		case build.FieldBuildUUID:
			values[i] = new(uuid.UUID)
		default:
			values[i] = new(sql.UnknownType)
		}
	}
	return values, nil
}

// assignValues assigns the values that were returned from sql.Rows (after scanning)
// to the Build fields.
func (b *Build) 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 build.FieldID:
			value, ok := values[i].(*sql.NullInt64)
			if !ok {
				return fmt.Errorf("unexpected type %T for field id", value)
			}
			b.ID = int(value.Int64)
		case build.FieldBuildURL:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field build_url", values[i])
			} else if value.Valid {
				b.BuildURL = value.String
			}
		case build.FieldBuildUUID:
			if value, ok := values[i].(*uuid.UUID); !ok {
				return fmt.Errorf("unexpected type %T for field build_uuid", values[i])
			} else if value != nil {
				b.BuildUUID = *value
			}
		case build.FieldEnv:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field env", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &b.Env); err != nil {
					return fmt.Errorf("unmarshal field env: %w", err)
				}
			}
		case build.FieldTimestamp:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field timestamp", values[i])
			} else if value.Valid {
				b.Timestamp = value.Time
			}
		default:
			b.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryInvocations queries the "invocations" edge of the Build entity.
func (b *Build) QueryInvocations() *BazelInvocationQuery {
	return NewBuildClient(b.config).QueryInvocations(b)
}

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

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

// String implements the fmt.Stringer.
func (b *Build) String() string {
	var builder strings.Builder
	builder.WriteString("Build(")
	builder.WriteString(fmt.Sprintf("id=%v, ", b.ID))
	builder.WriteString("build_url=")
	builder.WriteString(b.BuildURL)
	builder.WriteString(", ")
	builder.WriteString("build_uuid=")
	builder.WriteString(fmt.Sprintf("%v", b.BuildUUID))
	builder.WriteString(", ")
	builder.WriteString("env=")
	builder.WriteString(fmt.Sprintf("%v", b.Env))
	builder.WriteString(", ")
	builder.WriteString("timestamp=")
	builder.WriteString(b.Timestamp.Format(time.ANSIC))
	builder.WriteByte(')')
	return builder.String()
}

// NamedInvocations returns the Invocations named value or an error if the edge was not
// loaded in eager-loading with this name.
func (b *Build) NamedInvocations(name string) ([]*BazelInvocation, error) {
	if b.Edges.namedInvocations == nil {
		return nil, &NotLoadedError{edge: name}
	}
	nodes, ok := b.Edges.namedInvocations[name]
	if !ok {
		return nil, &NotLoadedError{edge: name}
	}
	return nodes, nil
}

func (b *Build) appendNamedInvocations(name string, edges ...*BazelInvocation) {
	if b.Edges.namedInvocations == nil {
		b.Edges.namedInvocations = make(map[string][]*BazelInvocation)
	}
	if len(edges) == 0 {
		b.Edges.namedInvocations[name] = []*BazelInvocation{}
	} else {
		b.Edges.namedInvocations[name] = append(b.Edges.namedInvocations[name], edges...)
	}
}

// Builds is a parsable slice of Build.
type Builds []*Build
