1// SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
 2//
 3// SPDX-License-Identifier: AGPL-3.0-or-later
 4
 5package cmd
 6
 7import (
 8	"fmt"
 9	"strings"
10
11	"git.secluded.site/np/cmd/shared"
12	"git.secluded.site/np/internal/task"
13	"github.com/spf13/cobra"
14)
15
16var rCmd = &cobra.Command{
17	Use:   "r",
18	Short: "Resume session",
19	Long:  `Resume an interrupted session in a new context window`,
20	RunE:  runResume,
21}
22
23func runResume(cmd *cobra.Command, args []string) error {
24	env, err := shared.Environment(cmd)
25	if err != nil {
26		return err
27	}
28
29	sessionDoc, found, err := shared.ActiveSession(cmd, env)
30	if err != nil {
31		return err
32	}
33	if !found {
34		return nil
35	}
36
37	state, err := shared.PrintPlan(cmd, env, sessionDoc.SID)
38	if err != nil {
39		return err
40	}
41
42	// Print instructions for resuming work
43	fmt.Fprintln(cmd.OutOrStdout(), strings.Repeat("-", 80))
44	fmt.Fprintln(cmd.OutOrStdout(), "\nResuming session. To continue:")
45	fmt.Fprintln(cmd.OutOrStdout(), "1. Check the goal description above for any bug/issue/ticket ID. If present, read that ticket for full context.")
46	fmt.Fprintln(cmd.OutOrStdout(), "2. Read the files and symbols referenced in the pending tasks to understand what work remains.")
47	fmt.Fprintln(cmd.OutOrStdout(), "3. Update task status as you work: `np t u -i <task-id> -s <status>`")
48	fmt.Fprintln(cmd.OutOrStdout(), "   Statuses: pending, in_progress, completed, failed, cancelled")
49
50	// Provide context about pending work
51	pendingCount := 0
52	inProgressCount := 0
53	for _, t := range state.Tasks {
54		switch t.Status {
55		case task.StatusPending:
56			pendingCount++
57		case task.StatusInProgress:
58			inProgressCount++
59		}
60	}
61
62	if inProgressCount > 0 {
63		fmt.Fprintf(cmd.OutOrStdout(), "\n%d task(s) are in progress.\n", inProgressCount)
64	}
65	if pendingCount > 0 {
66		fmt.Fprintf(cmd.OutOrStdout(), "%d task(s) are pending.\n", pendingCount)
67	}
68
69	return nil
70}