package schema

import (
	"database/sql"
	"log"

	"github.com/uptrace/bun"
	"github.com/uptrace/bun/dialect/pgdialect"
	"github.com/uptrace/bun/driver/pgdriver"
)

var DB *bun.DB

func GetDatabase() (*bun.DB, error) {
	var err error

	dsn := "postgres://kontor:kontor@postgres:5432/kontor?sslmode=disable"
	sqldb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(dsn)))

	sqldb.SetMaxOpenConns(4)
	sqldb.SetMaxIdleConns(4)

	DB := bun.NewDB(sqldb, pgdialect.New())

	if err = DB.Ping(); err != nil {
		return nil, err
	}

	log.Println("Returned Database Connection")
	return DB, nil
}
