package cmd

import (
	"bufio"
	"fmt"
	"os"
	"strings"

	"github.com/fatih/color"
	"github.com/spf13/cobra"
	"gmgauthier.com/grokkit/internal/grok"
)

var chatCmd = &cobra.Command{
	Use:   "chat",
	Short: "Interactive streaming chat with Grok",
	Run: func(cmd *cobra.Command, args []string) {
		client := grok.NewClient()
		color.Cyan("Grokkit Chat — type /quit or Ctrl+C to exit\n")

		history := []map[string]string{}
		scanner := bufio.NewScanner(os.Stdin)

		for {
			fmt.Print(color.YellowString("You: "))
			if !scanner.Scan() {
				break
			}
			input := strings.TrimSpace(scanner.Text())
			if input == "" {
				continue
			}
			if input == "/quit" || input == "/q" {
				break
			}

			history = append(history, map[string]string{"role": "user", "content": input})
			color.Green("Grok: ")
			reply := client.Stream(history, "grok-4")
			history = append(history, map[string]string{"role": "assistant", "content": reply})
		}
	},
}
