// 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/directlink"
	"github.com/anzhiyu-c/anheyu-app/ent/entity"
	"github.com/anzhiyu-c/anheyu-app/ent/file"
	"github.com/anzhiyu-c/anheyu-app/ent/user"
)

// File is the model entity for the File schema.
type File struct {
	config `json:"-"`
	// ID of the ent.
	ID uint `json:"id,omitempty"`
	// DeletedAt holds the value of the "deleted_at" field.
	DeletedAt *time.Time `json:"deleted_at,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"`
	// 文件类型 (1: 文件, 2: 目录)
	Type int `json:"type,omitempty"`
	// 文件所有者的用户ID (外键)
	OwnerID uint `json:"owner_id,omitempty"`
	// 父目录ID (外键，自引用), NULL表示根目录
	ParentID *uint `json:"parent_id,omitempty"`
	// 文件或目录的名称
	Name string `json:"name,omitempty"`
	// 文件大小 (单位: 字节)，目录大小为0
	Size int64 `json:"size,omitempty"`
	// 关联的物理存储实体ID
	PrimaryEntityID *uint `json:"primary_entity_id,omitempty"`
	// 直属子文件/子目录数量
	ChildrenCount int64 `json:"children_count,omitempty"`
	// 文件夹视图配置的JSON对象
	ViewConfig *string `json:"view_config,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the FileQuery when eager-loading is set.
	Edges        FileEdges `json:"edges"`
	selectValues sql.SelectValues
}

// FileEdges holds the relations/edges for other nodes in the graph.
type FileEdges struct {
	// Owner holds the value of the owner edge.
	Owner *User `json:"owner,omitempty"`
	// Parent holds the value of the parent edge.
	Parent *File `json:"parent,omitempty"`
	// Children holds the value of the children edge.
	Children []*File `json:"children,omitempty"`
	// PrimaryEntity holds the value of the primary_entity edge.
	PrimaryEntity *Entity `json:"primary_entity,omitempty"`
	// Versions holds the value of the versions edge.
	Versions []*FileEntity `json:"versions,omitempty"`
	// DirectLink holds the value of the direct_link edge.
	DirectLink *DirectLink `json:"direct_link,omitempty"`
	// Metadata holds the value of the metadata edge.
	Metadata []*Metadata `json:"metadata,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [7]bool
}

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

// ParentOrErr returns the Parent value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e FileEdges) ParentOrErr() (*File, error) {
	if e.Parent != nil {
		return e.Parent, nil
	} else if e.loadedTypes[1] {
		return nil, &NotFoundError{label: file.Label}
	}
	return nil, &NotLoadedError{edge: "parent"}
}

// ChildrenOrErr returns the Children value or an error if the edge
// was not loaded in eager-loading.
func (e FileEdges) ChildrenOrErr() ([]*File, error) {
	if e.loadedTypes[2] {
		return e.Children, nil
	}
	return nil, &NotLoadedError{edge: "children"}
}

// PrimaryEntityOrErr returns the PrimaryEntity value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e FileEdges) PrimaryEntityOrErr() (*Entity, error) {
	if e.PrimaryEntity != nil {
		return e.PrimaryEntity, nil
	} else if e.loadedTypes[3] {
		return nil, &NotFoundError{label: entity.Label}
	}
	return nil, &NotLoadedError{edge: "primary_entity"}
}

// VersionsOrErr returns the Versions value or an error if the edge
// was not loaded in eager-loading.
func (e FileEdges) VersionsOrErr() ([]*FileEntity, error) {
	if e.loadedTypes[4] {
		return e.Versions, nil
	}
	return nil, &NotLoadedError{edge: "versions"}
}

// DirectLinkOrErr returns the DirectLink value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e FileEdges) DirectLinkOrErr() (*DirectLink, error) {
	if e.DirectLink != nil {
		return e.DirectLink, nil
	} else if e.loadedTypes[5] {
		return nil, &NotFoundError{label: directlink.Label}
	}
	return nil, &NotLoadedError{edge: "direct_link"}
}

