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

// File is the model entity for the File schema.
type File 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"`
	// Type holds the value of the "type" field.
	Type int `json:"type,omitempty"`
	// Name holds the value of the "name" field.
	Name string `json:"name,omitempty"`
	// OwnerID holds the value of the "owner_id" field.
	OwnerID int `json:"owner_id,omitempty"`
	// Size holds the value of the "size" field.
	Size int64 `json:"size,omitempty"`
	// PrimaryEntity holds the value of the "primary_entity" field.
	PrimaryEntity int `json:"primary_entity,omitempty"`
	// FileChildren holds the value of the "file_children" field.
	FileChildren int `json:"file_children,omitempty"`
	// IsSymbolic holds the value of the "is_symbolic" field.
	IsSymbolic bool `json:"is_symbolic,omitempty"`
	// Props holds the value of the "props" field.
	Props *types.FileProps `json:"props,omitempty"`
	// StoragePolicyFiles holds the value of the "storage_policy_files" field.
	StoragePolicyFiles int `json:"storage_policy_files,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"`
	// StoragePolicies holds the value of the storage_policies edge.
	StoragePolicies *StoragePolicy `json:"storage_policies,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"`
	// Metadata holds the value of the metadata edge.
	Metadata []*Metadata `json:"metadata,omitempty"`
	// Entities holds the value of the entities edge.
	Entities []*Entity `json:"entities,omitempty"`
	// Shares holds the value of the shares edge.
	Shares []*Share `json:"shares,omitempty"`
	// DirectLinks holds the value of the direct_links edge.
	DirectLinks []*DirectLink `json:"direct_links,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [8]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.loadedTypes[0] {
		if e.Owner == nil {
			// Edge was loaded but was not found.
			return nil, &NotFoundError{label: user.Label}
		}
		return e.Owner, nil
	}
	return nil, &NotLoadedError{edge: "owner"}
}

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

// 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.loadedTypes[2] {
		if e.Parent == nil {
			// Edge was loaded but was not found.
			return nil, &NotFoundError{label: file.Label}
		}
		return e.Parent, nil
	}
	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[3] {
		return e.Children, nil
	}
	return nil, &NotLoadedError{edge: "children"}
}

// 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[4] {
		return e.Metadata, nil
	}
	return nil, &NotLoadedError{edge: "metadata"}
}

// EntitiesOrErr returns the Entities value or an error if the edge
// was not loaded in eager-loading.
func (e FileEdges) EntitiesOrErr() ([]*Entity, error) {
	if e.loadedTypes[5] {
		return e.Entities, nil
	}
	return nil, &NotLoadedError{edge: "entities"}
}

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

// DirectLinksOrErr returns the DirectLinks value or an error if the edge
// was not loaded in eager-loading.
func (e FileEdges) DirectLinksOrErr() ([]*DirectLink, error) {
	if e.loadedTypes[7] {
		return e.DirectLinks, nil
	}
	return nil, &NotLoadedError{edge: "direct_links"}
}

// 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.FieldProps:
			values[i] = new([]byte)
		case file.FieldIsSymbolic:
			values[i] = new(sql.NullBool)
		case file.FieldID, file.FieldType, file.FieldOwnerID, file.FieldSize, file.FieldPrimaryEntity, file.FieldFileChildren, file.FieldStoragePolicyFiles:
			values[i] = new(sql.NullInt64)
		case file.FieldName:
			values[i] = new(sql.NullString)
		case 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 (f *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)
			}
			f.ID = int(value.Int64)
		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 {
				f.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 {
				f.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 {
				f.Type = int(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 {
				f.Name = value.String
			}
		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 {
				f.OwnerID = int(value.Int64)
			}
		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 {
				f.Size = value.Int64
			}
		case file.FieldPrimaryEntity:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field primary_entity", values[i])
			} else if value.Valid {
				f.PrimaryEntity = int(value.Int64)
			}
		case file.FieldFileChildren:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field file_children", values[i])
			} else if value.Valid {
				f.FileChildren = int(value.Int64)
			}
		case file.FieldIsSymbolic:
			if value, ok := values[i].(*sql.NullBool); !ok {
				return fmt.Errorf("unexpected type %T for field is_symbolic", values[i])
			} else if value.Valid {
				f.IsSymbolic = value.Bool
			}
		case file.FieldProps:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field props", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &f.Props); err != nil {
					return fmt.Errorf("unmarshal field props: %w", err)
				}
			}
		case file.FieldStoragePolicyFiles:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field storage_policy_files", values[i])
			} else if value.Valid {
				f.StoragePolicyFiles = int(value.Int64)
			}
		default:
			f.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 (f *File) Value(name string) (ent.Value, error) {
	return f.selectValues.Get(name)
}

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

// QueryStoragePolicies queries the "storage_policies" edge of the File entity.
func (f *File) QueryStoragePolicies() *StoragePolicyQuery {
	return NewFileClient(f.config).QueryStoragePolicies(f)
}

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

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

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

// QueryEntities queries the "entities" edge of the File entity.
func (f *File) QueryEntities() *EntityQuery {
	return NewFileClient(f.config).QueryEntities(f)
}

// QueryShares queries the "shares" edge of the File entity.
func (f *File) QueryShares() *ShareQuery {
	return NewFileClient(f.config).QueryShares(f)
}

// QueryDirectLinks queries the "direct_links" edge of the File entity.
func (f *File) QueryDirectLinks() *DirectLinkQuery {
	return NewFileClient(f.config).QueryDirectLinks(f)
}

// 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 (f *File) Update() *FileUpdateOne {
	return NewFileClient(f.config).UpdateOne(f)
}

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

// String implements the fmt.Stringer.
func (f *File) String() string {
	var builder strings.Builder
	builder.WriteString("File(")
	builder.WriteString(fmt.Sprintf("id=%v, ", f.ID))
	builder.WriteString("created_at=")
	builder.WriteString(f.CreatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("updated_at=")
	builder.WriteString(f.UpdatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("type=")
	builder.WriteString(fmt.Sprintf("%v", f.Type))
	builder.WriteString(", ")
	builder.WriteString("name=")
	builder.WriteString(f.Name)
	builder.WriteString(", ")
	builder.WriteString("owner_id=")
	builder.WriteString(fmt.Sprintf("%v", f.OwnerID))
	builder.WriteString(", ")
	builder.WriteString("size=")
	builder.WriteString(fmt.Sprintf("%v", f.Size))
	builder.WriteString(", ")
	builder.WriteString("primary_entity=")
	builder.WriteString(fmt.Sprintf("%v", f.PrimaryEntity))
	builder.WriteString(", ")
	builder.WriteString("file_children=")
	builder.WriteString(fmt.Sprintf("%v", f.FileChildren))
	builder.WriteString(", ")
	builder.WriteString("is_symbolic=")
	builder.WriteString(fmt.Sprintf("%v", f.IsSymbolic))
	builder.WriteString(", ")
	builder.WriteString("props=")
	builder.WriteString(fmt.Sprintf("%v", f.Props))
	builder.WriteString(", ")
	builder.WriteString("storage_policy_files=")
	builder.WriteString(fmt.Sprintf("%v", f.StoragePolicyFiles))
	builder.WriteByte(')')
	return builder.String()
}

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

// SetStoragePolicies manually set the edge as loaded state.
func (e *File) SetStoragePolicies(v *StoragePolicy) {
	e.Edges.StoragePolicies = v
	e.Edges.loadedTypes[1] = true
}

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

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

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

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

// SetShares manually set the edge as loaded state.
func (e *File) SetShares(v []*Share) {
	e.Edges.Shares = v
	e.Edges.loadedTypes[6] = true
}

// SetDirectLinks manually set the edge as loaded state.
func (e *File) SetDirectLinks(v []*DirectLink) {
	e.Edges.DirectLinks = v
	e.Edges.loadedTypes[7] = true
}

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