package migrate

import (
	"github.com/pingcap/log"
	"gorm.io/gorm"

	"github.com/tidbcloud/auth-server/internal/entity"
)

// models owned by auth-server
// NOTE: if any new table added (and owned by auth-server), do not forget to add them here.
var models = []interface{}{
	// oauth2
	&entity.Oauth2Application{}, &entity.Oauth2DeviceGrant{}, &entity.Oauth2AccessToken{},
	&entity.Oauth2CodeGrant{},
}

func initAuthServerDB(db *gorm.DB) error {
	log.L().Info("init empty database with data")
	// create the tables in an empty database
	// it is 3 time faster than migrate model individually
	// NOTE: not all models defined in `model.go` are owned by auth-server, so we should NOT do the migration for them.
	if err := db.AutoMigrate(models...); err != nil {
		return err
	}
	log.L().Info("after create database tables")
	return nil
}
