package main

import (
	"log"
	"verification-platform-backend/internal/database"
	"verification-platform-backend/internal/model"
)

func main() {
	database.Init()

	log.Println("=== 修复套餐数据 ===")

	var pkg model.Package
	if err := database.DB.First(&pkg, 1).Error; err != nil {
		log.Printf("查询套餐失败: %v\n", err)
		return
	}

	log.Printf("修复前: ID=%d, Name='%s', Price=%.2f, Period='%s', Status='%s'\n",
		pkg.ID, pkg.Name, pkg.Price, pkg.Period, pkg.Status)

	if pkg.Name == "" {
		pkg.Name = "免费版"
		pkg.Price = 0
		pkg.Period = "permanent"
		pkg.Status = "active"
		pkg.Description = "免费体验版本，功能有限"
		pkg.Sort = 0

		if err := database.DB.Save(&pkg).Error; err != nil {
			log.Printf("更新套餐失败: %v\n", err)
			return
		}

		log.Printf("修复后: ID=%d, Name='%s', Price=%.2f, Period='%s', Status='%s'\n",
			pkg.ID, pkg.Name, pkg.Price, pkg.Period, pkg.Status)

		var permission model.PackagePermission
		if err := database.DB.Where("package_id = ?", pkg.ID).First(&permission).Error; err != nil {
			log.Printf("查询权限失败: %v\n", err)
		} else {
			permission.MaxApplications = 1
			permission.MaxStorage = 100
			permission.MaxApiCalls = 1000
			permission.AllowAgent = false
			permission.AllowCloudData = false
			permission.AllowDynamicCode = false
			permission.PrioritySupport = false

			if err := database.DB.Save(&permission).Error; err != nil {
				log.Printf("更新权限失败: %v\n", err)
			} else {
				log.Printf("权限已更新: MaxApps=%d, MaxStorage=%d, MaxApiCalls=%d\n",
					permission.MaxApplications, permission.MaxStorage, permission.MaxApiCalls)
			}
		}
	} else {
		log.Println("套餐名称不为空，无需修复")
	}

	log.Println("套餐数据修复完成")
}
