// 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/storagepolicy"
	"github.com/anzhiyu-c/anheyu-app/pkg/domain/model"
)

// StoragePolicy is the model entity for the StoragePolicy schema.
type StoragePolicy 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"`
	// 策略名称，必须唯一
	Name string `json:"name,omitempty"`
	// 存储类型, 如 local, onedrive
	Type string `json:"type,omitempty"`
	// 策略标志，如 article_image, comment_image
	Flag string `json:"flag,omitempty"`
	// S3 Endpoint 或 Onedrive API URL
	Server string `json:"server,omitempty"`
	// 存储桶名称或 Onedrive Drive ID
	BucketName string `json:"bucket_name,omitempty"`
	// 存储空间是否为私有
	IsPrivate bool `json:"is_private,omitempty"`
	// 访问密钥 (Access Key)
	AccessKey string `json:"access_key,omitempty"`
	// 私有密钥 (Secret Key)
	SecretKey string `json:"secret_key,omitempty"`
	// 允许上传的最大文件尺寸 (Bytes)，0为不限制
	MaxSize int64 `json:"max_size,omitempty"`
	// 物理基础路径 (本地路径、云存储前缀等)
	BasePath string `json:"base_path,omitempty"`
	// 系统内的虚拟挂载路径
	VirtualPath string `json:"virtual_path,omitempty"`
	// 其他设置，以 JSON 格式存储
	Settings model.StoragePolicySettings `json:"settings,omitempty"`
	// 关联的挂载点 ID
	NodeID       *uint `json:"node_id,omitempty"`
	selectValues sql.SelectValues
}

// scanValues returns the types for scanning values from sql.Rows.
func (*StoragePolicy) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case storagepolicy.FieldSettings:
			values[i] = new(model.StoragePolicySettings)
		case storagepolicy.FieldIsPrivate:
			values[i] = new(sql.NullBool)
		case storagepolicy.FieldID, storagepolicy.FieldMaxSize, storagepolicy.FieldNodeID:
			values[i] = new(sql.NullInt64)
		case storagepolicy.FieldName, storagepolicy.FieldType, storagepolicy.FieldFlag, storagepolicy.FieldServer, storagepolicy.FieldBucketName, storagepolicy.FieldAccessKey, storagepolicy.FieldSecretKey, storagepolicy.FieldBasePath, storagepolicy.FieldVirtualPath:
			values[i] = new(sql.NullString)
		case storagepolicy.FieldDeletedAt, storagepolicy.FieldCreatedAt, storagepolicy.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 StoragePolicy fields.
func (_m *StoragePolicy) 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 storagepolicy.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 storagepolicy.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 storagepolicy.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 storagepolicy.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 storagepolicy.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 storagepolicy.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 storagepolicy.FieldFlag:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field flag", values[i])
			} else if value.Valid {
				_m.Flag = value.String
			}
		case storagepolicy.FieldServer:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field server", values[i])
			} else if value.Valid {
				_m.Server = value.String
			}
		case storagepolicy.FieldBucketName:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field bucket_name", values[i])
			} else if value.Valid {
				_m.BucketName = value.String
			}
		case storagepolicy.FieldIsPrivate:
			if value, ok := values[i].(*sql.NullBool); !ok {
				return fmt.Errorf("unexpected type %T for field is_private", values[i])
			} else if value.Valid {
				_m.IsPrivate = value.Bool
			}
		case storagepolicy.FieldAccessKey:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field access_key", values[i])
			} else if value.Valid {
				_m.AccessKey = value.String
			}
		case storagepolicy.FieldSecretKey:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field secret_key", values[i])
			} else if value.Valid {
				_m.SecretKey = value.String
			}
		case storagepolicy.FieldMaxSize:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field max_size", values[i])
			} else if value.Valid {
				_m.MaxSize = value.Int64
			}
		case storagepolicy.FieldBasePath:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field base_path", values[i])
			} else if value.Valid {
				_m.BasePath = value.String
			}
		case storagepolicy.FieldVirtualPath:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field virtual_path", values[i])
			} else if value.Valid {
				_m.VirtualPath = value.String
			}
		case storagepolicy.FieldSettings:
			if value, ok := values[i].(*model.StoragePolicySettings); !ok {
				return fmt.Errorf("unexpected type %T for field settings", values[i])
			} else if value != nil {
				_m.Settings = *value
			}
		case storagepolicy.FieldNodeID:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field node_id", values[i])
			} else if value.Valid {
				_m.NodeID = new(uint)
				*_m.NodeID = uint(value.Int64)
			}
		default:
			_m.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

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

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

// String implements the fmt.Stringer.
func (_m *StoragePolicy) String() string {
	var builder strings.Builder
	builder.WriteString("StoragePolicy(")
	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("name=")
	builder.WriteString(_m.Name)
	builder.WriteString(", ")
	builder.WriteString("type=")
	builder.WriteString(_m.Type)
	builder.WriteString(", ")
	builder.WriteString("flag=")
	builder.WriteString(_m.Flag)
	builder.WriteString(", ")
	builder.WriteString("server=")
	builder.WriteString(_m.Server)
	builder.WriteString(", ")
	builder.WriteString("bucket_name=")
	builder.WriteString(_m.BucketName)
	builder.WriteString(", ")
	builder.WriteString("is_private=")
	builder.WriteString(fmt.Sprintf("%v", _m.IsPrivate))
	builder.WriteString(", ")
	builder.WriteString("access_key=")
	builder.WriteString(_m.AccessKey)
	builder.WriteString(", ")
	builder.WriteString("secret_key=")
	builder.WriteString(_m.SecretKey)
	builder.WriteString(", ")
	builder.WriteString("max_size=")
	builder.WriteString(fmt.Sprintf("%v", _m.MaxSize))
	builder.WriteString(", ")
	builder.WriteString("base_path=")
	builder.WriteString(_m.BasePath)
	builder.WriteString(", ")
	builder.WriteString("virtual_path=")
	builder.WriteString(_m.VirtualPath)
	builder.WriteString(", ")
	builder.WriteString("settings=")
	builder.WriteString(fmt.Sprintf("%v", _m.Settings))
	builder.WriteString(", ")
	if v := _m.NodeID; v != nil {
		builder.WriteString("node_id=")
		builder.WriteString(fmt.Sprintf("%v", *v))
	}
	builder.WriteByte(')')
	return builder.String()
}

// StoragePolicies is a parsable slice of StoragePolicy.
type StoragePolicies []*StoragePolicy
