package database

import (
	"fmt"

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

	"git.tovijaeschke.xyz/tovi/personal_website/variables"
)

var (
	DB *gorm.DB
)

func InitDatabaseConn() error {
	var (
		dbConnString string
		err          error
	)

	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, err = gorm.Open(
		postgres.Open(dbConnString),
		&gorm.Config{},
	)
	if err != nil {
		return err
	}

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

	return nil
}
