package models

import (
	"time"
)

// Enterprise 企业表
type Enterprise struct {
	ID                   int       `gorm:"primaryKey;autoIncrement" json:"id"`
	EnterpriseID         string    `gorm:"type:varchar(255);not null;default:''" json:"enterprise_id" comment:"企业ID"`
	Name                 string    `gorm:"type:varchar(200);not null;default:''" json:"name" comment:"企业名称"`
	ShortName            string    `gorm:"type:varchar(100);not null;default:''" json:"short_name" comment:"企业简称"`
	Icon                 string    `gorm:"type:varchar(500);not null;default:''" json:"icon" comment:"企业图标URL"`
	Phone                string    `gorm:"type:varchar(20);not null;default:'';uniqueIndex:uk_phone" json:"phone" comment:"登录手机号"`
	Password             string    `gorm:"type:varchar(255);not null;default:''" json:"-" comment:"登录密码（加密存储）"`
	Email                string    `gorm:"type:varchar(128);not null;default:''" json:"email" comment:"企业邮箱"`
	Website              string    `gorm:"type:varchar(255);not null;default:''" json:"website" comment:"企业网站"`
	Address              string    `gorm:"type:varchar(255);not null;default:''" json:"address" comment:"企业地址"`
	Status               string    `gorm:"type:enum('active','disabled');not null;default:'active';index:idx_status" json:"status" comment:"状态"`
	UsersTotal           int       `gorm:"type:int(10) unsigned;not null;default:0" json:"users_total" comment:"员工总数"`
	ProductsTotal        int       `gorm:"type:int(10) unsigned;not null;default:0" json:"products_total" comment:"产品总数"`
	ArticlesTotal        int       `gorm:"type:int(10) unsigned;not null;default:0" json:"articles_total" comment:"文章总数"`
	ReleasedMonthTotal   int       `gorm:"type:int(10) unsigned;not null;default:0" json:"released_month_total" comment:"本月发布数量"`
	LinkedToXHSNum       int       `gorm:"type:int(10) unsigned;not null;default:0" json:"linked_to_xhs_num" comment:"绑定小红书"`
	CreatedAt            time.Time `gorm:"index:idx_created_at" json:"created_at" comment:"创建时间"`
	UpdatedAt            time.Time `json:"updated_at" comment:"更新时间"`
}

// User 用户账号表（原Employee，对应ai_users）
type User struct {
	ID             int        `gorm:"primaryKey;autoIncrement" json:"id"`
	EnterpriseID   int        `gorm:"not null;default:0;index:idx_enterprise_id" json:"enterprise_id" comment:"所属企业ID"`
	Enterprise     Enterprise `gorm:"foreignKey:EnterpriseID" json:"enterprise,omitempty"`
	EnterpriseName string     `gorm:"type:varchar(255);not null;default:''" json:"enterprise_name" comment:"企业名称"`
	Username       string     `gorm:"type:varchar(50);not null;default:'';uniqueIndex:uk_username" json:"username" comment:"用户名"`
	Password       string     `gorm:"type:varchar(255);not null;default:''" json:"-" comment:"密码"`
	RealName       string     `gorm:"type:varchar(50)" json:"real_name" comment:"真实姓名"`
	Email          string     `gorm:"type:varchar(100)" json:"email" comment:"邮箱"`
	Phone          string     `gorm:"type:varchar(20)" json:"phone" comment:"手机号"`
	WechatOpenID   *string    `gorm:"type:varchar(100);uniqueIndex:uk_wechat_openid" json:"wechat_openid,omitempty" comment:"微信OpenID"`
	WechatUnionID  *string    `gorm:"type:varchar(100)" json:"wechat_unionid,omitempty" comment:"微信UnionID"`
	XHSPhone       string     `gorm:"type:varchar(20);not null;default:''" json:"xhs_phone" comment:"小红书绑定手机号"`
	XHSAccount     string     `gorm:"type:varchar(255);not null;default:''" json:"xhs_account" comment:"小红书账号名称"`
	XHSCookie      string     `gorm:"type:text" json:"xhs_cookie" comment:"小红书Cookie"`
	IsBoundXHS     int        `gorm:"type:tinyint(1);not null;default:0;index:idx_is_bound_xhs" json:"is_bound_xhs" comment:"是否绑定小红书：0=未绑定，1=已绑定"`
	BoundAt        *time.Time `json:"bound_at" comment:"绑定小红书的时间"`
	Department     string     `gorm:"type:varchar(50)" json:"department" comment:"部门"`
	Role           string     `gorm:"type:enum('admin','editor','reviewer','publisher','each_title_reviewer');default:'editor'" json:"role" comment:"角色"`
	Status         string     `gorm:"type:enum('active','inactive','deleted');default:'active';index:idx_status" json:"status" comment:"状态"`
	CreatedAt      time.Time  `json:"created_at" comment:"创建时间"`
	UpdatedAt      time.Time  `json:"updated_at" comment:"更新时间"`
}

