// 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 newReportsExportReportYearCmd(flags *rootFlags) *cobra.Command {
	var flagYear int

	cmd := &cobra.Command{
		Use:         "report-year",
		Aliases:     []string{"get"},
		Short:       "Download the annual report as a binary file",
		Example:     "  printing-press-golden-pp-cli reports export report-year --year 42",
		Annotations: map[string]string{"pp:endpoint": "export.report-year", "pp:method": "GET", "pp:path": "/reports/{year}/export", "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("year") && !flags.dryRun {
				return fmt.Errorf("required flag \"%s\" not set", "year")
			}
			path := "/reports/{year}/export"
			c, err := flags.newClient()
			if err != nil {
				return err
			}
			path = replacePathParam(path, "year", formatCLIParamValue(flagYear))
			headerOverrides := map[string]string{
				"Accept":                           "application/octet-stream",
				"X-Printing-Press-Binary-Response": "true",
			}
			params := map[string]string{}
			data, prov, err := resolveReadWithStrategyAndResponsePath(cmd.Context(), c, flags, "auto", "export", false, path, params, headerOverrides, "", cmd.ErrOrStderr())
			if err != nil {
				return classifyAPIError(err, flags)
			}
			_ = json.Valid
			_ = os.Stderr
			_ = prov
			if flags.quiet {
				return nil
			}
			if flags.asJSON || flags.csv || flags.compact || flags.plain || flags.selectFields != "" {
				return fmt.Errorf("binary response cannot be rendered as structured output; redirect stdout or use --deliver file:<path>")
			}
			_, err = cmd.OutOrStdout().Write(data)
			return err
		},
	}
	cmd.Flags().IntVar(&flagYear, "year", 0, "Year")

	return cmd
}
