package main

import (
	"context"
	"log"
	"os"
	"os/signal"
	"syscall"
	"time"
	"weatherstation/internal/config"
	"weatherstation/internal/forecast"
	"weatherstation/internal/server"
)

func hourlyLoop(ctx context.Context, fn func() error, name string) {
	for {
		select {
		case <-ctx.Done():
			return
		default:
		}
		now := time.Now()
		next := now.Truncate(time.Hour).Add(time.Hour)
		t := time.NewTimer(time.Until(next))
		select {
		case <-ctx.Done():
			t.Stop()
			return
		case <-t.C:
		}
		if err := fn(); err != nil {
			log.Printf("[%s] scheduled run failed: %v", name, err)
		} else {
			log.Printf("[%s] scheduled run completed", name)
		}
	}
}

func main() {
	server.SetupLogger()

	ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
	defer stop()

	// Open-Meteo hourly fetch
	go hourlyLoop(ctx, func() error { return forecast.RunOpenMeteoFetch(context.Background()) }, "open-meteo")

	// Caiyun hourly fetch (if token configured)
	token := os.Getenv("CAIYUN_TOKEN")
	if token == "" {
		token = config.GetConfig().Forecast.CaiyunToken
	}
	if token == "" {
		log.Printf("[caiyun] token not set; caiyun scheduler disabled")
	} else {
		t := token
		go hourlyLoop(ctx, func() error { return forecast.RunCaiyunFetch(context.Background(), t) }, "caiyun")
	}

	// CMA hourly fetch
	// go hourlyLoop(ctx, func() error { return forecast.RunCMAFetch(context.Background()) }, "cma")

	<-ctx.Done()
	log.Println("service-forecast shutting down")
}
