package setupinfrastructure

import (
	"database/sql"
	"log"

	"github.com/jobs-scraper/internal/pkg/infrastructure"
	"github.com/joho/godotenv"
)

func SetupInfrastructure() (*sql.DB, error) {
	// Load .env from the scraper-google service directory
	if err := godotenv.Load(".local.env"); err != nil {
		log.Printf("No .local.env file found: %v, trying .env", err)
		if err := godotenv.Load(".env"); err != nil {
			log.Println("No .env file found, using system environment variables")
		}
	}

	dbConfig := infrastructure.LoadConfigFromEnv()

	db, err := infrastructure.NewConnection(dbConfig)

	if err != nil {
		log.Fatalf("Error connecting to db: %v", err)
		return nil, err
	}

	err = db.Ping()

	if err != nil {
		log.Fatal("Error pinging db")
		return nil, err
	}

	log.Println("Successfully connected to db")
	return db, err
}
