// Filename: internal/db/migrations/migrations.go (全新)
package migrations

import (
	"gemini-balancer/internal/models"

	"github.com/sirupsen/logrus"
	"gorm.io/gorm"
)

// RunMigrations 负责执行所有的数据库模式迁移。
func RunMigrations(db *gorm.DB, logger *logrus.Logger) error {
	log := logger.WithField("component", "migrations")
	log.Info("Running database schema migrations...")
	// 集中管理所有需要被创建或更新的表。
	err := db.AutoMigrate(
		&models.UpstreamEndpoint{},
		&models.ProxyConfig{},
		&models.APIKey{},
		&models.KeyGroup{},
		&models.GroupModelMapping{},
		&models.AuthToken{},
		&models.RequestLog{},
		&models.StatsHourly{},
		&models.FileRecord{},
		&models.Setting{},
		&models.GroupSettings{},
		&models.GroupAPIKeyMapping{},
	)
	if err != nil {
		log.Errorf("Database schema migration failed: %v", err)
		return err
	}
	log.Info("Database schema migrations completed successfully.")
	return nil
}
