package main

import (
	"context"
	"flag"
	"fmt"
	"os"
	"os/signal"
	"syscall"

	"haixun-backend/internal/bootstrap"
	"haixun-backend/internal/config"
	"haixun-backend/internal/svc"

	"github.com/zeromicro/go-zero/core/conf"
)

var configFile = flag.String("f", "etc/gateway.worker.yaml", "config file")

func main() {
	flag.Parse()
	bootstrap.LoadInfraEnv()

	var c config.Config
	conf.MustLoad(*configFile, &c, conf.UseEnv())
	if !c.JobWorker.Enabled && !c.JobScheduler.Enabled && !c.JobReaper.Enabled && !c.ThreadsTokenRefresh.Enabled && !c.ThreadsPublishQueue.Enabled {
		fmt.Fprintln(os.Stderr, "[worker] at least one worker, scheduler, reaper, token sweeper, or publish queue sweeper must be enabled")
		os.Exit(1)
	}

	sc := svc.NewServiceContext(c)
	defer sc.Close(context.Background())

	fmt.Printf(
		"[worker] started type=%s (job=%v scheduler=%v reaper=%v token_sweeper=%v publish_sweeper=%v)\n",
		c.JobWorker.WorkerType,
		c.JobWorker.Enabled,
		c.JobScheduler.Enabled,
		c.JobReaper.Enabled,
		c.ThreadsTokenRefresh.Enabled,
		c.ThreadsPublishQueue.Enabled,
	)

	ch := make(chan os.Signal, 1)
	signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
	<-ch
	fmt.Println("[worker] shutting down")
}
