package main

import (
	"context"
	"log"
	"os"
	"os/signal"
	"syscall"

	"github.com/jobs-scraper/internal/pkg/infrastructure"
	"github.com/jobs-scraper/libs/repo"
	"github.com/jobs-scraper/services/scraper-hiringcafe/models"
	"github.com/jobs-scraper/services/scraper-hiringcafe/scraper"
	"github.com/joho/godotenv"
)

func main() {
	if err := godotenv.Load(".local.env"); err != nil {
		log.Println("No .local.env file found, trying .env")
		if err := godotenv.Load(); err != nil {
			log.Println("No .env file found, using system environment variables")
		}
	}

	// Setup database connection
	dbConfig := infrastructure.LoadConfigFromEnv()
	db, err := infrastructure.NewConnection(dbConfig)
	if err != nil {
		log.Fatalf("Error connecting to db: %v", err)
	}
	defer db.Close()

	if err := db.Ping(); err != nil {
		log.Fatalf("Error pinging db: %v", err)
	}
	log.Println("Successfully connected to db")

	// Initialize repositories
	jobRepo := repo.NewJobRepository(db)
	jobDescRepo := repo.NewJobDescriptionRepository(db)

	// Example filters - these will be configurable later
	filters := models.SearchFilters{
		Keywords: "software engineer",
		Location: "",
		Remote:   true,
	}

	// Create scraper and run
	s := scraper.New(jobRepo, jobDescRepo)

	// Handle graceful shutdown
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	sigChan := make(chan os.Signal, 1)
	signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)

	go func() {
		<-sigChan
		log.Println("Received shutdown signal, cleaning up...")
		cancel()
	}()

	if err := s.Scrape(ctx, filters); err != nil {
		log.Printf("Error scraping: %v", err)
	}

	log.Println("Scraper finished")
}
