package main

import (
	"fmt"

	"github.com/orchard9/rdev/internal/sdlc"
	"github.com/spf13/cobra"
)

var archiveCmd = &cobra.Command{
	Use:   "archive <slug>",
	Short: "Archive a released feature",
	Args:  cobra.ExactArgs(1),
	RunE: func(_ *cobra.Command, args []string) error {
		root := mustResolveRoot()
		slug := args[0]

		if err := sdlc.ArchiveFeature(root, slug); err != nil {
			return err
		}

		// Remove from active work in state
		state, err := sdlc.LoadState(root)
		if err != nil {
			return err
		}
		state.RemoveActiveFeature(slug)
		state.RecordAction("ARCHIVE_FEATURE", slug, "cli", "success")
		if err := state.Save(root); err != nil {
			return err
		}

		if jsonOutput {
			return printJSON(map[string]string{
				"feature": slug,
				"status":  "archived",
			})
		}

		fmt.Printf("Archived: %s\n", slug)
		return nil
	},
}

func init() {
	rootCmd.AddCommand(archiveCmd)
}
