package main

import (
	"context"
	"time"

	"git.kanopo.dev/rhythm/rhythm-backend/internal/config"
	"git.kanopo.dev/rhythm/rhythm-backend/internal/db"
	"git.kanopo.dev/rhythm/rhythm-backend/internal/logger"
	"github.com/jackc/pgx/v5/pgxpool"
)

func main() {
	cfg := config.Load()
	log := logger.New(cfg.AppEnv)
	defer log.Sync()

	log.Info("Starting rhythm")

	ctx := context.Background()
	pool, err := pgxpool.New(ctx, cfg.DbUrl)
	if err != nil {
		log.Fatalf("Error creatin the db pool:%v\n", err.Error())
	}
	defer pool.Close()

	{
		ctx, cancel := context.WithTimeout(ctx, time.Second*5)
		defer cancel()
		if err := pool.Ping(ctx); err != nil {
			log.Fatalf("ping to db failed %v", err.Error())
		}
		log.Info("successfully connected to database")

		db.RunMigrations(cfg.DbUrl, log)
	}

}
