// Copyright 2026 printing-press-golden. 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

	cmd := &cobra.Command{
		Use:         "find",
		Short:       "Find nearby stores by address",
		Example:     "  public-param-golden-pp-cli stores find --address example-value --city example-value",
		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 {
			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")
			}
			c, err := flags.newClient()
			if err != nil {
				return err
			}

			path := "/power/store-locator"
			params := map[string]string{}
			if flagS != "" {
				params["s"] = fmt.Sprintf("%v", flagS)
			}
			if flagC != "" {
				params["c"] = fmt.Sprintf("%v", flagC)
			}
			data, err := c.Get(path, params)
			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 printOutputWithFlags(cmd.OutOrStdout(), data, flags)
		},
	}
	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")

	return cmd
}
