package main

import (
	"context"
	"fmt"
	"net/http"
	"os"
	"os/signal"
	"syscall"
	"time"

	"github.com/spf13/cobra"
	"go.uber.org/zap"

	"github.com/Warky-Devs/vecna.git/pkg/config"
	"github.com/Warky-Devs/vecna.git/pkg/embedclient"
	"github.com/Warky-Devs/vecna.git/pkg/metrics"
	"github.com/Warky-Devs/vecna.git/pkg/server"
)

var serveCmd = &cobra.Command{
	Use:   "serve",
	Short: "Start the vecna embedding proxy server",
	RunE:  runServe,
}

func init() {
	serveCmd.Flags().String("host", "", "bind host (overrides config)")
	serveCmd.Flags().Int("port", 0, "bind port (overrides config)")
}

func runServe(cmd *cobra.Command, _ []string) error {
	cfg, err := config.Load(cfgFile)
	if err != nil {
		return fmt.Errorf("load config: %w", err)
	}

	// Flag overrides
	if h, _ := cmd.Flags().GetString("host"); h != "" {
		cfg.Server.Host = h
	}
	if p, _ := cmd.Flags().GetInt("port"); p != 0 {
		cfg.Server.Port = p
	}

	adp, err := buildAdapter(cfg)
	if err != nil {
		return fmt.Errorf("build adapter: %w", err)
	}

	clients, err := buildClients(cfg)
	if err != nil {
		return fmt.Errorf("build clients: %w", err)
	}

	var reg *metrics.Registry
	if cfg.Metrics.Enabled {
		reg = metrics.New()
	}

	router := server.New(cfg, clients, adp, reg, logger)

	addr := fmt.Sprintf("%s:%d", cfg.Server.Host, cfg.Server.Port)
	srv := &http.Server{
		Addr:    addr,
		Handler: router,
	}

	// Graceful shutdown
	quit := make(chan os.Signal, 1)
	signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)

	go func() {
		logger.Info("vecna listening", zap.String("addr", addr))
		if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
			logger.Error("server error", zap.Error(err))
			quit <- syscall.SIGTERM
		}
	}()

	<-quit
	logger.Info("shutting down")

	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()
	if err := srv.Shutdown(ctx); err != nil {
		return fmt.Errorf("graceful shutdown: %w", err)
	}
	return nil
}

// buildClients constructs one embedclient.Client per named forward target.
func buildClients(cfg *config.Config) (map[string]embedclient.Client, error) {
	clients := make(map[string]embedclient.Client, len(cfg.Forward.Targets))

	for name, target := range cfg.Forward.Targets {
		if len(target.Endpoints) == 0 {
			return nil, fmt.Errorf("target %q has no endpoints", name)
		}

		timeout := time.Duration(target.TimeoutSecs) * time.Second
		httpClient := &http.Client{Timeout: timeout}

		slots := make([]embedclient.RouterSlot, len(target.Endpoints))
		for i, ep := range target.Endpoints {
			apiKey := ep.APIKey
			if apiKey == "" {
				apiKey = target.APIKey
			}

			var c embedclient.Client
			switch target.APIType {
			case "google":
				c = embedclient.NewGoogle(ep.URL, apiKey, target.Model, httpClient)
			default: // "openai" or unset
				c = embedclient.NewOpenAI(ep.URL, apiKey, httpClient)
			}

			slots[i] = embedclient.RouterSlot{
				Client:   c,
				URL:      ep.URL,
				Priority: ep.Priority,
			}
		}

		routerCfg := embedclient.RouterConfig{
			TargetName:       name,
			TimeoutSecs:      target.TimeoutSecs,
			CooldownSecs:     target.CooldownSecs,
			PriorityDecay:    target.PriorityDecay,
			PriorityRecovery: target.PriorityRecovery,
		}

		// metrics registry may be nil (disabled)
		var reg *metrics.Registry
		if cfg.Metrics.Enabled {
			// Registry is created in runServe before buildClients; pass nil here,
			// caller wires it in after creation. See note below.
			_ = reg
		}

		router, err := embedclient.NewTargetRouter(slots, routerCfg, nil)
		if err != nil {
			return nil, fmt.Errorf("build router for target %q: %w", name, err)
		}
		clients[name] = router
	}

	return clients, nil
}
