package main

import (
	"log"

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

func main() {
	if err := godotenv.Load("cmd/cli/.local.env"); err != nil {
		log.Printf("No .local.env file found: %v, trying .env", err)
		if err := godotenv.Load("cmd/cli/.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)
	}

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

	log.Println("Successfully connected to db")

	// Run migrations
	log.Println("Running database migrations...")
	if err := infrastructure.RunMigrations(db); err != nil {
		log.Fatalf("Error running migrations: %v", err)
	}
	log.Println("Migrations completed successfully")

	analyzer := internal.NewJobAnalyzer(db)

	if err := analyzer.AnalyzeJobs(); err != nil {
		log.Fatalf("Error analyzing jobs: %v", err)
	}

	log.Println("Job analysis completed successfully")
}
