package db

import (
	"fmt"
	"machines/app/cfg"

	"gorm.io/driver/postgres"
	"gorm.io/gorm"
	gormLogger "gorm.io/gorm/logger"
)

func ConnectDb() *gorm.DB {
	var connectionString = fmt.Sprintf("postgres://%s:%s@%s:%d/%s?sslmode=disable",
		cfg.Config.DbUser,
		cfg.Config.DbPassword,
		cfg.Config.DbHost,
		cfg.Config.DbPort,
		cfg.Config.DbName,
	)

	var err error
	dbConn, err := gorm.Open(postgres.Open(connectionString), &gorm.Config{
		Logger: gormLogger.Default.LogMode(gormLogger.Info),
	})
	if err != nil {
		panic("Error connecting to database: " + err.Error())
	}

	return dbConn
}
