package Database

import (
	"fmt"

	"gorm.io/driver/postgres"
	"gorm.io/gorm"

	"PersonalWebsite/Variables"
)

var (
	DB *gorm.DB
)

func InitDatabaseConn() error {
	var (
		dbConnString string
		e            error
	)
	// Connect to the postgres db
	// you might have to change the connection string to add your database credentials
	dbConnString = fmt.Sprintf(
		"host=%s port=%d dbname=%s user=%s password=%s %s",
		Variables.DbHost,
		Variables.DbPort,
		Variables.DbName,
		Variables.DbUser,
		Variables.DbPass,
		Variables.DbOpts,
	)

	DB, e = gorm.Open(
		postgres.Open(dbConnString),
		&gorm.Config{},
	)
	if e != nil {
		return e
	}

	e = MigrateDB()
	if e != nil {
		return e
	}

	return nil
}
