package main

import (
	"context"
	"errors"
	"log"
	"os"
	"os/signal"
	"syscall"
	"weatherstation/internal/server"
	"weatherstation/internal/tools"
)

func main() {
	server.SetupLogger()

	// If CAIYUN_TOKEN is provided, enable wind override automatically.
	opts := tools.ExporterOptions{}
	if token := os.Getenv("CAIYUN_TOKEN"); token != "" {
		opts.OverrideWindWithCaiyun = true
		opts.CaiyunToken = token
		log.Printf("[service-exporter] wind override enabled via CAIYUN_TOKEN")
	}

	exp := tools.NewExporterWithOptions(opts)

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

	if err := exp.Start(ctx); err != nil && !errors.Is(err, context.Canceled) {
		log.Fatalf("service-exporter failed: %v", err)
	}
}