// MetadataOrErr returns the Metadata value or an error if the edge
// was not loaded in eager-loading.
func (e FileEdges) MetadataOrErr() ([]*Metadata, error) {
	if e.loadedTypes[6] {
		return e.Metadata, nil
	}
	return nil, &NotLoadedError{edge: "metadata"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*File) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case file.FieldID, file.FieldType, file.FieldOwnerID, file.FieldParentID, file.FieldSize, file.FieldPrimaryEntityID, file.FieldChildrenCount:
			values[i] = new(sql.NullInt64)
		case file.FieldName, file.FieldViewConfig:
			values[i] = new(sql.NullString)
		case file.FieldDeletedAt, file.FieldCreatedAt, file.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 File fields.
func (_m *File) 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 file.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 file.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 {
				_m.DeletedAt = new(time.Time)
				*_m.DeletedAt = value.Time
			}
		case file.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 file.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 file.FieldType:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field type", values[i])
			} else if value.Valid {
				_m.Type = int(value.Int64)
			}
		case file.FieldOwnerID:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field owner_id", values[i])
			} else if value.Valid {
				_m.OwnerID = uint(value.Int64)
			}
		case file.FieldParentID:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field parent_id", values[i])
			} else if value.Valid {
				_m.ParentID = new(uint)
				*_m.ParentID = uint(value.Int64)
			}
		case file.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 file.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 file.FieldPrimaryEntityID:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field primary_entity_id", values[i])
			} else if value.Valid {
				_m.PrimaryEntityID = new(uint)
				*_m.PrimaryEntityID = uint(value.Int64)
			}
		case file.FieldChildrenCount:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field children_count", values[i])
			} else if value.Valid {
				_m.ChildrenCount = value.Int64
			}
		case file.FieldViewConfig:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field view_config", values[i])
			} else if value.Valid {
				_m.ViewConfig = new(string)
				*_m.ViewConfig = 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 File.
// This includes values selected through modifiers, order, etc.
func (_m *File) Value(name string) (ent.Value, error) {
	return _m.selectValues.Get(name)
}

// QueryOwner queries the "owner" edge of the File entity.
func (_m *File) QueryOwner() *UserQuery {
	return NewFileClient(_m.config).QueryOwner(_m)
}

// QueryParent queries the "parent" edge of the File entity.
func (_m *File) QueryParent() *FileQuery {
	return NewFileClient(_m.config).QueryParent(_m)
}

// QueryChildren queries the "children" edge of the File entity.
func (_m *File) QueryChildren() *FileQuery {
	return NewFileClient(_m.config).QueryChildren(_m)
}

// QueryPrimaryEntity queries the "primary_entity" edge of the File entity.
func (_m *File) QueryPrimaryEntity() *EntityQuery {
	return NewFileClient(_m.config).QueryPrimaryEntity(_m)
}

// QueryVersions queries the "versions" edge of the File entity.
func (_m *File) QueryVersions() *FileEntityQuery {
	return NewFileClient(_m.config).QueryVersions(_m)
}

// QueryDirectLink queries the "direct_link" edge of the File entity.
func (_m *File) QueryDirectLink() *DirectLinkQuery {
	return NewFileClient(_m.config).QueryDirectLink(_m)
}

// QueryMetadata queries the "metadata" edge of the File entity.
func (_m *File) QueryMetadata() *MetadataQuery {
	return NewFileClient(_m.config).QueryMetadata(_m)
}

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

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

// String implements the fmt.Stringer.
func (_m *File) String() string {
	var builder strings.Builder
	builder.WriteString("File(")
	builder.WriteString(fmt.Sprintf("id=%v, ", _m.ID))
	if v := _m.DeletedAt; v != nil {
		builder.WriteString("deleted_at=")
		builder.WriteString(v.Format(time.ANSIC))
	}
	builder.WriteString(", ")
	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(fmt.Sprintf("%v", _m.Type))
	builder.WriteString(", ")
	builder.WriteString("owner_id=")
	builder.WriteString(fmt.Sprintf("%v", _m.OwnerID))
	builder.WriteString(", ")
	if v := _m.ParentID; v != nil {
		builder.WriteString("parent_id=")
		builder.WriteString(fmt.Sprintf("%v", *v))
	}
	builder.WriteString(", ")
	builder.WriteString("name=")
	builder.WriteString(_m.Name)
	builder.WriteString(", ")
	builder.WriteString("size=")
	builder.WriteString(fmt.Sprintf("%v", _m.Size))
	builder.WriteString(", ")
	if v := _m.PrimaryEntityID; v != nil {
		builder.WriteString("primary_entity_id=")
		builder.WriteString(fmt.Sprintf("%v", *v))
	}
	builder.WriteString(", ")
	builder.WriteString("children_count=")
	builder.WriteString(fmt.Sprintf("%v", _m.ChildrenCount))
	builder.WriteString(", ")
	if v := _m.ViewConfig; v != nil {
		builder.WriteString("view_config=")
		builder.WriteString(*v)
	}
	builder.WriteByte(')')
	return builder.String()
}

// Files is a parsable slice of File.
type Files []*File
