// 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 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 {
			if !cmd.Flags().Changed("year") && !flags.dryRun {
				return fmt.Errorf("required flag \"%s\" not set", "year")
			}
			c, err := flags.newClient()
			if err != nil {
				return err
			}

			path := "/reports/{year}/export"
			path = replacePathParam(path, "year", fmt.Sprintf("%v", flagYear))
			headerOverrides := map[string]string{
				"Accept":                           "application/octet-stream",
				"X-Printing-Press-Binary-Response": "true",
			}
			params := map[string]string{}
			data, prov, err := resolveRead(cmd.Context(), c, flags, "export", false, path, params, headerOverrides)
			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
}
