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

// NamedSetOfFiles is the model entity for the NamedSetOfFiles schema.
type NamedSetOfFiles struct {
	config
	// ID of the ent.
	ID int `json:"id,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the NamedSetOfFilesQuery when eager-loading is set.
	Edges                        NamedSetOfFilesEdges `json:"edges"`
	named_set_of_files_file_sets *int
	output_group_file_sets       *int
	selectValues                 sql.SelectValues
}

// NamedSetOfFilesEdges holds the relations/edges for other nodes in the graph.
type NamedSetOfFilesEdges struct {
	// OutputGroup holds the value of the output_group edge.
	OutputGroup *OutputGroup `json:"output_group,omitempty"`
	// Files holds the value of the files edge.
	Files []*TestFile `json:"files,omitempty"`
	// FileSets holds the value of the file_sets edge.
	FileSets *NamedSetOfFiles `json:"file_sets,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

	namedFiles map[string][]*TestFile
}

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

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

// FileSetsOrErr returns the FileSets value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e NamedSetOfFilesEdges) FileSetsOrErr() (*NamedSetOfFiles, error) {
	if e.FileSets != nil {
		return e.FileSets, nil
	} else if e.loadedTypes[2] {
		return nil, &NotFoundError{label: namedsetoffiles.Label}
	}
	return nil, &NotLoadedError{edge: "file_sets"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*NamedSetOfFiles) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case namedsetoffiles.FieldID:
			values[i] = new(sql.NullInt64)
		case namedsetoffiles.ForeignKeys[0]: // named_set_of_files_file_sets
			values[i] = new(sql.NullInt64)
		case namedsetoffiles.ForeignKeys[1]: // output_group_file_sets
			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 NamedSetOfFiles fields.
func (nsof *NamedSetOfFiles) 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 namedsetoffiles.FieldID:
			value, ok := values[i].(*sql.NullInt64)
			if !ok {
				return fmt.Errorf("unexpected type %T for field id", value)
			}
			nsof.ID = int(value.Int64)
		case namedsetoffiles.ForeignKeys[0]:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for edge-field named_set_of_files_file_sets", value)
			} else if value.Valid {
				nsof.named_set_of_files_file_sets = new(int)
				*nsof.named_set_of_files_file_sets = int(value.Int64)
			}
		case namedsetoffiles.ForeignKeys[1]:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for edge-field output_group_file_sets", value)
			} else if value.Valid {
				nsof.output_group_file_sets = new(int)
				*nsof.output_group_file_sets = int(value.Int64)
			}
		default:
			nsof.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryOutputGroup queries the "output_group" edge of the NamedSetOfFiles entity.
func (nsof *NamedSetOfFiles) QueryOutputGroup() *OutputGroupQuery {
	return NewNamedSetOfFilesClient(nsof.config).QueryOutputGroup(nsof)
}

// QueryFiles queries the "files" edge of the NamedSetOfFiles entity.
func (nsof *NamedSetOfFiles) QueryFiles() *TestFileQuery {
	return NewNamedSetOfFilesClient(nsof.config).QueryFiles(nsof)
}

// QueryFileSets queries the "file_sets" edge of the NamedSetOfFiles entity.
func (nsof *NamedSetOfFiles) QueryFileSets() *NamedSetOfFilesQuery {
	return NewNamedSetOfFilesClient(nsof.config).QueryFileSets(nsof)
}

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

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

// String implements the fmt.Stringer.
func (nsof *NamedSetOfFiles) String() string {
	var builder strings.Builder
	builder.WriteString("NamedSetOfFiles(")
	builder.WriteString(fmt.Sprintf("id=%v", nsof.ID))
	builder.WriteByte(')')
	return builder.String()
}

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

func (nsof *NamedSetOfFiles) appendNamedFiles(name string, edges ...*TestFile) {
	if nsof.Edges.namedFiles == nil {
		nsof.Edges.namedFiles = make(map[string][]*TestFile)
	}
	if len(edges) == 0 {
		nsof.Edges.namedFiles[name] = []*TestFile{}
	} else {
		nsof.Edges.namedFiles[name] = append(nsof.Edges.namedFiles[name], edges...)
	}
}

// NamedSetOfFilesSlice is a parsable slice of NamedSetOfFiles.
type NamedSetOfFilesSlice []*NamedSetOfFiles
