// Code generated by ent, DO NOT EDIT.

package ent

import (
	"fmt"
	"strings"
	"time"

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

// Dsse is the model entity for the Dsse schema.
type Dsse struct {
	config `json:"-"`
	// ID of the ent.
	ID uuid.UUID `json:"id,omitempty"`
	// CreatedAt holds the value of the "created_at" field.
	CreatedAt *time.Time `json:"created_at,omitempty"`
	// GitoidSha256 holds the value of the "gitoid_sha256" field.
	GitoidSha256 string `json:"gitoid_sha256,omitempty"`
	// PayloadType holds the value of the "payload_type" field.
	PayloadType string `json:"payload_type,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the DsseQuery when eager-loading is set.
	Edges          DsseEdges `json:"edges"`
	dsse_statement *uuid.UUID
	selectValues   sql.SelectValues
}

// DsseEdges holds the relations/edges for other nodes in the graph.
type DsseEdges struct {
	// Statement holds the value of the statement edge.
	Statement *Statement `json:"statement,omitempty"`
	// Signatures holds the value of the signatures edge.
	Signatures []*Signature `json:"signatures,omitempty"`
	// PayloadDigests holds the value of the payload_digests edge.
	PayloadDigests []*PayloadDigest `json:"payload_digests,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

	namedSignatures     map[string][]*Signature
	namedPayloadDigests map[string][]*PayloadDigest
}

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

// SignaturesOrErr returns the Signatures value or an error if the edge
// was not loaded in eager-loading.
func (e DsseEdges) SignaturesOrErr() ([]*Signature, error) {
	if e.loadedTypes[1] {
		return e.Signatures, nil
	}
	return nil, &NotLoadedError{edge: "signatures"}
}

// PayloadDigestsOrErr returns the PayloadDigests value or an error if the edge
// was not loaded in eager-loading.
func (e DsseEdges) PayloadDigestsOrErr() ([]*PayloadDigest, error) {
	if e.loadedTypes[2] {
		return e.PayloadDigests, nil
	}
	return nil, &NotLoadedError{edge: "payload_digests"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*Dsse) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case dsse.FieldGitoidSha256, dsse.FieldPayloadType:
			values[i] = new(sql.NullString)
		case dsse.FieldCreatedAt:
			values[i] = new(sql.NullTime)
		case dsse.FieldID:
			values[i] = new(uuid.UUID)
		case dsse.ForeignKeys[0]: // dsse_statement
			values[i] = &sql.NullScanner{S: 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 Dsse fields.
func (_m *Dsse) 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 dsse.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 dsse.FieldCreatedAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field created_at", values[i])
			} else if value.Valid {
				_m.CreatedAt = new(time.Time)
				*_m.CreatedAt = value.Time
			}
		case dsse.FieldGitoidSha256:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field gitoid_sha256", values[i])
			} else if value.Valid {
				_m.GitoidSha256 = value.String
			}
		case dsse.FieldPayloadType:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field payload_type", values[i])
			} else if value.Valid {
				_m.PayloadType = value.String
			}
		case dsse.ForeignKeys[0]:
			if value, ok := values[i].(*sql.NullScanner); !ok {
				return fmt.Errorf("unexpected type %T for field dsse_statement", values[i])
			} else if value.Valid {
				_m.dsse_statement = new(uuid.UUID)
				*_m.dsse_statement = *value.S.(*uuid.UUID)
			}
		default:
			_m.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryStatement queries the "statement" edge of the Dsse entity.
func (_m *Dsse) QueryStatement() *StatementQuery {
	return NewDsseClient(_m.config).QueryStatement(_m)
}

// QuerySignatures queries the "signatures" edge of the Dsse entity.
func (_m *Dsse) QuerySignatures() *SignatureQuery {
	return NewDsseClient(_m.config).QuerySignatures(_m)
}

// QueryPayloadDigests queries the "payload_digests" edge of the Dsse entity.
func (_m *Dsse) QueryPayloadDigests() *PayloadDigestQuery {
	return NewDsseClient(_m.config).QueryPayloadDigests(_m)
}

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

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

// String implements the fmt.Stringer.
func (_m *Dsse) String() string {
	var builder strings.Builder
	builder.WriteString("Dsse(")
	builder.WriteString(fmt.Sprintf("id=%v, ", _m.ID))
	if v := _m.CreatedAt; v != nil {
		builder.WriteString("created_at=")
		builder.WriteString(v.Format(time.ANSIC))
	}
	builder.WriteString(", ")
	builder.WriteString("gitoid_sha256=")
	builder.WriteString(_m.GitoidSha256)
	builder.WriteString(", ")
	builder.WriteString("payload_type=")
	builder.WriteString(_m.PayloadType)
	builder.WriteByte(')')
	return builder.String()
}

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

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

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

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

// Dsses is a parsable slice of Dsse.
type Dsses []*Dsse
