package cmd

import (
	"encoding/json"
	"fmt"
	"io"
	"strings"

	"github.com/spf13/cobra"
)

var languagesFormat string

var languagesCmd = &cobra.Command{
	Use:   "languages",
	Short: "Show supported languages and aliases",
	Long: `Display all supported languages for 'devour get' and 'devour ask'
with aliases and starter examples.`,
	RunE: runLanguages,
}

func init() {
	languagesCmd.Flags().StringVar(&languagesFormat, "format", "text", "output format (text, json)")
}

type languageInfo struct {
	Canonical string   `json:"canonical"`
	Aliases   []string `json:"aliases"`
	Example   string   `json:"example"`
	Source    string   `json:"source"`
}

func runLanguages(cmd *cobra.Command, args []string) error {
	rows := []languageInfo{
		{Canonical: "go", Aliases: []string{"go", "golang"}, Example: "devour get go http", Source: "pkg.go.dev"},
		{Canonical: "rust", Aliases: []string{"rust"}, Example: "devour get rust tokio", Source: "docs.rs"},
		{Canonical: "python", Aliases: []string{"python", "py"}, Example: "devour get python asyncio", Source: "docs.python.org"},
		{Canonical: "java", Aliases: []string{"java"}, Example: "devour get java string", Source: "docs.oracle.com"},
		{Canonical: "spring", Aliases: []string{"spring"}, Example: "devour get spring mcp", Source: "docs.spring.io"},
		{Canonical: "typescript", Aliases: []string{"typescript", "ts"}, Example: "devour get ts interfaces", Source: "typescriptlang.org"},
		{Canonical: "react", Aliases: []string{"react"}, Example: "devour get react hooks", Source: "react.dev"},
		{Canonical: "vue", Aliases: []string{"vue"}, Example: "devour get vue reactivity", Source: "vuejs.org"},
		{Canonical: "nuxt", Aliases: []string{"nuxt"}, Example: "devour get nuxt routing", Source: "nuxt.com"},
		{Canonical: "docker", Aliases: []string{"docker"}, Example: "devour get docker compose", Source: "docs.docker.com"},
		{Canonical: "cloudflare", Aliases: []string{"cloudflare", "cf"}, Example: "devour get cloudflare workers", Source: "developers.cloudflare.com"},
		{Canonical: "astro", Aliases: []string{"astro"}, Example: "devour get astro components", Source: "docs.astro.build"},
		{Canonical: "csharp", Aliases: []string{"csharp", "cs"}, Example: "devour get csharp regex", Source: "learn.microsoft.com"},
		{Canonical: "kotlin", Aliases: []string{"kotlin", "kt"}, Example: "devour get kotlin strings", Source: "kotlinlang.org"},
		{Canonical: "php", Aliases: []string{"php"}, Example: "devour get php pcre", Source: "php.net"},
		{Canonical: "ruby", Aliases: []string{"ruby", "rb"}, Example: "devour get ruby Regexp", Source: "ruby-doc.org"},
		{Canonical: "elixir", Aliases: []string{"elixir", "ex"}, Example: "devour get elixir String", Source: "hexdocs.pm"},
		{Canonical: "nextjs", Aliases: []string{"next", "nextjs"}, Example: "devour get nextjs routing", Source: "nextjs.org"},
		{Canonical: "svelte", Aliases: []string{"svelte"}, Example: "devour get svelte kit", Source: "svelte.dev"},
		{Canonical: "angular", Aliases: []string{"angular", "ng"}, Example: "devour get angular http", Source: "angular.dev"},
		{Canonical: "remix", Aliases: []string{"remix"}, Example: "devour get remix routes", Source: "v2.remix.run"},
		{Canonical: "solid", Aliases: []string{"solid", "solidjs"}, Example: "devour get solid router", Source: "github.com/solidjs/solid-docs"},
		{Canonical: "express", Aliases: []string{"express", "expressjs"}, Example: "devour get express middleware", Source: "expressjs.com"},
	}

	switch strings.ToLower(strings.TrimSpace(languagesFormat)) {
	case "json":
		out := struct {
			Count     int            `json:"count"`
			Languages []languageInfo `json:"languages"`
		}{
			Count:     len(rows),
			Languages: rows,
		}
		enc := json.NewEncoder(cmd.OutOrStdout())
		enc.SetIndent("", "  ")
		return enc.Encode(out)
	case "text", "":
		printLanguagesText(cmd.OutOrStdout(), rows)
		return nil
	default:
		return fmt.Errorf("unsupported format: %s", languagesFormat)
	}
}

func printLanguagesText(out io.Writer, rows []languageInfo) {
	_, _ = fmt.Fprintln(out, "Devour Supported Languages")
	_, _ = fmt.Fprintln(out, "============================================")
	_, _ = fmt.Fprintln(out)
	for _, row := range rows {
		_, _ = fmt.Fprintf(out, "- %s (%s)\n", row.Canonical, strings.Join(row.Aliases, ", "))
		_, _ = fmt.Fprintf(out, "  source: %s\n", row.Source)
		_, _ = fmt.Fprintf(out, "  example: %s\n\n", row.Example)
	}
	_, _ = fmt.Fprintln(out, "Tip: use 'devour get <language> <keyword> --format markdown' for readable output.")
}
