// 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/TSELab/astra/internal/store/ent/artifact"
)

// Artifact is the model entity for the Artifact schema.
type Artifact struct {
	config `json:"-"`
	// ID of the ent.
	ID int `json:"id,omitempty"`
	// unique AStRA artifact ID
	AstraID string `json:"astra_id,omitempty"`
	// Name holds the value of the "name" field.
	Name string `json:"name,omitempty"`
	// e.g. source_file, repo_state, binary, sbom
	Kind string `json:"kind,omitempty"`
	// Optional original locator/path/URL
	URI string `json:"uri,omitempty"`
	// Version holds the value of the "version" field.
	Version string `json:"version,omitempty"`
	// Optional content hash
	Hash string `json:"hash,omitempty"`
	// Size holds the value of the "size" field.
	Size int64 `json:"size,omitempty"`
	// Metadata holds the value of the "metadata" field.
	Metadata map[string]string `json:"metadata,omitempty"`
	// Completeness holds the value of the "completeness" field.
	Completeness artifact.Completeness `json:"completeness,omitempty"`
	// CreatedAt holds the value of the "created_at" field.
	CreatedAt    time.Time `json:"created_at,omitempty"`
	selectValues sql.SelectValues
}

// scanValues returns the types for scanning values from sql.Rows.
func (*Artifact) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case artifact.FieldMetadata:
			values[i] = new([]byte)
		case artifact.FieldID, artifact.FieldSize:
			values[i] = new(sql.NullInt64)
		case artifact.FieldAstraID, artifact.FieldName, artifact.FieldKind, artifact.FieldURI, artifact.FieldVersion, artifact.FieldHash, artifact.FieldCompleteness:
			values[i] = new(sql.NullString)
		case artifact.FieldCreatedAt:
			values[i] = new(sql.NullTime)
		default:
			values[i] = new(sql.UnknownType)
		}
	}
	return values, nil
}

// assignValues assigns the values that were returned from sql.Rows (after scanning)
// to the Artifact fields.
func (_m *Artifact) 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 artifact.FieldID:
			value, ok := values[i].(*sql.NullInt64)
			if !ok {
				return fmt.Errorf("unexpected type %T for field id", value)
			}
			_m.ID = int(value.Int64)
		case artifact.FieldAstraID:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field astra_id", values[i])
			} else if value.Valid {
				_m.AstraID = value.String
			}
		case artifact.FieldName:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field name", values[i])
			} else if value.Valid {
				_m.Name = value.String
			}
		case artifact.FieldKind:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field kind", values[i])
			} else if value.Valid {
				_m.Kind = value.String
			}
		case artifact.FieldURI:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field uri", values[i])
			} else if value.Valid {
				_m.URI = value.String
			}
		case artifact.FieldVersion:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field version", values[i])
			} else if value.Valid {
				_m.Version = value.String
			}
		case artifact.FieldHash:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field hash", values[i])
			} else if value.Valid {
				_m.Hash = value.String
			}
		case artifact.FieldSize:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field size", values[i])
			} else if value.Valid {
				_m.Size = value.Int64
			}
		case artifact.FieldMetadata:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field metadata", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &_m.Metadata); err != nil {
					return fmt.Errorf("unmarshal field metadata: %w", err)
				}
			}
		case artifact.FieldCompleteness:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field completeness", values[i])
			} else if value.Valid {
				_m.Completeness = artifact.Completeness(value.String)
			}
		case artifact.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 = value.Time
			}
		default:
			_m.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

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

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

// String implements the fmt.Stringer.
func (_m *Artifact) String() string {
	var builder strings.Builder
	builder.WriteString("Artifact(")
	builder.WriteString(fmt.Sprintf("id=%v, ", _m.ID))
	builder.WriteString("astra_id=")
	builder.WriteString(_m.AstraID)
	builder.WriteString(", ")
	builder.WriteString("name=")
	builder.WriteString(_m.Name)
	builder.WriteString(", ")
	builder.WriteString("kind=")
	builder.WriteString(_m.Kind)
	builder.WriteString(", ")
	builder.WriteString("uri=")
	builder.WriteString(_m.URI)
	builder.WriteString(", ")
	builder.WriteString("version=")
	builder.WriteString(_m.Version)
	builder.WriteString(", ")
	builder.WriteString("hash=")
	builder.WriteString(_m.Hash)
	builder.WriteString(", ")
	builder.WriteString("size=")
	builder.WriteString(fmt.Sprintf("%v", _m.Size))
	builder.WriteString(", ")
	builder.WriteString("metadata=")
	builder.WriteString(fmt.Sprintf("%v", _m.Metadata))
	builder.WriteString(", ")
	builder.WriteString("completeness=")
	builder.WriteString(fmt.Sprintf("%v", _m.Completeness))
	builder.WriteString(", ")
	builder.WriteString("created_at=")
	builder.WriteString(_m.CreatedAt.Format(time.ANSIC))
	builder.WriteByte(')')
	return builder.String()
}

// Artifacts is a parsable slice of Artifact.
type Artifacts []*Artifact
