package datasql

import (
	"log"
	"os"
	"time"

	"github.com/jackc/pgx/v5/pgxpool"
)

func Config() *pgxpool.Config {
	const defaultMaxConns = int32(4)
	const defaultMinConns = int32(0)
	const defaultMaxConnLifetime = time.Hour
	const defaultMaxConnIdleTime = time.Minute * 30
	const defaultHealthCheckPeriod = time.Minute
	const defaultConnectTimeout = time.Second * 5

	var databaseUrl string = os.Getenv("DATABASE_URL")

	dbConfig, err := pgxpool.ParseConfig(databaseUrl)
	if err != nil {
		log.Fatal("Failed to create a config, error: ", err)
	}

	dbConfig.MaxConns = defaultMaxConns
	dbConfig.MinConns = defaultMinConns
	dbConfig.MaxConnLifetime = defaultMaxConnLifetime
	dbConfig.MaxConnIdleTime = defaultMaxConnIdleTime
	dbConfig.HealthCheckPeriod = defaultHealthCheckPeriod
	dbConfig.ConnConfig.ConnectTimeout = defaultConnectTimeout

	/*
		dbConfig.BeforeAcquire = func(ctx context.Context, c *pgx.Conn) bool {
			log.Println("Before acquiring the connection pool to the database!!")
			return true
		}

		dbConfig.AfterRelease = func(c *pgx.Conn) bool {
			log.Println("After releasing the connection pool to the database!")
			return true
		}

		dbConfig.BeforeClose = func(c *pgx.Conn) {
			log.Println("Closed the connection pool to the database!")
		}
	*/

	return dbConfig
}
