// Copyright 2026 printing-press-golden and contributors. Licensed under Apache-2.0. See LICENSE.
// Generated by CLI Printing Press (https://github.com/mvanhorn/cli-printing-press). DO NOT EDIT.

package cli

import (
	"encoding/json"
	"fmt"
	"os"

	"github.com/spf13/cobra"
)

func newThingsPromotedCmd(flags *rootFlags) *cobra.Command {

	cmd := &cobra.Command{
		Use:         "things <thing_id>",
		Short:       "Get a thing via GraphQL",
		Long:        "Get a thing via GraphQL",
		Example:     "  graphql-shared-golden-pp-cli things 550e8400-e29b-41d4-a716-446655440000",
		Annotations: map[string]string{"pp:endpoint": "things.get", "pp:method": "POST", "pp:path": "/graphql", "mcp:read-only": "true"},
		RunE: func(cmd *cobra.Command, args []string) error {
			c, err := flags.newClient()
			if err != nil {
				return err
			}

			path := "/graphql"
			if len(args) < 1 {
				// JSON envelope: {error, usage}. Written first; the
				// usageErr return preserves exit code 2 across modes.
				if flags.asJSON {
					if printErr := printJSONFiltered(cmd.OutOrStdout(), map[string]any{
						"error": "thing_id is required",
						"usage": fmt.Sprintf("%s <%s>", cmd.CommandPath(), "thing_id"),
					}, flags); printErr != nil {
						return printErr
					}
				}
				return usageErr(fmt.Errorf("thing_id is required\nUsage: %s <%s>", cmd.CommandPath(), "thing_id"))
			}
			params := map[string]string{}
			params["thing_id"] = args[0]
			// HasStore + non-GET falls through to a live API call here
			// rather than through resolveRead (GET-only internally); a
			// body-aware cached read helper is filed as #425 for when a
			// second store-backed POST-search consumer ships.
			body := map[string]any{}
			data, _, err := c.PostQueryWithParams(cmd.Context(), path, params, body)

			if err != nil {
				return classifyAPIError(err, flags)
			}
			if wantsHumanTable(cmd.OutOrStdout(), flags) {
				var items []map[string]any
				if json.Unmarshal(data, &items) == nil && len(items) > 0 {
					if err := printAutoTable(cmd.OutOrStdout(), items); err != nil {
						return err
					}
					if len(items) >= 25 {
						fmt.Fprintf(os.Stderr, "\nShowing %d results. To narrow: add --limit, --json --select, or filter flags.\n", len(items))
					}
					return nil
				}
			}
			return printOutputWithFlagsMeta(cmd.OutOrStdout(), data, flags, map[string]any{"source": "live"})
		},
	}

	// Wire sibling endpoints and sub-resources as subcommands
	cmd.AddCommand(newThingsListCmd(flags))

	return cmd
}
