package main

import (
	"agent/src/agent"
	"agent/src/llm"
	"agent/src/utils/log"
	"context"
	"os"
	"os/signal"
	"syscall"
	"time"

	"github.com/spf13/cobra"
)


var (
	main_data = struct {
		port uint64
		crtDir        string
		ollamaUrls    []string
		saveRequestTo string

	}{}

)


func init() {
	f := cmd_run.Flags()
	f.Uint64Var(&main_data.port, "port", 8080, "http port")
	f.StringVar(&main_data.crtDir, "crt", "", "where is server.crt, server.key (default no https)")
	f.StringSliceVar(&main_data.ollamaUrls, "ollama_urls", []string{llm.DefaultOllamaUrl}, "ollama urls")

	// TODO
	// f.StringVar(&main_data.saveRequestTo, "save_request_to", "", "save request to dir (default not save)")
}


var cmd_root = &cobra.Command{
	Use: "agent",
}

var cmd_run = &cobra.Command{
	Use:   "run",
	Short: "run agent service",
	Example: "agent run",
	RunE: func(cmd *cobra.Command, args []string) error {
		var crtDir *string
		if len(main_data.crtDir) > 0 {
			crtDir = &main_data.crtDir
		}
		a, err := agent.NewAgent(cmd.Context(), main_data.ollamaUrls)
		if err != nil {
			return err
		}
		a.Start(main_data.port, crtDir)
		return nil
	},
}


func main() {
	ctx := context.Background()
	ctx, fnCancel := context.WithCancel(ctx)
	cmd_root.AddCommand(cmd_run)
	cmd_root.ExecuteContext(ctx)
	handleTerminationSignals(fnCancel)
}

func handleTerminationSignals(stopFunc func()) {
	signalCh := make(chan os.Signal, 1)
	signal.Notify(signalCh, syscall.SIGTERM, syscall.SIGINT)

	switch s := <-signalCh; s {
	case syscall.SIGTERM, syscall.SIGINT:
		log.Info("Stopping agent")
		// stop nicely
		stopFunc()
		time.Sleep(2*time.Second)
		// os.Exit(-int(syscall.SIGINT))
	}
}