// Employee 员工表别名，兼容旧代码
type Employee = User

// Product 产品表
type Product struct {
	ID                int       `gorm:"primaryKey;autoIncrement" json:"id"`
	EnterpriseID      int       `gorm:"not null;default:0;index:idx_enterprise_id" json:"enterprise_id" comment:"所属企业ID"`
	Name              string    `gorm:"type:varchar(200);not null;default:''" json:"name" comment:"产品名称"`
	TypeName          string    `gorm:"type:varchar(128);not null;default:''" json:"type_name" comment:"产品类型"`
	ImageURL          string    `gorm:"type:varchar(500);not null;default:''" json:"image_url" comment:"产品主图URL"`
	ImageThumbnailURL string    `gorm:"type:varchar(500);not null;default:''" json:"image_thumbnail_url" comment:"缩略图URL"`
	Knowledge         string    `gorm:"type:text" json:"knowledge" comment:"产品知识库（纯文字）"`
	ArticlesTotal     int       `gorm:"not null;default:0" json:"articles_total" comment:"文章总数"`
	PublishedTotal    int       `gorm:"not null;default:0" json:"published_total" comment:"发布总数"`
	Status            string    `gorm:"type:enum('draft','active','deleted');not null;default:'draft';index:idx_status" json:"status" comment:"状态：draft=草稿，active=正常，deleted=已删除"`
	CreatedAt         time.Time `gorm:"index:idx_created_at" json:"created_at" comment:"创建时间"`
	UpdatedAt         time.Time `json:"updated_at" comment:"更新时间"`
}

// Article 文章表（原Copy，对应ai_articles）
type Article struct {
	ID                 int        `gorm:"primaryKey;autoIncrement" json:"id"`
	BatchID            uint64     `gorm:"type:bigint unsigned;not null;default:0" json:"batch_id" comment:"批次ID"`
	EnterpriseID       int        `gorm:"not null;default:0;index:idx_enterprise_id" json:"enterprise_id" comment:"所属企业ID"`
	ProductID          int        `gorm:"not null;default:0;index:idx_product_id" json:"product_id" comment:"关联产品ID"`
	TopicTypeID        int        `gorm:"type:int unsigned;not null;default:0" json:"topic_type_id" comment:"topic类型ID"`
	PromptWorkflowID   int        `gorm:"type:int unsigned;not null;default:0" json:"prompt_workflow_id" comment:"提示词工作流ID"`
	Topic              string     `gorm:"type:varchar(255);not null;default:''" json:"topic" comment:"topic主题"`
	Title              string     `gorm:"type:varchar(200);not null;default:''" json:"title" comment:"标题"`
	Content            string     `gorm:"type:text" json:"content" comment:"文章内容"`
	Department         string     `gorm:"type:varchar(255);not null;default:''" json:"department" comment:"部门"`
	DepartmentIDs      string     `gorm:"column:departmentids;type:varchar(255);not null;default:''" json:"department_ids" comment:"部门IDs"`
	AuthorID           *int       `json:"author_id" comment:"作者ID"`
	AuthorName         string     `gorm:"type:varchar(100)" json:"author_name" comment:"作者名称"`
	DepartmentID       *int       `json:"department_id" comment:"部门ID"`
	DepartmentName     string     `gorm:"type:varchar(255)" json:"department_name" comment:"部门名称"`
	CreatedUserID      int        `gorm:"not null;default:0" json:"created_user_id" comment:"创建用户ID"`
	ReviewUserID       *int       `json:"review_user_id" comment:"审核用户ID"`
	PublishUserID      *int       `json:"publish_user_id" comment:"发布用户ID"`
	Status             string     `gorm:"type:enum('topic','cover_image','generate','generate_failed','draft','pending_review','approved','rejected','published_review','published','failed');default:'draft';index:idx_status" json:"status" comment:"状态"`
	Channel            int        `gorm:"type:tinyint(1);not null;default:1" json:"channel" comment:"渠道：1=baidu|2=toutiao|3=weixin"`
	ReviewComment      string     `gorm:"type:text" json:"review_comment" comment:"审核评论"`
	PublishTime        *time.Time `json:"publish_time" comment:"发布时间"`
	BaijiahaoID        string     `gorm:"type:varchar(100)" json:"baijiahao_id" comment:"百家号ID"`
	BaijiahaoStatus    string     `gorm:"type:varchar(50)" json:"baijiahao_status" comment:"百家号状态"`
	WordCount          int        `gorm:"default:0" json:"word_count" comment:"字数统计"`
	ImageCount         int        `gorm:"default:0" json:"image_count" comment:"图片数量"`
	CozeTag            string     `gorm:"type:varchar(500)" json:"coze_tag" comment:"Coze生成的标签"`
	CreatedAt          time.Time  `gorm:"index:idx_created_at" json:"created_at" comment:"创建时间"`
	UpdatedAt          time.Time  `gorm:"index:idx_updated_at" json:"updated_at" comment:"更新时间"`
}

