// 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/cloudreve/Cloudreve/v4/ent/entity"
	"github.com/cloudreve/Cloudreve/v4/ent/storagepolicy"
	"github.com/cloudreve/Cloudreve/v4/ent/user"
	"github.com/cloudreve/Cloudreve/v4/inventory/types"
	"github.com/gofrs/uuid"
)

// Entity is the model entity for the Entity schema.
type Entity struct {
	config `json:"-"`
	// ID of the ent.
	ID int `json:"id,omitempty"`
	// CreatedAt holds the value of the "created_at" field.
	CreatedAt time.Time `json:"created_at,omitempty"`
	// UpdatedAt holds the value of the "updated_at" field.
	UpdatedAt time.Time `json:"updated_at,omitempty"`
	// DeletedAt holds the value of the "deleted_at" field.
	DeletedAt *time.Time `json:"deleted_at,omitempty"`
	// Type holds the value of the "type" field.
	Type int `json:"type,omitempty"`
	// Source holds the value of the "source" field.
	Source string `json:"source,omitempty"`
	// Size holds the value of the "size" field.
	Size int64 `json:"size,omitempty"`
	// ReferenceCount holds the value of the "reference_count" field.
	ReferenceCount int `json:"reference_count,omitempty"`
	// StoragePolicyEntities holds the value of the "storage_policy_entities" field.
	StoragePolicyEntities int `json:"storage_policy_entities,omitempty"`
	// CreatedBy holds the value of the "created_by" field.
	CreatedBy int `json:"created_by,omitempty"`
	// UploadSessionID holds the value of the "upload_session_id" field.
	UploadSessionID *uuid.UUID `json:"upload_session_id,omitempty"`
	// RecycleOptions holds the value of the "recycle_options" field.
	RecycleOptions *types.EntityRecycleOption `json:"recycle_options,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the EntityQuery when eager-loading is set.
	Edges        EntityEdges `json:"edges"`
	selectValues sql.SelectValues
}

// EntityEdges holds the relations/edges for other nodes in the graph.
type EntityEdges struct {
	// File holds the value of the file edge.
	File []*File `json:"file,omitempty"`
	// User holds the value of the user edge.
	User *User `json:"user,omitempty"`
	// StoragePolicy holds the value of the storage_policy edge.
	StoragePolicy *StoragePolicy `json:"storage_policy,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [3]bool
}

// FileOrErr returns the File value or an error if the edge
// was not loaded in eager-loading.
func (e EntityEdges) FileOrErr() ([]*File, error) {
	if e.loadedTypes[0] {
		return e.File, nil
	}
	return nil, &NotLoadedError{edge: "file"}
}

// UserOrErr returns the User value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e EntityEdges) UserOrErr() (*User, error) {
	if e.loadedTypes[1] {
		if e.User == nil {
			// Edge was loaded but was not found.
			return nil, &NotFoundError{label: user.Label}
		}
		return e.User, nil
	}
	return nil, &NotLoadedError{edge: "user"}
}

// StoragePolicyOrErr returns the StoragePolicy value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e EntityEdges) StoragePolicyOrErr() (*StoragePolicy, error) {
	if e.loadedTypes[2] {
		if e.StoragePolicy == nil {
			// Edge was loaded but was not found.
			return nil, &NotFoundError{label: storagepolicy.Label}
		}
		return e.StoragePolicy, nil
	}
	return nil, &NotLoadedError{edge: "storage_policy"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*Entity) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case entity.FieldUploadSessionID:
			values[i] = &sql.NullScanner{S: new(uuid.UUID)}
		case entity.FieldRecycleOptions:
			values[i] = new([]byte)
		case entity.FieldID, entity.FieldType, entity.FieldSize, entity.FieldReferenceCount, entity.FieldStoragePolicyEntities, entity.FieldCreatedBy:
			values[i] = new(sql.NullInt64)
		case entity.FieldSource:
			values[i] = new(sql.NullString)
		case entity.FieldCreatedAt, entity.FieldUpdatedAt, entity.FieldDeletedAt:
			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 Entity fields.
