package main

import (
	"fmt"
	"log"

	"verification-platform-backend/internal/database"
	"verification-platform-backend/internal/model"
)

func main() {
	database.Init()
	db := database.DB

	var categories []model.DocCategory
	if err := db.Find(&categories).Error; err != nil {
		log.Fatal("获取分类失败:", err)
	}

	for _, cat := range categories {
		if cat.NameEn == "" && cat.Name != "" {
			cat.NameEn = cat.Name
		}
		if cat.DescriptionEn == "" && cat.Description != "" {
			cat.DescriptionEn = cat.Description
		}
		if err := db.Save(&cat).Error; err != nil {
			log.Printf("更新分类 %d 失败: %v", cat.ID, err)
		} else {
			fmt.Printf("已更新分类: %s\n", cat.Name)
		}
	}

	var docs []model.Doc
	if err := db.Find(&docs).Error; err != nil {
		log.Fatal("获取文档失败:", err)
	}

	for _, doc := range docs {
		if doc.TitleEn == "" && doc.Title != "" {
			doc.TitleEn = doc.Title
		}
		if doc.ContentEn == "" && doc.Content != "" {
			doc.ContentEn = doc.Content
		}
		if err := db.Save(&doc).Error; err != nil {
			log.Printf("更新文档 %d 失败: %v", doc.ID, err)
		} else {
			fmt.Printf("已更新文档: %s\n", doc.Title)
		}
	}

	fmt.Println("\n迁移完成!")
	fmt.Printf("更新了 %d 个分类\n", len(categories))
	fmt.Printf("更新了 %d 个文档\n", len(docs))
}
