package main

import (
	"fmt"

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

var stateCmd = &cobra.Command{
	Use:   "state",
	Short: "Show current SDLC state",
	RunE: func(_ *cobra.Command, _ []string) error {
		root := mustResolveRoot()

		state, err := sdlc.LoadState(root)
		if err != nil {
			return err
		}

		if jsonOutput {
			return printJSON(state)
		}

		fmt.Printf("SDLC State (v%d)\n", state.Version)
		fmt.Printf("  Project: %s\n", state.Project.Name)
		if state.Project.CurrentRoadmap != "" {
			fmt.Printf("  Roadmap: %s\n", state.Project.CurrentRoadmap)
		}
		fmt.Println()

		if len(state.ActiveWork.Features) > 0 {
			fmt.Println("Active Features:")
			for _, f := range state.ActiveWork.Features {
				branch := ""
				if f.Branch != "" {
					branch = fmt.Sprintf(" (%s)", f.Branch)
				}
				fmt.Printf("  - %s [%s]%s\n", f.Slug, f.Phase, branch)
			}
			fmt.Println()
		}

		if len(state.Blocked) > 0 {
			fmt.Println("Blocked Items:")
			for _, b := range state.Blocked {
				fmt.Printf("  - %s/%s: %s\n", b.Type, b.Slug, b.Reason)
			}
			fmt.Println()
		}

		if state.LastAction != "" {
			fmt.Printf("Last Action: %s by %s\n", state.LastAction, state.LastActor)
		}
		if state.LastUpdated != nil {
			fmt.Printf("Last Updated: %s\n", state.LastUpdated.Format("2006-01-02 15:04:05"))
		}

		return nil
	},
}

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