package main

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

func main() {
	database.Init()

	log.Println("=== 初始化套餐数据 ===")

	var count int64
	database.DB.Model(&model.Package{}).Count(&count)
	log.Printf("当前套餐数量: %d\n", count)

	if count > 0 {
		log.Println("套餐数据已存在，跳过初始化")
		return
	}

	packages := []model.Package{
		{
			Name:          "基础版",
			Description:   "适合个人开发者和小型项目",
			Price:         9.9,
			Period:        "monthly",
			Status:        "active",
			Sort:          1,
			IsRecommended: false,
			AllowUpgrade:  true,
		},
		{
			Name:          "专业版",
			Description:   "适合中小型企业和成长型项目",
			Price:         29.9,
			Period:        "monthly",
			Status:        "active",
			Sort:          2,
			IsRecommended: true,
			AllowUpgrade:  true,
		},
		{
			Name:          "企业版",
			Description:   "适合大型企业和高流量项目",
			Price:         99.9,
			Period:        "monthly",
			Status:        "active",
			Sort:          3,
			IsRecommended: false,
			AllowUpgrade:  true,
		},
		{
			Name:          "永久版",
			Description:   "一次性购买，永久使用",
			Price:         999,
			Period:        "permanent",
			Status:        "active",
			Sort:          4,
			IsRecommended: false,
			AllowUpgrade:  false,
		},
	}

	for _, pkg := range packages {
		if err := database.DB.Create(&pkg).Error; err != nil {
			log.Printf("创建套餐失败: %v\n", err)
			continue
		}

		permission := model.PackagePermission{
			PackageID:        pkg.ID,
			MaxApplications:  5,
			MaxStorage:       1024,
			MaxApiCalls:      10000,
			AllowAgent:       false,
			AllowCloudData:   true,
			AllowDynamicCode: false,
			PrioritySupport:  false,
		}

		if pkg.Name == "专业版" {
			permission.MaxApplications = 20
			permission.MaxStorage = 5120
			permission.MaxApiCalls = 100000
			permission.AllowAgent = true
			permission.AllowDynamicCode = true
		} else if pkg.Name == "企业版" {
			permission.MaxApplications = -1
			permission.MaxStorage = -1
			permission.MaxApiCalls = -1
			permission.AllowAgent = true
			permission.AllowDynamicCode = true
			permission.PrioritySupport = true
		} else if pkg.Name == "永久版" {
			permission.MaxApplications = -1
			permission.MaxStorage = -1
			permission.MaxApiCalls = -1
			permission.AllowAgent = true
			permission.AllowDynamicCode = true
			permission.PrioritySupport = true
		}

		if err := database.DB.Create(&permission).Error; err != nil {
			log.Printf("创建套餐权限失败: %v\n", err)
		} else {
			log.Printf("创建套餐成功: %s (ID: %d)\n", pkg.Name, pkg.ID)
		}
	}

	log.Println("套餐初始化完成")
}
