package main

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

	"docproc/api"
	"docproc/processor"
	"docproc/watcher"
)

func main() {
	log.SetFlags(log.LstdFlags | log.Lshortfile)
	log.Println("Starting Document Processor...")

	// Initialize processor
	proc := processor.New()

	// Start file watcher
	w, err := watcher.New(proc)
	if err != nil {
		log.Fatalf("Failed to start watcher: %v", err)
	}
	go w.Watch()

	// Start HTTP API
	server := api.NewServer(proc)
	go func() {
		if err := server.Start(":9900"); err != nil {
			log.Fatalf("Server failed: %v", err)
		}
	}()

	log.Println("Document Processor running on :9900")
	log.Println("Watching ~/documents/inbox/")

	// Graceful shutdown
	sigChan := make(chan os.Signal, 1)
	signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
	<-sigChan
	log.Println("Shutting down...")
	w.Stop()
}