func (e *Entity) 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 entity.FieldID:
			value, ok := values[i].(*sql.NullInt64)
			if !ok {
				return fmt.Errorf("unexpected type %T for field id", value)
			}
			e.ID = int(value.Int64)
		case entity.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 {
				e.CreatedAt = value.Time
			}
		case entity.FieldUpdatedAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field updated_at", values[i])
			} else if value.Valid {
				e.UpdatedAt = value.Time
			}
		case entity.FieldDeletedAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field deleted_at", values[i])
			} else if value.Valid {
				e.DeletedAt = new(time.Time)
				*e.DeletedAt = value.Time
			}
		case entity.FieldType:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field type", values[i])
			} else if value.Valid {
				e.Type = int(value.Int64)
			}
		case entity.FieldSource:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field source", values[i])
			} else if value.Valid {
				e.Source = value.String
			}
		case entity.FieldSize:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field size", values[i])
			} else if value.Valid {
				e.Size = value.Int64
			}
		case entity.FieldReferenceCount:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field reference_count", values[i])
			} else if value.Valid {
				e.ReferenceCount = int(value.Int64)
			}
		case entity.FieldStoragePolicyEntities:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field storage_policy_entities", values[i])
			} else if value.Valid {
				e.StoragePolicyEntities = int(value.Int64)
			}
		case entity.FieldCreatedBy:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field created_by", values[i])
			} else if value.Valid {
				e.CreatedBy = int(value.Int64)
			}
		case entity.FieldUploadSessionID:
			if value, ok := values[i].(*sql.NullScanner); !ok {
				return fmt.Errorf("unexpected type %T for field upload_session_id", values[i])
			} else if value.Valid {
				e.UploadSessionID = new(uuid.UUID)
				*e.UploadSessionID = *value.S.(*uuid.UUID)
			}
		case entity.FieldRecycleOptions:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field recycle_options", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &e.RecycleOptions); err != nil {
					return fmt.Errorf("unmarshal field recycle_options: %w", err)
				}
			}
		default:
			e.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryFile queries the "file" edge of the Entity entity.
func (e *Entity) QueryFile() *FileQuery {
	return NewEntityClient(e.config).QueryFile(e)
}

// QueryUser queries the "user" edge of the Entity entity.
func (e *Entity) QueryUser() *UserQuery {
	return NewEntityClient(e.config).QueryUser(e)
}

// QueryStoragePolicy queries the "storage_policy" edge of the Entity entity.
func (e *Entity) QueryStoragePolicy() *StoragePolicyQuery {
	return NewEntityClient(e.config).QueryStoragePolicy(e)
}

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

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

// String implements the fmt.Stringer.
func (e *Entity) String() string {
	var builder strings.Builder
	builder.WriteString("Entity(")
	builder.WriteString(fmt.Sprintf("id=%v, ", e.ID))
	builder.WriteString("created_at=")
	builder.WriteString(e.CreatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("updated_at=")
	builder.WriteString(e.UpdatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	if v := e.DeletedAt; v != nil {
		builder.WriteString("deleted_at=")
		builder.WriteString(v.Format(time.ANSIC))
	}
	builder.WriteString(", ")
	builder.WriteString("type=")
	builder.WriteString(fmt.Sprintf("%v", e.Type))
	builder.WriteString(", ")
	builder.WriteString("source=")
	builder.WriteString(e.Source)
	builder.WriteString(", ")
	builder.WriteString("size=")
	builder.WriteString(fmt.Sprintf("%v", e.Size))
	builder.WriteString(", ")
	builder.WriteString("reference_count=")
	builder.WriteString(fmt.Sprintf("%v", e.ReferenceCount))
	builder.WriteString(", ")
	builder.WriteString("storage_policy_entities=")
	builder.WriteString(fmt.Sprintf("%v", e.StoragePolicyEntities))
	builder.WriteString(", ")
	builder.WriteString("created_by=")
	builder.WriteString(fmt.Sprintf("%v", e.CreatedBy))
	builder.WriteString(", ")
	if v := e.UploadSessionID; v != nil {
		builder.WriteString("upload_session_id=")
		builder.WriteString(fmt.Sprintf("%v", *v))
	}
	builder.WriteString(", ")
	builder.WriteString("recycle_options=")
	builder.WriteString(fmt.Sprintf("%v", e.RecycleOptions))
	builder.WriteByte(')')
	return builder.String()
}

// SetFile manually set the edge as loaded state.
func (e *Entity) SetFile(v []*File) {
	e.Edges.File = v
	e.Edges.loadedTypes[0] = true
}

// SetUser manually set the edge as loaded state.
func (e *Entity) SetUser(v *User) {
	e.Edges.User = v
	e.Edges.loadedTypes[1] = true
}

// SetStoragePolicy manually set the edge as loaded state.
func (e *Entity) SetStoragePolicy(v *StoragePolicy) {
	e.Edges.StoragePolicy = v
	e.Edges.loadedTypes[2] = true
}

// Entities is a parsable slice of Entity.
type Entities []*Entity