// Copy 文案表别名，兼容旧代码
type Copy = Article

// PublishRecord 发布记录表（对应ai_article_published_records）
type PublishRecord struct {
	ID             int        `gorm:"primaryKey;autoIncrement" json:"id"`
	ArticleID      *int       `gorm:"index:idx_article_id" json:"article_id" comment:"文章ID"`
	EnterpriseID   int        `gorm:"not null;default:0;index:idx_enterprise_id" json:"enterprise_id" comment:"所属企业ID"`
	ProductID      int        `gorm:"not null;default:0;index:idx_product_id" json:"product_id" comment:"关联产品ID"`
	Topic          string     `gorm:"type:varchar(255);not null;default:''" json:"topic" comment:"topic主题"`
	Title          string     `gorm:"type:varchar(200);not null;default:''" json:"title" comment:"标题"`
	CreatedUserID  int        `gorm:"not null;default:0;index:idx_created_user_id" json:"created_user_id" comment:"创建用户ID"`
	ReviewUserID   *int       `json:"review_user_id" comment:"审核用户ID"`
	PublishUserID  *int       `json:"publish_user_id" comment:"发布用户ID"`
	Status         string     `gorm:"type:enum('topic','cover_image','generate','generate_failed','draft','pending_review','approved','rejected','published_review','published','failed');default:'draft';index:idx_status" json:"status" comment:"状态"`
	Channel        int        `gorm:"type:tinyint(1);not null;default:1" json:"channel" comment:"渠道：1=baidu|2=toutiao|3=weixin"`
	ReviewComment  string     `gorm:"type:text" json:"review_comment" comment:"审核评论"`
	PublishTime    *time.Time `json:"publish_time" comment:"发布时间"`
	PublishLink    string     `gorm:"type:varchar(128);not null;default:''" json:"publish_link" comment:"发布访问链接"`
	WordCount      int        `gorm:"default:0" json:"word_count" comment:"字数统计"`
	ImageCount     int        `gorm:"default:0" json:"image_count" comment:"图片数量"`
	CreatedAt      time.Time  `gorm:"index:idx_created_at" json:"created_at" comment:"创建时间"`
	UpdatedAt      time.Time  `gorm:"index:idx_updated_at" json:"updated_at" comment:"更新时间"`
}

// XHSAccount 小红书账号表（保持兼容）
type XHSAccount struct {
	ID             int        `gorm:"primaryKey;autoIncrement" json:"id"`
	EmployeeID     int        `gorm:"not null;default:0;uniqueIndex:uk_employee_id" json:"employee_id"`
	Employee       User       `gorm:"foreignKey:EmployeeID" json:"employee,omitempty"`
	XHSUserID      string     `gorm:"type:varchar(100);not null;default:'';index:idx_xhs_user_id" json:"xhs_user_id"`
	XHSNickname    string     `gorm:"type:varchar(100);not null;default:''" json:"xhs_nickname"`
	XHSPhone       string     `gorm:"type:varchar(20);not null;default:''" json:"xhs_phone"`
	XHSAvatar      string     `gorm:"type:varchar(500);not null;default:''" json:"xhs_avatar"`
	FansCount      int        `gorm:"not null;default:0" json:"fans_count"`
	NotesCount     int        `gorm:"not null;default:0" json:"notes_count"`
	Cookies        string     `gorm:"type:text" json:"cookies"`
	AccessToken    string     `gorm:"type:varchar(500);not null;default:''" json:"access_token"`
	RefreshToken   string     `gorm:"type:varchar(500);not null;default:''" json:"refresh_token"`
	TokenExpireAt  *time.Time `json:"token_expire_at"`
	Status         string     `gorm:"type:enum('active','expired','banned');default:'active';index:idx_status" json:"status"`
	CreatedAt      time.Time  `json:"created_at"`
	UpdatedAt      time.Time  `json:"updated_at"`
}

