package main

import (
	"context"
	"log/slog"
	"os"

	"codeberg.org/nextgo/dbots/internal/config"
	"codeberg.org/nextgo/dbots/internal/db"
	"codeberg.org/nextgo/dbots/internal/server"
	"codeberg.org/ungo/gonsole"
	"github.com/jackc/pgx/v5/pgxpool"
)

func init() {
	gonsoleHandler := gonsole.New(os.Stdout, slog.LevelDebug)
	slogHandler := slog.New(gonsoleHandler)
	slog.SetDefault(slogHandler)
}

func main() {
	config := config.LoadConfig()
	ctx := context.Background()

	conn, err := pgxpool.New(ctx, config.Database.PostgresURL)
	if err != nil {
		slog.Error("error connecting to postgres", "err", err)
		return
	}

	queries := db.New(conn)
	server := server.NewServer(queries, config)

	server.Start(config.Server.Address, config.Server.Port)
}
