package model

// WorkConfig is the data persisted in each work's work_config.json.
// JSON field names are kept in Chinese to maintain backward compatibility
// with data files written by the original Python backend.
type WorkConfig struct {
	WorkID        string              `json:"作品ID"`
	WorkName      string              `json:"作品作品"`
	WorkDesc      string              `json:"作品描述"`
	Author        string              `json:"作者"`
	Version       string              `json:"作品版本号"`
	Category      string              `json:"作品分类"`
	Tags          []string            `json:"作品标签"`
	UploadTime    string              `json:"上传时间"`
	UpdateTime    string              `json:"更新时间"`
	Platforms     []string            `json:"支持平台"`
	FileNames     map[string][]string `json:"文件名称"`
	// 外部下载：每个平台可以配置多个外链下载（带别名）。
	ExternalDownloads map[string][]ExternalDownload `json:"外部下载,omitempty"`
	Screenshots   []string            `json:"作品截图"`
	VideoFiles    []string            `json:"作品视频"`
	Cover         string              `json:"作品封面"`
	Downloads     int                 `json:"作品下载量"`
	Views         int                 `json:"作品浏览量"`
	Likes         int                 `json:"作品点赞量"`
	UpdateCount   int                 `json:"作品更新次数"`
	OriginalNames map[string]string   `json:"原始文件名,omitempty"`
}

type ExternalDownload struct {
	Alias string `json:"别名"`
	URL   string `json:"链接"`
}

// Normalize replaces nil slices/maps with empty equivalents so JSON
// serialization produces [] / {} instead of null, matching Python behaviour.
func (w *WorkConfig) Normalize() {
	if w.Tags == nil {
		w.Tags = []string{}
	}
	if w.Platforms == nil {
		w.Platforms = []string{}
	}
	if w.FileNames == nil {
		w.FileNames = map[string][]string{}
	}
	if w.ExternalDownloads == nil {
		w.ExternalDownloads = map[string][]ExternalDownload{}
	}
	if w.Screenshots == nil {
		w.Screenshots = []string{}
	}
	if w.VideoFiles == nil {
		w.VideoFiles = []string{}
	}
}

// WorkResponse is a WorkConfig with dynamically computed link fields appended.
// These link fields are NEVER stored in work_config.json; they are built on
// the fly at response time.
type WorkResponse struct {
	WorkConfig
	// 下载链接 is always present (empty map when there are no download files).
	DownloadLinks map[string][]string `json:"下载链接"`
	// 下载资源：统一返回“本地下载 + 外部下载”，前端可直接按别名渲染按钮/链接。
	DownloadResources map[string][]DownloadResource `json:"下载资源"`
	ImageLinks    []string            `json:"图片链接,omitempty"`
	VideoLinks    []string            `json:"视频链接,omitempty"`
}

type DownloadResource struct {
	Type  string `json:"类型"` // local | external
	Alias string `json:"别名"`
	URL   string `json:"链接"`
}