// PromptWorkflow 提示词工作流表
type PromptWorkflow struct {
	ID                   int       `gorm:"primaryKey;autoIncrement" json:"id"`
	EnterpriseID         int       `gorm:"not null;default:0;index:idx_enterprise_id" json:"enterprise_id" comment:"所属企业ID"`
	PromptWorkflowName   string    `gorm:"type:varchar(100);not null;default:''" json:"prompt_workflow_name" comment:"提示词工作流名称"`
	AuthToken            string    `gorm:"type:varchar(100);not null;default:''" json:"auth_token" comment:"认证Token"`
	WorkflowID           string    `gorm:"type:varchar(100);not null;default:'';index:idx_workflow_id" json:"workflow_id" comment:"工作流ID"`
	Content              string    `gorm:"type:text" json:"content" comment:"提示词内容"`
	UsageCount           int       `gorm:"not null;default:0" json:"usage_count" comment:"使用次数统计"`
	CreatedUserID        int       `gorm:"not null;default:0" json:"created_user_id" comment:"创建用户ID"`
	CreatedAt            time.Time `gorm:"index:idx_created_at" json:"created_at" comment:"创建时间"`
	UpdatedAt            time.Time `json:"updated_at" comment:"更新时间"`
}

// ProductImage 产品图片库表
type ProductImage struct {
	ID               int       `gorm:"primaryKey;autoIncrement" json:"id"`
	EnterpriseID     int       `gorm:"not null;default:0;index:idx_enterprise_id" json:"enterprise_id" comment:"所属企业ID"`
	ProductID        int       `gorm:"not null;default:0;index:idx_product_id" json:"product_id" comment:"关联产品ID"`
	ImageID          int       `gorm:"not null;default:0" json:"image_id" comment:"图片ID"`
	ImageName        string    `gorm:"type:varchar(255);not null;default:''" json:"image_name" comment:"图片名称"`
	ImageURL         string    `gorm:"type:varchar(500);not null;default:''" json:"image_url" comment:"图片URL"`
	ThumbnailURL     string    `gorm:"type:varchar(500);not null;default:''" json:"thumbnail_url" comment:"缩略图URL"`
	TypeName         string    `gorm:"type:varchar(50);not null;default:''" json:"type_name" comment:"图片类型"`
	Description      string    `gorm:"type:varchar(500);not null;default:''" json:"description" comment:"图片描述"`
	FileSize         *int64    `json:"file_size" comment:"文件大小"`
	Width            *int      `json:"width" comment:"图片宽度"`
	Height           *int      `json:"height" comment:"图片高度"`
	UploadUserID     int       `gorm:"not null;default:0" json:"upload_user_id" comment:"上传用户ID"`
	Status           string    `gorm:"type:enum('active','deleted');default:'active';index:idx_status" json:"status" comment:"状态"`
	CreatedAt        time.Time `gorm:"index:idx_created_at" json:"created_at" comment:"上传时间"`
	UpdatedAt        time.Time `json:"updated_at" comment:"更新时间"`
}

// ArticleImage 文章图片表
type ArticleImage struct {
	ID             int       `gorm:"primaryKey;autoIncrement" json:"id"`
	ArticleID      int       `gorm:"not null;default:0;index:idx_article_id" json:"article_id" comment:"文章ID"`
	ImageID        int       `gorm:"not null;default:0;index:idx_image_id" json:"image_id" comment:"图片ID"`
	ImageURL       string    `gorm:"type:varchar(500);not null;default:''" json:"image_url" comment:"图片URL"`
	ImageThumbURL  string    `gorm:"type:varchar(255);not null;default:''" json:"image_thumb_url" comment:"缩略图URL"`
	ImageTagID     int       `gorm:"not null;default:0" json:"image_tag_id" comment:"图片标签ID"`
	SortOrder      int       `gorm:"default:0" json:"sort_order" comment:"排序"`
	KeywordsID     int       `gorm:"not null;default:0" json:"keywords_id" comment:"关键词ID"`
	KeywordsName   string    `gorm:"type:varchar(255);not null;default:''" json:"keywords_name" comment:"关键词名称"`
	DepartmentID   int       `gorm:"not null;default:0" json:"department_id" comment:"部门ID"`
	DepartmentName string    `gorm:"type:varchar(255);not null;default:''" json:"department_name" comment:"部门名称"`
	ImageSource    int       `gorm:"type:tinyint(1);not null;default:0" json:"image_source" comment:"图片来源：1=tag|2=change"`
	CreatedAt      time.Time `gorm:"index:idx_created_at" json:"created_at" comment:"创建时间"`
	UpdatedAt      time.Time `json:"updated_at" comment:"更新时间"`
}

