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

// Comment is the model entity for the Comment schema.
type Comment 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 time.Time `json:"created_at,omitempty"`
	// 更新时间
	UpdatedAt time.Time `json:"updated_at,omitempty"`
	// 评论所属的目标路径 (例如 /posts/some-slug)
	TargetPath string `json:"target_path,omitempty"`
	// 目标页面的标题，用于在后台管理界面展示
	TargetTitle *string `json:"target_title,omitempty"`
	// 关联的用户ID (如果是登录用户)
	UserID *uint `json:"user_id,omitempty"`
	// 父评论ID (用于嵌套回复)
	ParentID *uint `json:"parent_id,omitempty"`
	// 评论者昵称
	Nickname string `json:"nickname,omitempty"`
	// 评论者邮箱 (用于接收回复通知)
	Email *string `json:"email,omitempty"`
	// 邮箱的MD5哈希值 (用于Gravatar头像)
	EmailMd5 string `json:"email_md5,omitempty"`
	// 评论者个人网站链接
	Website *string `json:"website,omitempty"`
	// 评论内容 (Markdown格式)
	Content string `json:"content,omitempty"`
	// 经后端安全处理后的HTML格式评论内容
	ContentHTML string `json:"content_html,omitempty"`
	// 评论状态 1:已发布 2:待审核
	Status int `json:"status,omitempty"`
	// 是否为博主/管理员的评论
	IsAdminComment bool `json:"is_admin_comment,omitempty"`
	// 是否为匿名评论（使用匿名邮箱发表的评论）
	IsAnonymous bool `json:"is_anonymous,omitempty"`
	// 用户是否同意接收回复邮件通知
	AllowNotification bool `json:"allow_notification,omitempty"`
	// 评论者的 User Agent 信息
	UserAgent *string `json:"user_agent,omitempty"`
	// 评论者的IP地址
	IPAddress string `json:"ip_address,omitempty"`
	// IP地址归属地
	IPLocation *string `json:"ip_location,omitempty"`
	// 点赞数
	LikeCount int `json:"like_count,omitempty"`
	// 评论置顶时间，为NULL表示未置顶
	PinnedAt *time.Time `json:"pinned_at,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the CommentQuery when eager-loading is set.
	Edges            CommentEdges `json:"edges"`
	article_comments *uint
	selectValues     sql.SelectValues
}

// CommentEdges holds the relations/edges for other nodes in the graph.
type CommentEdges struct {
	// User holds the value of the user edge.
	User *User `json:"user,omitempty"`
	// Children holds the value of the children edge.
	Children *Comment `json:"children,omitempty"`
	// Parent holds the value of the parent edge.
	Parent []*Comment `json:"parent,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [3]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 CommentEdges) 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"}
}

// ChildrenOrErr returns the Children value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e CommentEdges) ChildrenOrErr() (*Comment, error) {
	if e.Children != nil {
		return e.Children, nil
	} else if e.loadedTypes[1] {
		return nil, &NotFoundError{label: comment.Label}
	}
	return nil, &NotLoadedError{edge: "children"}
}

// ParentOrErr returns the Parent value or an error if the edge
// was not loaded in eager-loading.
func (e CommentEdges) ParentOrErr() ([]*Comment, error) {
	if e.loadedTypes[2] {
		return e.Parent, nil
	}
	return nil, &NotLoadedError{edge: "parent"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*Comment) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case comment.FieldIsAdminComment, comment.FieldIsAnonymous, comment.FieldAllowNotification:
			values[i] = new(sql.NullBool)
		case comment.FieldID, comment.FieldUserID, comment.FieldParentID, comment.FieldStatus, comment.FieldLikeCount:
			values[i] = new(sql.NullInt64)
		case comment.FieldTargetPath, comment.FieldTargetTitle, comment.FieldNickname, comment.FieldEmail, comment.FieldEmailMd5, comment.FieldWebsite, comment.FieldContent, comment.FieldContentHTML, comment.FieldUserAgent, comment.FieldIPAddress, comment.FieldIPLocation:
			values[i] = new(sql.NullString)
		case comment.FieldDeletedAt, comment.FieldCreatedAt, comment.FieldUpdatedAt, comment.FieldPinnedAt:
			values[i] = new(sql.NullTime)
		case comment.ForeignKeys[0]: // article_comments
			values[i] = new(sql.NullInt64)
		default:
			values[i] = new(sql.UnknownType)
		}
	}
	return values, nil
}

