package models

import (
	"time"

	"gorm.io/gorm"
)

// AttachmentCategory 附件分类模型
type AttachmentCategory struct {
	ID          uint   `json:"id" gorm:"primaryKey;column:id"`
	Name        string `json:"name" gorm:"column:name"`
	Description string `json:"description" gorm:"column:description;type:text"`
	SortOrder   uint   `json:"sortOrder" gorm:"column:sort_order;default:0"`
	DeletedAt   int64  `json:"deletedAt" gorm:"column:deleted_at;default:0"`
	CreatedAt   int64  `json:"createdAt" gorm:"column:created_at"`
	UpdatedAt   int64  `json:"updatedAt" gorm:"column:updated_at"`
}

// TableName 指定表名
func (AttachmentCategory) TableName() string {
	return "attachment_categories"
}

// BeforeCreate 创建前钩子
func (ac *AttachmentCategory) BeforeCreate(tx *gorm.DB) error {
	now := time.Now().Unix()
	if ac.CreatedAt == 0 {
		ac.CreatedAt = now
	}
	if ac.UpdatedAt == 0 {
		ac.UpdatedAt = now
	}
	if ac.DeletedAt == 0 {
		ac.DeletedAt = 0
	}
	return nil
}

// BeforeUpdate 更新前钩子
func (ac *AttachmentCategory) BeforeUpdate(tx *gorm.DB) error {
	ac.UpdatedAt = time.Now().Unix()
	return nil
}

// Attachment 附件模型
type Attachment struct {
	ID           uint                `json:"id" gorm:"primaryKey;column:id"`
	CategoryID   *uint               `json:"categoryId,omitempty" gorm:"column:category_id"`
	Category     *AttachmentCategory `json:"category,omitempty" gorm:"foreignKey:CategoryID"`
	OriginalName string              `json:"originalName" gorm:"column:original_name"`
	StoredName   string              `json:"storedName" gorm:"column:stored_name"`
	FilePath     string              `json:"filePath" gorm:"column:file_path"`
	FileURL      string              `json:"fileUrl" gorm:"column:file_url"`
	FileSize     int64               `json:"fileSize" gorm:"column:file_size"`
	FileType     string              `json:"fileType" gorm:"column:file_type"` // image/video/document/other
	MimeType     string              `json:"mimeType" gorm:"column:mime_type"`
	StorageType  string              `json:"storageType" gorm:"column:storage_type;default:local"` // local/qcloud/aliyun/qiniu
	OSSConfigID  *uint               `json:"ossConfigId,omitempty" gorm:"column:oss_config_id"`
	DeletedAt    int64               `json:"deletedAt" gorm:"column:deleted_at;default:0"`
	CreatedAt    int64               `json:"createdAt" gorm:"column:created_at"`
	UpdatedAt    int64               `json:"updatedAt" gorm:"column:updated_at"`
}

// TableName 指定表名
func (Attachment) TableName() string {
	return "attachments"
}

// BeforeCreate 创建前钩子
func (a *Attachment) BeforeCreate(tx *gorm.DB) error {
	now := time.Now().Unix()
	if a.CreatedAt == 0 {
		a.CreatedAt = now
	}
	if a.UpdatedAt == 0 {
		a.UpdatedAt = now
	}
	if a.DeletedAt == 0 {
		a.DeletedAt = 0
	}
	return nil
}

// BeforeUpdate 更新前钩子
func (a *Attachment) BeforeUpdate(tx *gorm.DB) error {
	a.UpdatedAt = time.Now().Unix()
	return nil
}