// ArticleTag 文章标签表
type ArticleTag struct {
	ID        int       `gorm:"primaryKey;autoIncrement" json:"id"`
	ArticleID int       `gorm:"not null;default:0;uniqueIndex:uk_article_tag" json:"article_id" comment:"文章ID"`
	CozeTag   string    `gorm:"type:varchar(500)" json:"coze_tag" comment:"Coze生成的标签"`
	CreatedAt time.Time `gorm:"index:idx_created_at" json:"created_at" comment:"创建时间"`
}

// DataStatistics 数据统计表
type DataStatistics struct {
	ID                      int       `gorm:"primaryKey;autoIncrement" json:"id"`
	EnterpriseID            int       `gorm:"not null;default:0;index:idx_enterprise_id" json:"enterprise_id" comment:"所属企业ID"`
	ProductID               int       `gorm:"not null;default:0;index:idx_product_id" json:"product_id" comment:"关联产品ID"`
	CumulativeReleasesNum   int       `gorm:"type:int(10) unsigned;not null;default:0" json:"cumulative_releases_num" comment:"累计发布"`
	PublishedTodayNum       int       `gorm:"type:int(10) unsigned;not null;default:0" json:"published_today_num" comment:"今日发布"`
	PublishedWeekNum        int       `gorm:"type:int(10) unsigned;not null;default:0" json:"published_week_num" comment:"本周发布"`
	ParticipatingEmployees  int       `gorm:"type:int(10) unsigned;not null;default:0" json:"participating_employees_num" comment:"参与员工"`
	CreatedAt               time.Time `gorm:"index:idx_created_at" json:"created_at" comment:"创建时间"`
	UpdatedAt               time.Time `json:"updated_at" comment:"更新时间"`
}

// Log 操作日志表（对应ai_logs）
type Log struct {
	ID           int       `gorm:"primaryKey;autoIncrement" json:"id"`
	UserID       *int      `gorm:"index:idx_user_id" json:"user_id" comment:"用户ID"`
	Action       string    `gorm:"type:varchar(100);not null;default:'';index:idx_action" json:"action" comment:"操作动作"`
	TargetType   string    `gorm:"type:varchar(50)" json:"target_type" comment:"目标类型"`
	TargetID     *int      `json:"target_id" comment:"目标ID"`
	Description  string    `gorm:"type:text" json:"description" comment:"描述"`
	IPAddress    string    `gorm:"type:varchar(45)" json:"ip_address" comment:"IP地址"`
	UserAgent    string    `gorm:"type:text" json:"user_agent" comment:"用户代理"`
	RequestData  string    `gorm:"type:json" json:"request_data" comment:"请求数据"`
	ResponseData string    `gorm:"type:json" json:"response_data" comment:"响应数据"`
	Status       string    `gorm:"type:enum('success','error','warning');default:'success';index:idx_status" json:"status" comment:"状态"`
	ErrorMessage string    `gorm:"type:text" json:"error_message" comment:"错误消息"`
	CreatedAt    time.Time `gorm:"index:idx_created_at" json:"created_at" comment:"创建时间"`
}

// TableName 指定表名（带ai_前缀）
func (Enterprise) TableName() string {
	return "ai_enterprises"
}

func (User) TableName() string {
	return "ai_users"
}

func (Product) TableName() string {
	return "ai_products"
}

func (Article) TableName() string {
	return "ai_articles"
}

func (XHSAccount) TableName() string {
	return "wht_xhs_accounts" // 保持兼容旧表
}

func (PublishRecord) TableName() string {
	return "ai_article_published_records"
}

func (PromptWorkflow) TableName() string {
	return "ai_prompt_workflow"
}

func (ProductImage) TableName() string {
	return "ai_product_images"
}

func (ArticleImage) TableName() string {
	return "ai_article_images"
}

func (ArticleTag) TableName() string {
	return "ai_article_tags"
}

func (DataStatistics) TableName() string {
	return "ai_data_statistics"
}

func (Log) TableName() string {
	return "ai_logs"
}
