package main

import (
	"fmt"
	"os"

	"github.com/spf13/cobra"

	"git.tswf.io/infra/go-synapse-backupper/resources"
)

func generateConfigCmd() *cobra.Command {
	var lang string
	var output string

	cmd := &cobra.Command{
		Use:   "generate-config",
		Short: "Generate a commented example configuration file",
		RunE: func(cmd *cobra.Command, args []string) error {
			var tmpl []byte
			switch lang {
			case "en":
				tmpl = resources.ConfigEnYAML
			case "ru":
				tmpl = resources.ConfigRuYAML
			default:
				return fmt.Errorf("unsupported language: %q (must be \"en\" or \"ru\")", lang)
			}

			if output != "" {
				return os.WriteFile(output, tmpl, 0o644)
			}
			_, err := cmd.OutOrStdout().Write(tmpl)
			return err
		},
	}

	cmd.Flags().StringVar(&lang, "lang", "en", "Language for comments (en or ru)")
	cmd.Flags().StringVar(&output, "output", "", "Output file path (empty = stdout)")

	return cmd
}
