package main

// This is a basic docs gen with cobra's md_docs.go and util.go brought in now to give complete control over
// document generation - Dj

import (
	"bytes"
	"fmt"
	"io"
	"log"
	"os"
	"path"
	"path/filepath"
	"sort"
	"strings"
	"time"

	"github.com/spf13/cobra"

	"github.com/superfly/flyctl/internal/cli"
)

var titlePrefix = "## "

func main() {
	cmd := cli.NewRootCommand()
	cmd.DisableAutoGenTag = true

	// Override root command to always be `fly`,
	// Otherwise it could be `main`, `flyctl` or whatever name is set to the executable
	cmd.Use = "fly"

	filePrepender := func(filename string) string {
		return ""
	}

	linkHandler := func(name string) string {
		base := strings.TrimSuffix(name, path.Ext(name))
		base = strings.Replace(base, "fly_", "", 1)
		if base == "fly" {
			base = "help"
		}
		base = strings.ReplaceAll(base, "_", "-") + "/"

		return "/docs/flyctl/" + strings.ToLower(base)
	}

	os.MkdirAll("out", 0o700)

	err := GenMarkdownTreeCustom(cmd, "./out", filePrepender, linkHandler)
	if err != nil {
		log.Fatal(err)
	}
}

func printOptions(buf *bytes.Buffer, cmd *cobra.Command, name string) error {
	flags := cmd.NonInheritedFlags()
	flags.SetOutput(buf)
	if flags.HasAvailableFlags() {
		buf.WriteString(titlePrefix + "Options\n\n```\n")
		flags.PrintDefaults()
		buf.WriteString("```\n\n")
	}

	parentFlags := cmd.InheritedFlags()
	parentFlags.SetOutput(buf)
	if parentFlags.HasAvailableFlags() {
		buf.WriteString(titlePrefix + "Global Options\n\n```\n")
		parentFlags.PrintDefaults()
		buf.WriteString("```\n\n")
	}

	return nil
}

// GenMarkdownCustom creates custom markdown output.
func GenMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) string) error {
	cmd.InitDefaultHelpCmd()
	cmd.InitDefaultHelpFlag()

	buf := new(bytes.Buffer)
	name := cmd.CommandPath()
	// name := cmd.Name()

	short := cmd.Short
	long := cmd.Long
	if len(long) == 0 {
		long = short
	}

	buf.WriteString(long + "\n\n")

	if len(cmd.UseLine()) > 0 {
		buf.WriteString(titlePrefix + "Usage\n")

		// If it's runnable, show the useline otherwise show a version with [command]
		if cmd.Runnable() {
			fmt.Fprintf(buf, "```\n%s\n```\n\n", cmd.UseLine())
		} else {
			fmt.Fprintf(buf, "```\n%s [command] [flags]\n```\n\n", cmd.CommandPath())
		}
	}

	if hasSubCommands(cmd) {
		buf.WriteString(titlePrefix + "Available Commands\n")
		children := cmd.Commands()
		sort.Sort(byName(children))

		for _, child := range children {
			if !child.IsAvailableCommand() || child.IsAdditionalHelpTopicCommand() {
				continue
			}
			cname := name + " " + child.Name()
			link := cname + ".md"
			link = strings.ReplaceAll(link, " ", "_")
			fmt.Fprintf(buf, "* [%s](%s)\t - %s\n", child.Name(), linkHandler(link), child.Short)
		}
		buf.WriteString("\n")
	}

	if len(cmd.Example) > 0 {
		buf.WriteString(titlePrefix + "Examples\n\n")
		fmt.Fprintf(buf, "```\n%s\n```\n\n", cmd.Example)
	}

	if err := printOptions(buf, cmd, name); err != nil {
		return err
	}
	if hasSeeAlso(cmd) {
		buf.WriteString(titlePrefix + "See Also\n\n")
		if cmd.HasParent() {
			parent := cmd.Parent()
			pname := parent.CommandPath()
			link := pname + ".md"
			link = strings.ReplaceAll(link, " ", "_")
			fmt.Fprintf(buf, "* [%s](%s)\t - %s\n", pname, linkHandler(link), parent.Short)
			cmd.VisitParents(func(c *cobra.Command) {
				if c.DisableAutoGenTag {
					cmd.DisableAutoGenTag = c.DisableAutoGenTag
				}
			})
		}

		// children := cmd.Commands()
		// sort.Sort(byName(children))

		// for _, child := range children {
		// 	if !child.IsAvailableCommand() || child.IsAdditionalHelpTopicCommand() {
		// 		continue
		// 	}
		// 	cname := name + " " + child.Name()
		// 	link := cname + ".md"
		// 	link = strings.Replace(link, " ", "_", -1)
		// 	buf.WriteString(fmt.Sprintf("* [%s](%s)\t - %s\n", cname, linkHandler(link), child.Short))
		// }
		buf.WriteString("\n")
	}
	if !cmd.DisableAutoGenTag {
		buf.WriteString("###### Auto generated by spf13/cobra on " + time.Now().Format("2-Jan-2006") + "\n")
	}
	_, err := buf.WriteTo(w)

	return err
}

// GenMarkdownTreeCustom is the the same as GenMarkdownTree, but
// with custom filePrepender and linkHandler.
func GenMarkdownTreeCustom(cmd *cobra.Command, dir string, filePrepender, linkHandler func(string) string) error {
	for _, c := range cmd.Commands() {
		if !c.IsAvailableCommand() || c.IsAdditionalHelpTopicCommand() {
			continue
		}
		if err := GenMarkdownTreeCustom(c, dir, filePrepender, linkHandler); err != nil {
			return err
		}
	}

	basename := strings.ReplaceAll(cmd.CommandPath(), " ", "_") + ".md"
	filename := filepath.Join(dir, basename)
	f, err := os.Create(filename)
	if err != nil {
		return err
	}
	defer f.Close()

	if _, err := io.WriteString(f, filePrepender(filename)); err != nil {
		return err
	}
	if err := GenMarkdownCustom(cmd, f, linkHandler); err != nil {
		return err
	}

	return nil
}

// Test to see if we have a reason to print See Also information in docs
// Basically this is a test for a parent commend or a subcommand which is
// both not deprecated and not the autogenerated help command.
func hasSeeAlso(cmd *cobra.Command) bool {
	if cmd.HasParent() {
		return true
	}
	for _, c := range cmd.Commands() {
		if !c.IsAvailableCommand() || c.IsAdditionalHelpTopicCommand() {
			continue
		}

		return true
	}

	return false
}

// Test to see if we have a reason to print Sub Commands information in docs
// Basically this is a test for a parent commend or a subcommand which is
// both not deprecated and not the autogenerated help command.
func hasSubCommands(cmd *cobra.Command) bool {
	for _, c := range cmd.Commands() {
		if !c.IsAvailableCommand() || c.IsAdditionalHelpTopicCommand() {
			continue
		}

		return true
	}

	return false
}

type byName []*cobra.Command

func (s byName) Len() int           { return len(s) }
func (s byName) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }
func (s byName) Less(i, j int) bool { return s[i].Name() < s[j].Name() }
