// 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 newStoresFindCmd(flags *rootFlags) *cobra.Command {
	var flagS string
	var flagC string
	var flagLocationId string

	cmd := &cobra.Command{
		Use:   "find",
		Short: "Find nearby stores by address",
		// TODO: replace placeholder example values before relying on this for live dogfood.
		Example:     "  public-param-golden-pp-cli stores find --address example-value --city example-value --location-id 550e8400-e29b-41d4-a716-446655440000",
		Annotations: map[string]string{"pp:endpoint": "stores.find", "pp:method": "GET", "pp:path": "/power/store-locator", "mcp:read-only": "true"},
		RunE: func(cmd *cobra.Command, args []string) error {
			// Bare invocation of a command with required input prints help
			// instead of pflag's terse "required flag not set" error. Optional-
			// only read commands fall through so a bare call still executes.
			if cmd.Flags().NFlag() == 0 && len(args) == 0 && !flags.dryRun {
				return cmd.Help()
			}
			if !(cmd.Flags().Changed("address") || cmd.Flags().Changed("s")) && !flags.dryRun {
				return fmt.Errorf("required flag \"%s\" not set", "address")
			}
			if !(cmd.Flags().Changed("city") || cmd.Flags().Changed("c")) && !flags.dryRun {
				return fmt.Errorf("required flag \"%s\" not set", "city")
			}
			if !cmd.Flags().Changed("location-id") && !flags.dryRun {
				return fmt.Errorf("required flag \"%s\" not set", "location-id")
			}
			path := "/power/store-locator"
			c, err := flags.newClient()
			if err != nil {
				return err
			}
			params := map[string]string{}
			if flagS != "" {
				params["s"] = formatCLIParamValue(flagS)
			}
			if flagC != "" {
				params["c"] = formatCLIParamValue(flagC)
			}
			if flagLocationId != "" {
				params["location_id"] = formatCLIParamValue(flagLocationId)
			}
			data, prov, err := resolveReadWithStrategyAndResponsePath(cmd.Context(), c, flags, "auto", "stores", true, path, params, nil, "", cmd.ErrOrStderr())
			if err != nil {
				return classifyAPIError(err, flags)
			}
			// Print provenance to stderr for human-facing output only.
			// Machine-format flags (--json, --csv, --compact, --quiet, --plain,
			// --select) and piped stdout suppress this line; the JSON envelope
			// already carries meta.source for those consumers.
			// SYNC: keep this gate aligned with command_promoted.go.tmpl.
			if wantsHumanTable(cmd.OutOrStdout(), flags) {
				var countItems []json.RawMessage
				_ = json.Unmarshal(data, &countItems)
				printProvenance(cmd, len(countItems), prov)
			}
			// For JSON output, wrap with provenance envelope before passing through flags.
			// --select wins over --compact when both are set; --compact only runs when
			// no explicit fields were requested. Explicit format flags (--csv, --quiet,
			// --plain) opt out of the auto-JSON path so piped consumers that asked for
			// a non-JSON format reach the standard pipeline below.
			if flags.asJSON || (!isTerminal(cmd.OutOrStdout()) && !flags.csv && !flags.quiet && !flags.plain) {
				filtered := data
				if flags.selectFields != "" {
					filtered = filterFields(filtered, flags.selectFields)
				} else if flags.compact {
					filtered = compactFields(filtered)
				}
				wrapped, wrapErr := wrapWithProvenance(filtered, prov)
				if wrapErr != nil {
					return wrapErr
				}
				return printOutput(cmd.OutOrStdout(), wrapped, true)
			}
			// For all other output modes (table, csv, plain, quiet), use the standard pipeline
			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"})
		},
	}
	cmd.Flags().StringVar(&flagS, "address", "", "Street address")
	cmd.Flags().StringVar(&flagS, "s", "", "Street address")
	_ = cmd.Flags().MarkHidden("s")
	cmd.Flags().StringVar(&flagC, "city", "", "City, state, zip")
	cmd.Flags().StringVar(&flagC, "c", "", "City, state, zip")
	_ = cmd.Flags().MarkHidden("c")
	cmd.Flags().StringVar(&flagLocationId, "location-id", "", "Location identifier sent with this endpoint's snake-case query key")

	return cmd
}
