// Code generated by ent, DO NOT EDIT.

package ent

import (
	"fmt"
	"strings"
	"time"

	"entgo.io/ent"
	"entgo.io/ent/dialect/sql"
	"github.com/anzhiyu-c/anheyu-app/ent/entity"
	"github.com/anzhiyu-c/anheyu-app/pkg/domain/model"
)

// Entity is the model entity for the Entity schema.
type Entity struct {
	config `json:"-"`
	// ID of the ent.
	ID uint `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"`
	// 存储内容的类型 (文件、图片、视频等)
	Type string `json:"type,omitempty"`
	// 文件内容的来源路径或键 (如OSS ObjectKey, 本地路径)
	Source *string `json:"source,omitempty"`
	// 物理存储的文件大小 (字节)
	Size int64 `json:"size,omitempty"`
	// 关联的上传会话ID
	UploadSessionID *string `json:"upload_session_id,omitempty"`
	// 回收选项 (如保留时间)
	RecycleOptions *string `json:"recycle_options,omitempty"`
	// 关联的存储策略ID
	PolicyID uint `json:"policy_id,omitempty"`
	// 创建此存储实体的用户ID
	CreatedBy *uint64 `json:"created_by,omitempty"`
	// 存储实体的ETag/哈希值
	Etag *string `json:"etag,omitempty"`
	// 文件的MIME类型
	MimeType *string `json:"mime_type,omitempty"`
	// 媒体文件尺寸 (如 '1920x1080'), 非媒体文件为空
	Dimension *string `json:"dimension,omitempty"`
	// 存储提供者特有的额外元数据
	StorageMetadata model.JSONMap `json:"storage_metadata,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 {
	// FileVersions holds the value of the file_versions edge.
	FileVersions []*FileEntity `json:"file_versions,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [1]bool
}

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

// 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.FieldStorageMetadata:
			values[i] = new(model.JSONMap)
		case entity.FieldID, entity.FieldSize, entity.FieldPolicyID, entity.FieldCreatedBy:
			values[i] = new(sql.NullInt64)
		case entity.FieldType, entity.FieldSource, entity.FieldUploadSessionID, entity.FieldRecycleOptions, entity.FieldEtag, entity.FieldMimeType, entity.FieldDimension:
			values[i] = new(sql.NullString)
		case entity.FieldCreatedAt, entity.FieldUpdatedAt:
			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 (_m *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)
			}
			_m.ID = uint(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 {
				_m.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 {
				_m.UpdatedAt = value.Time
			}
		case entity.FieldType:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field type", values[i])
			} else if value.Valid {
				_m.Type = value.String
			}
		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 {
				_m.Source = new(string)
				*_m.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 {
				_m.Size = value.Int64
			}
		case entity.FieldUploadSessionID:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field upload_session_id", values[i])
			} else if value.Valid {
				_m.UploadSessionID = new(string)
				*_m.UploadSessionID = value.String
			}
		case entity.FieldRecycleOptions:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field recycle_options", values[i])
			} else if value.Valid {
				_m.RecycleOptions = new(string)
				*_m.RecycleOptions = value.String
			}
		case entity.FieldPolicyID:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field policy_id", values[i])
			} else if value.Valid {
				_m.PolicyID = uint(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 {
				_m.CreatedBy = new(uint64)
				*_m.CreatedBy = uint64(value.Int64)
			}
		case entity.FieldEtag:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field etag", values[i])
			} else if value.Valid {
				_m.Etag = new(string)
				*_m.Etag = value.String
			}
		case entity.FieldMimeType:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field mime_type", values[i])
			} else if value.Valid {
				_m.MimeType = new(string)
				*_m.MimeType = value.String
			}
		case entity.FieldDimension:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field dimension", values[i])
			} else if value.Valid {
				_m.Dimension = new(string)
				*_m.Dimension = value.String
			}
		case entity.FieldStorageMetadata:
			if value, ok := values[i].(*model.JSONMap); !ok {
				return fmt.Errorf("unexpected type %T for field storage_metadata", values[i])
			} else if value != nil {
				_m.StorageMetadata = *value
			}
		default:
			_m.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 (_m *Entity) Value(name string) (ent.Value, error) {
	return _m.selectValues.Get(name)
}

// QueryFileVersions queries the "file_versions" edge of the Entity entity.
func (_m *Entity) QueryFileVersions() *FileEntityQuery {
	return NewEntityClient(_m.config).QueryFileVersions(_m)
}

// 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 (_m *Entity) Update() *EntityUpdateOne {
	return NewEntityClient(_m.config).UpdateOne(_m)
}

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

// String implements the fmt.Stringer.
func (_m *Entity) String() string {
	var builder strings.Builder
	builder.WriteString("Entity(")
	builder.WriteString(fmt.Sprintf("id=%v, ", _m.ID))
	builder.WriteString("created_at=")
	builder.WriteString(_m.CreatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("updated_at=")
	builder.WriteString(_m.UpdatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("type=")
	builder.WriteString(_m.Type)
	builder.WriteString(", ")
	if v := _m.Source; v != nil {
		builder.WriteString("source=")
		builder.WriteString(*v)
	}
	builder.WriteString(", ")
	builder.WriteString("size=")
	builder.WriteString(fmt.Sprintf("%v", _m.Size))
	builder.WriteString(", ")
	if v := _m.UploadSessionID; v != nil {
		builder.WriteString("upload_session_id=")
		builder.WriteString(*v)
	}
	builder.WriteString(", ")
	if v := _m.RecycleOptions; v != nil {
		builder.WriteString("recycle_options=")
		builder.WriteString(*v)
	}
	builder.WriteString(", ")
	builder.WriteString("policy_id=")
	builder.WriteString(fmt.Sprintf("%v", _m.PolicyID))
	builder.WriteString(", ")
	if v := _m.CreatedBy; v != nil {
		builder.WriteString("created_by=")
		builder.WriteString(fmt.Sprintf("%v", *v))
	}
	builder.WriteString(", ")
	if v := _m.Etag; v != nil {
		builder.WriteString("etag=")
		builder.WriteString(*v)
	}
	builder.WriteString(", ")
	if v := _m.MimeType; v != nil {
		builder.WriteString("mime_type=")
		builder.WriteString(*v)
	}
	builder.WriteString(", ")
	if v := _m.Dimension; v != nil {
		builder.WriteString("dimension=")
		builder.WriteString(*v)
	}
	builder.WriteString(", ")
	builder.WriteString("storage_metadata=")
	builder.WriteString(fmt.Sprintf("%v", _m.StorageMetadata))
	builder.WriteByte(')')
	return builder.String()
}

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