package main

import (
	"context"
	"fmt"
	"time"

	"github.com/spf13/cobra"
)

func statusCmd() *cobra.Command {
	return &cobra.Command{
		Use:   "status",
		Short: "Show server status",
		Long:  "Show server status including version, uptime, and listener information.",
		RunE: func(cmd *cobra.Command, args []string) error {
			client := clientFromContext(cmd.Context())

			ctx, cancel := context.WithTimeout(cmd.Context(), 5*time.Second)
			defer cancel()

			status, err := client.GetStatus(ctx)
			if err != nil {
				return fmt.Errorf("getting status: %w", err)
			}

			fmt.Printf("mc-proxy %s\n", status.Version)
			if !status.StartedAt.IsZero() {
				uptime := time.Since(status.StartedAt).Truncate(time.Second)
				fmt.Printf("uptime:      %s\n", uptime)
			}
			fmt.Printf("connections: %d\n", status.TotalConnections)
			fmt.Println()

			for _, ls := range status.Listeners {
				fmt.Printf("  %s  routes=%d  active=%d\n", ls.Addr, ls.RouteCount, ls.ActiveConnections)
			}

			return nil
		},
	}
}