// OSSConfig OSS配置模型
type OSSConfig struct {
	ID          uint   `json:"id" gorm:"primaryKey;column:id"`
	Name        string `json:"name" gorm:"column:name"`
	StorageType string `json:"storageType" gorm:"column:storage_type"`                                   // local/qcloud/aliyun/qiniu
	AccessKey   string `json:"accessKey" gorm:"column:access_key;type:varchar(255);default:'';not null"` // AES加密存储，默认空字符串（向后兼容）
	SecretKey   string `json:"secretKey" gorm:"column:secret_key;type:varchar(255);default:'';not null"` // AES加密存储，默认空字符串（向后兼容）
	Bucket      string `json:"bucket" gorm:"column:bucket"`                                              // 向后兼容
	Region      string `json:"region" gorm:"column:region"`                                              // 向后兼容
	Domain      string `json:"domain" gorm:"column:domain"`                                              // 向后兼容

	// 阿里云OSS专用字段
	OSSAccessKeyID     string `json:"ossAccessKeyId" gorm:"column:oss_access_key_id;type:varchar(255);default:'';not null"`
	OSSAccessKeySecret string `json:"ossAccessKeySecret" gorm:"column:oss_access_key_secret;type:varchar(255);default:'';not null"`
	OSSEndpoint        string `json:"ossEndpoint" gorm:"column:oss_endpoint;type:varchar(255);default:'';not null"`
	OSSBucket          string `json:"ossBucket" gorm:"column:oss_bucket;type:varchar(255);default:'';not null"`
	OSSDomain          string `json:"ossDomain" gorm:"column:oss_domain;type:varchar(255);default:'';not null"`

	// 腾讯云COS专用字段
	QCloudSecretID  string `json:"qcloudSecretId" gorm:"column:qcloud_secret_id;type:varchar(255);default:'';not null"`
	QCloudSecretKey string `json:"qcloudSecretKey" gorm:"column:qcloud_secret_key;type:varchar(255);default:'';not null"`
	QCloudRegion    string `json:"qcloudRegion" gorm:"column:qcloud_region;type:varchar(255);default:'';not null"`
	QCloudBucket    string `json:"qcloudBucket" gorm:"column:qcloud_bucket;type:varchar(255);default:'';not null"`
	QCloudDomain    string `json:"qcloudDomain" gorm:"column:qcloud_domain;type:varchar(255);default:'';not null"`

	// 七牛云专用字段
	QiniuAccessKey string `json:"qiniuAccessKey" gorm:"column:qiniu_access_key;type:varchar(255);default:'';not null"`
	QiniuSecretKey string `json:"qiniuSecretKey" gorm:"column:qiniu_secret_key;type:varchar(255);default:'';not null"`
	QiniuBucket    string `json:"qiniuBucket" gorm:"column:qiniu_bucket;type:varchar(255);default:'';not null"`
	QiniuRegion    string `json:"qiniuRegion" gorm:"column:qiniu_region;type:varchar(255);default:'';not null"`
	QiniuDomain    string `json:"qiniuDomain" gorm:"column:qiniu_domain;type:varchar(255);default:'';not null"`

	IsActive  int   `json:"isActive" gorm:"column:is_active;default:0"`
	DeletedAt int64 `json:"deletedAt" gorm:"column:deleted_at;default:0"`
	CreatedAt int64 `json:"createdAt" gorm:"column:created_at"`
	UpdatedAt int64 `json:"updatedAt" gorm:"column:updated_at"`
}

// TableName 指定表名
func (OSSConfig) TableName() string {
	return "oss_configs"
}

// BeforeCreate 创建前钩子
func (oc *OSSConfig) BeforeCreate(tx *gorm.DB) error {
	now := time.Now().Unix()
	if oc.CreatedAt == 0 {
		oc.CreatedAt = now
	}
	if oc.UpdatedAt == 0 {
		oc.UpdatedAt = now
	}
	if oc.DeletedAt == 0 {
		oc.DeletedAt = 0
	}
	return nil
}

// BeforeUpdate 更新前钩子
func (oc *OSSConfig) BeforeUpdate(tx *gorm.DB) error {
	oc.UpdatedAt = time.Now().Unix()
	return nil
}
