package database

import (
	"fmt"
	config "learnsteam/learsteam-quiz-api/configs"
	"learnsteam/learsteam-quiz-api/internal/models"

	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/schema"
)

type Database struct {
	*gorm.DB
}

var DB *gorm.DB

func Init() {
	DB = ConnectDB((config.DATABASE_URL))
}

func ConnectDB(url string) *gorm.DB {
	gorm_config := gorm.Config{NamingStrategy: schema.NamingStrategy{SingularTable: true}}
	db, err := gorm.Open(mysql.Open(url), &gorm_config)
	if err != nil {
		panic(err)
	}
	return db
}

func GetDB() *gorm.DB {
	return DB
}

func AutoMigrate() {
	fmt.Println("AugoMigrate")
	DB.AutoMigrate(
		&models.User{},
		&models.Token{},
		&models.Program{},
		&models.Quiz{},
	)
}
