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

// UserInstalledTheme is the model entity for the UserInstalledTheme schema.
type UserInstalledTheme 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"`
	// 用户ID
	UserID uint `json:"user_id,omitempty"`
	// 主题名称（对应themes目录下的文件夹名，也是外部API的主题标识）
	ThemeName string `json:"theme_name,omitempty"`
	// 主题商城中的ID（用于关联外部API数据）
	ThemeMarketID int `json:"theme_market_id,omitempty"`
	// 是否为当前使用主题
	IsCurrent bool `json:"is_current,omitempty"`
	// 安装时间
	InstallTime time.Time `json:"install_time,omitempty"`
	// 用户个性化主题配置（覆盖默认配置）
	UserThemeConfig map[string]interface{} `json:"user_theme_config,omitempty"`
	// 安装时的版本号（用于版本检查和更新提醒）
	InstalledVersion string `json:"installed_version,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the UserInstalledThemeQuery when eager-loading is set.
	Edges        UserInstalledThemeEdges `json:"edges"`
	selectValues sql.SelectValues
}

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

// 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 UserInstalledThemeEdges) UserOrErr() (*User, error) {
	if e.User != nil {
		return e.User, nil
	} else if e.loadedTypes[0] {
		return nil, &NotFoundError{label: user.Label}
	}
	return nil, &NotLoadedError{edge: "user"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*UserInstalledTheme) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case userinstalledtheme.FieldUserThemeConfig:
			values[i] = new([]byte)
		case userinstalledtheme.FieldIsCurrent:
			values[i] = new(sql.NullBool)
		case userinstalledtheme.FieldID, userinstalledtheme.FieldUserID, userinstalledtheme.FieldThemeMarketID:
			values[i] = new(sql.NullInt64)
		case userinstalledtheme.FieldThemeName, userinstalledtheme.FieldInstalledVersion:
			values[i] = new(sql.NullString)
		case userinstalledtheme.FieldDeletedAt, userinstalledtheme.FieldCreatedAt, userinstalledtheme.FieldUpdatedAt, userinstalledtheme.FieldInstallTime:
			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 UserInstalledTheme fields.
func (_m *UserInstalledTheme) 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 userinstalledtheme.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 userinstalledtheme.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 userinstalledtheme.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 userinstalledtheme.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 userinstalledtheme.FieldUserID:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field user_id", values[i])
			} else if value.Valid {
				_m.UserID = uint(value.Int64)
			}
		case userinstalledtheme.FieldThemeName:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field theme_name", values[i])
			} else if value.Valid {
				_m.ThemeName = value.String
			}
		case userinstalledtheme.FieldThemeMarketID:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field theme_market_id", values[i])
			} else if value.Valid {
				_m.ThemeMarketID = int(value.Int64)
			}
		case userinstalledtheme.FieldIsCurrent:
			if value, ok := values[i].(*sql.NullBool); !ok {
				return fmt.Errorf("unexpected type %T for field is_current", values[i])
			} else if value.Valid {
				_m.IsCurrent = value.Bool
			}
		case userinstalledtheme.FieldInstallTime:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field install_time", values[i])
			} else if value.Valid {
				_m.InstallTime = value.Time
			}
		case userinstalledtheme.FieldUserThemeConfig:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field user_theme_config", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &_m.UserThemeConfig); err != nil {
					return fmt.Errorf("unmarshal field user_theme_config: %w", err)
				}
			}
		case userinstalledtheme.FieldInstalledVersion:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field installed_version", values[i])
			} else if value.Valid {
				_m.InstalledVersion = 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 UserInstalledTheme.
// This includes values selected through modifiers, order, etc.
func (_m *UserInstalledTheme) Value(name string) (ent.Value, error) {
	return _m.selectValues.Get(name)
}

// QueryUser queries the "user" edge of the UserInstalledTheme entity.
func (_m *UserInstalledTheme) QueryUser() *UserQuery {
	return NewUserInstalledThemeClient(_m.config).QueryUser(_m)
}

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

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

// String implements the fmt.Stringer.
func (_m *UserInstalledTheme) String() string {
	var builder strings.Builder
	builder.WriteString("UserInstalledTheme(")
	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("user_id=")
	builder.WriteString(fmt.Sprintf("%v", _m.UserID))
	builder.WriteString(", ")
	builder.WriteString("theme_name=")
	builder.WriteString(_m.ThemeName)
	builder.WriteString(", ")
	builder.WriteString("theme_market_id=")
	builder.WriteString(fmt.Sprintf("%v", _m.ThemeMarketID))
	builder.WriteString(", ")
	builder.WriteString("is_current=")
	builder.WriteString(fmt.Sprintf("%v", _m.IsCurrent))
	builder.WriteString(", ")
	builder.WriteString("install_time=")
	builder.WriteString(_m.InstallTime.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("user_theme_config=")
	builder.WriteString(fmt.Sprintf("%v", _m.UserThemeConfig))
	builder.WriteString(", ")
	builder.WriteString("installed_version=")
	builder.WriteString(_m.InstalledVersion)
	builder.WriteByte(')')
	return builder.String()
}

// UserInstalledThemes is a parsable slice of UserInstalledTheme.
type UserInstalledThemes []*UserInstalledTheme
