// Code generated by ent, DO NOT EDIT.

package ent

import (
	"fmt"
	"strings"

	"entgo.io/ent"
	"entgo.io/ent/dialect/sql"
	"github.com/google/uuid"
	"github.com/in-toto/archivista/ent/attestationcollection"
	"github.com/in-toto/archivista/ent/attestationpolicy"
	"github.com/in-toto/archivista/ent/statement"
)

// Statement is the model entity for the Statement schema.
type Statement struct {
	config `json:"-"`
	// ID of the ent.
	ID uuid.UUID `json:"id,omitempty"`
	// Predicate holds the value of the "predicate" field.
	Predicate string `json:"predicate,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the StatementQuery when eager-loading is set.
	Edges        StatementEdges `json:"edges"`
	selectValues sql.SelectValues
}

// StatementEdges holds the relations/edges for other nodes in the graph.
type StatementEdges struct {
	// Subjects holds the value of the subjects edge.
	Subjects []*Subject `json:"subjects,omitempty"`
	// Policy holds the value of the policy edge.
	Policy *AttestationPolicy `json:"policy,omitempty"`
	// AttestationCollections holds the value of the attestation_collections edge.
	AttestationCollections *AttestationCollection `json:"attestation_collections,omitempty"`
	// Dsse holds the value of the dsse edge.
	Dsse []*Dsse `json:"dsse,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [4]bool
	// totalCount holds the count of the edges above.
	totalCount [4]map[string]int

	namedSubjects map[string][]*Subject
	namedDsse     map[string][]*Dsse
}

// SubjectsOrErr returns the Subjects value or an error if the edge
// was not loaded in eager-loading.
func (e StatementEdges) SubjectsOrErr() ([]*Subject, error) {
	if e.loadedTypes[0] {
		return e.Subjects, nil
	}
	return nil, &NotLoadedError{edge: "subjects"}
}

// PolicyOrErr returns the Policy value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e StatementEdges) PolicyOrErr() (*AttestationPolicy, error) {
	if e.Policy != nil {
		return e.Policy, nil
	} else if e.loadedTypes[1] {
		return nil, &NotFoundError{label: attestationpolicy.Label}
	}
	return nil, &NotLoadedError{edge: "policy"}
}

// AttestationCollectionsOrErr returns the AttestationCollections value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e StatementEdges) AttestationCollectionsOrErr() (*AttestationCollection, error) {
	if e.AttestationCollections != nil {
		return e.AttestationCollections, nil
	} else if e.loadedTypes[2] {
		return nil, &NotFoundError{label: attestationcollection.Label}
	}
	return nil, &NotLoadedError{edge: "attestation_collections"}
}

// DsseOrErr returns the Dsse value or an error if the edge
// was not loaded in eager-loading.
func (e StatementEdges) DsseOrErr() ([]*Dsse, error) {
	if e.loadedTypes[3] {
		return e.Dsse, nil
	}
	return nil, &NotLoadedError{edge: "dsse"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*Statement) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case statement.FieldPredicate:
			values[i] = new(sql.NullString)
		case statement.FieldID:
			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 Statement fields.
func (_m *Statement) 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 statement.FieldID:
			if value, ok := values[i].(*uuid.UUID); !ok {
				return fmt.Errorf("unexpected type %T for field id", values[i])
			} else if value != nil {
				_m.ID = *value
			}
		case statement.FieldPredicate:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field predicate", values[i])
			} else if value.Valid {
				_m.Predicate = value.String
			}
		default:
			_m.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QuerySubjects queries the "subjects" edge of the Statement entity.
func (_m *Statement) QuerySubjects() *SubjectQuery {
	return NewStatementClient(_m.config).QuerySubjects(_m)
}

// QueryPolicy queries the "policy" edge of the Statement entity.
func (_m *Statement) QueryPolicy() *AttestationPolicyQuery {
	return NewStatementClient(_m.config).QueryPolicy(_m)
}

// QueryAttestationCollections queries the "attestation_collections" edge of the Statement entity.
func (_m *Statement) QueryAttestationCollections() *AttestationCollectionQuery {
	return NewStatementClient(_m.config).QueryAttestationCollections(_m)
}

// QueryDsse queries the "dsse" edge of the Statement entity.
func (_m *Statement) QueryDsse() *DsseQuery {
	return NewStatementClient(_m.config).QueryDsse(_m)
}

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

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

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

// NamedSubjects returns the Subjects named value or an error if the edge was not
// loaded in eager-loading with this name.
func (_m *Statement) NamedSubjects(name string) ([]*Subject, error) {
	if _m.Edges.namedSubjects == nil {
		return nil, &NotLoadedError{edge: name}
	}
	nodes, ok := _m.Edges.namedSubjects[name]
	if !ok {
		return nil, &NotLoadedError{edge: name}
	}
	return nodes, nil
}

func (_m *Statement) appendNamedSubjects(name string, edges ...*Subject) {
	if _m.Edges.namedSubjects == nil {
		_m.Edges.namedSubjects = make(map[string][]*Subject)
	}
	if len(edges) == 0 {
		_m.Edges.namedSubjects[name] = []*Subject{}
	} else {
		_m.Edges.namedSubjects[name] = append(_m.Edges.namedSubjects[name], edges...)
	}
}

// NamedDsse returns the Dsse named value or an error if the edge was not
// loaded in eager-loading with this name.
func (_m *Statement) NamedDsse(name string) ([]*Dsse, error) {
	if _m.Edges.namedDsse == nil {
		return nil, &NotLoadedError{edge: name}
	}
	nodes, ok := _m.Edges.namedDsse[name]
	if !ok {
		return nil, &NotLoadedError{edge: name}
	}
	return nodes, nil
}

func (_m *Statement) appendNamedDsse(name string, edges ...*Dsse) {
	if _m.Edges.namedDsse == nil {
		_m.Edges.namedDsse = make(map[string][]*Dsse)
	}
	if len(edges) == 0 {
		_m.Edges.namedDsse[name] = []*Dsse{}
	} else {
		_m.Edges.namedDsse[name] = append(_m.Edges.namedDsse[name], edges...)
	}
}

// Statements is a parsable slice of Statement.
type Statements []*Statement