// assignValues assigns the values that were returned from sql.Rows (after scanning)
// to the Comment fields.
func (_m *Comment) 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 comment.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 comment.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 comment.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 comment.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 comment.FieldTargetPath:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field target_path", values[i])
			} else if value.Valid {
				_m.TargetPath = value.String
			}
		case comment.FieldTargetTitle:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field target_title", values[i])
			} else if value.Valid {
				_m.TargetTitle = new(string)
				*_m.TargetTitle = value.String
			}
		case comment.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 = new(uint)
				*_m.UserID = uint(value.Int64)
			}
		case comment.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 comment.FieldNickname:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field nickname", values[i])
			} else if value.Valid {
				_m.Nickname = value.String
			}
		case comment.FieldEmail:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field email", values[i])
			} else if value.Valid {
				_m.Email = new(string)
				*_m.Email = value.String
			}
		case comment.FieldEmailMd5:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field email_md5", values[i])
			} else if value.Valid {
				_m.EmailMd5 = value.String
			}
		case comment.FieldWebsite:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field website", values[i])
			} else if value.Valid {
				_m.Website = new(string)
				*_m.Website = value.String
			}
		case comment.FieldContent:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field content", values[i])
			} else if value.Valid {
				_m.Content = value.String
			}
		case comment.FieldContentHTML:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field content_html", values[i])
			} else if value.Valid {
				_m.ContentHTML = value.String
			}
		case comment.FieldStatus:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field status", values[i])
			} else if value.Valid {
				_m.Status = int(value.Int64)
			}
		case comment.FieldIsAdminComment:
			if value, ok := values[i].(*sql.NullBool); !ok {
				return fmt.Errorf("unexpected type %T for field is_admin_comment", values[i])
			} else if value.Valid {
				_m.IsAdminComment = value.Bool
			}
		case comment.FieldIsAnonymous:
			if value, ok := values[i].(*sql.NullBool); !ok {
				return fmt.Errorf("unexpected type %T for field is_anonymous", values[i])
			} else if value.Valid {
				_m.IsAnonymous = value.Bool
			}
		case comment.FieldAllowNotification:
			if value, ok := values[i].(*sql.NullBool); !ok {
				return fmt.Errorf("unexpected type %T for field allow_notification", values[i])
			} else if value.Valid {
				_m.AllowNotification = value.Bool
			}
		case comment.FieldUserAgent:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field user_agent", values[i])
			} else if value.Valid {
				_m.UserAgent = new(string)
				*_m.UserAgent = value.String
			}
		case comment.FieldIPAddress:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field ip_address", values[i])
			} else if value.Valid {
				_m.IPAddress = value.String
			}
		case comment.FieldIPLocation:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field ip_location", values[i])
			} else if value.Valid {
				_m.IPLocation = new(string)
				*_m.IPLocation = value.String
			}
		case comment.FieldLikeCount:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field like_count", values[i])
			} else if value.Valid {
				_m.LikeCount = int(value.Int64)
			}
		case comment.FieldPinnedAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field pinned_at", values[i])
			} else if value.Valid {
				_m.PinnedAt = new(time.Time)
				*_m.PinnedAt = value.Time
			}
		case comment.ForeignKeys[0]:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for edge-field article_comments", value)
			} else if value.Valid {
				_m.article_comments = new(uint)
				*_m.article_comments = 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 Comment.
// This includes values selected through modifiers, order, etc.
func (_m *Comment) Value(name string) (ent.Value, error) {
	return _m.selectValues.Get(name)
}

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

// QueryChildren queries the "children" edge of the Comment entity.
func (_m *Comment) QueryChildren() *CommentQuery {
	return NewCommentClient(_m.config).QueryChildren(_m)
}

// QueryParent queries the "parent" edge of the Comment entity.
func (_m *Comment) QueryParent() *CommentQuery {
	return NewCommentClient(_m.config).QueryParent(_m)
}

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

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

// String implements the fmt.Stringer.
func (_m *Comment) String() string {
	var builder strings.Builder
	builder.WriteString("Comment(")
	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("target_path=")
	builder.WriteString(_m.TargetPath)
	builder.WriteString(", ")
	if v := _m.TargetTitle; v != nil {
		builder.WriteString("target_title=")
		builder.WriteString(*v)
	}
	builder.WriteString(", ")
	if v := _m.UserID; v != nil {
		builder.WriteString("user_id=")
		builder.WriteString(fmt.Sprintf("%v", *v))
	}
	builder.WriteString(", ")
	if v := _m.ParentID; v != nil {
		builder.WriteString("parent_id=")
		builder.WriteString(fmt.Sprintf("%v", *v))
	}
	builder.WriteString(", ")
	builder.WriteString("nickname=")
	builder.WriteString(_m.Nickname)
	builder.WriteString(", ")
	if v := _m.Email; v != nil {
		builder.WriteString("email=")
		builder.WriteString(*v)
	}
	builder.WriteString(", ")
	builder.WriteString("email_md5=")
	builder.WriteString(_m.EmailMd5)
	builder.WriteString(", ")
	if v := _m.Website; v != nil {
		builder.WriteString("website=")
		builder.WriteString(*v)
	}
	builder.WriteString(", ")
	builder.WriteString("content=")
	builder.WriteString(_m.Content)
	builder.WriteString(", ")
	builder.WriteString("content_html=")
	builder.WriteString(_m.ContentHTML)
	builder.WriteString(", ")
	builder.WriteString("status=")
	builder.WriteString(fmt.Sprintf("%v", _m.Status))
	builder.WriteString(", ")
	builder.WriteString("is_admin_comment=")
	builder.WriteString(fmt.Sprintf("%v", _m.IsAdminComment))
	builder.WriteString(", ")
	builder.WriteString("is_anonymous=")
	builder.WriteString(fmt.Sprintf("%v", _m.IsAnonymous))
	builder.WriteString(", ")
	builder.WriteString("allow_notification=")
	builder.WriteString(fmt.Sprintf("%v", _m.AllowNotification))
	builder.WriteString(", ")
	if v := _m.UserAgent; v != nil {
		builder.WriteString("user_agent=")
		builder.WriteString(*v)
	}
	builder.WriteString(", ")
	builder.WriteString("ip_address=")
	builder.WriteString(_m.IPAddress)
	builder.WriteString(", ")
	if v := _m.IPLocation; v != nil {
		builder.WriteString("ip_location=")
		builder.WriteString(*v)
	}
	builder.WriteString(", ")
	builder.WriteString("like_count=")
	builder.WriteString(fmt.Sprintf("%v", _m.LikeCount))
	builder.WriteString(", ")
	if v := _m.PinnedAt; v != nil {
		builder.WriteString("pinned_at=")
		builder.WriteString(v.Format(time.ANSIC))
	}
	builder.WriteByte(')')
	return builder.String()
}

// Comments is a parsable slice of Comment.
type Comments []*Comment
