package svc

import (
	plantModel "sundynix-micro-go/app/plant/model"
	"sundynix-micro-go/app/plant/rpc/internal/config"

	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

type ServiceContext struct {
	Config config.Config
	DB     *gorm.DB
}

func NewServiceContext(c config.Config) *ServiceContext {
	db, err := gorm.Open(mysql.Open(c.DB.DataSource), &gorm.Config{})
	if err != nil {
		logx.Errorf("连接数据库失败: %v", err)
		panic(err)
	}

	if err := db.AutoMigrate(
		&plantModel.UserProfile{},
		&plantModel.MyPlant{},
		&plantModel.MyPlantOss{},
		&plantModel.CarePlan{},
		&plantModel.CareRecord{},
		&plantModel.CareTask{},
		&plantModel.GrowthRecord{},
		&plantModel.Wiki{},
		&plantModel.WikiClass{},
		&plantModel.WikiOss{},
		&plantModel.WikiClassRelation{},
		&plantModel.WikiRelated{},
		&plantModel.UserStar{},
		&plantModel.Post{},
		&plantModel.PostComment{},
		&plantModel.PostLike{},
		&plantModel.PostOss{},
		&plantModel.Topic{},
		&plantModel.OcrLog{},
		&plantModel.MediaCheckResult{},
		&plantModel.ExchangeItem{},
		&plantModel.ExchangeOrder{},
		&plantModel.LevelConfig{},
		&plantModel.BadgeConfig{},
		&plantModel.UserBadge{},
		&plantModel.AiChatHistory{},
		&plantModel.GrowthRecordOss{},
		&plantModel.Banner{},
	); err != nil {
		logx.Errorf("数据库迁移失败: %v", err)
	}

	return &ServiceContext{Config: c, DB: db}
}
