package main

import (
	"log"
	"sheetless-server/database"
	"sheetless-server/routes"
	"sheetless-server/sync"
	"time"

	"github.com/gin-gonic/gin"
)

func main() {
	database.InitDatabase()

	// Start sync runner
	go func() {
		ticker := time.NewTicker(1 * time.Minute)
		defer ticker.Stop()
		for {
			select {
			case <-ticker.C:
				if err := sync.SyncSheets(); err != nil {
					log.Printf("Sync error: %v", err)
				}
			}
		}
	}()

	r := gin.Default()

	routes.SetupRoutes(r)

	log.Println("Server starting on port 8080...")
	if err := r.Run(":8080"); err != nil {
		log.Fatal("Failed to start server:", err)
	}
}
