package orch

import (
	"fmt"

	"ai-workflow-skill/internal/protocol"
	"ai-workflow-skill/internal/store"

	"github.com/spf13/cobra"
)

type reassignOptions struct {
	runID   string
	taskID  string
	toAgent string
	reason  string
}

func newReassignCmd(root *rootOptions) *cobra.Command {
	opts := &reassignOptions{}

	cmd := &cobra.Command{
		Use:   "reassign",
		Short: "Reassign a blocked or failed task to another worker",
		RunE: func(cmd *cobra.Command, args []string) error {
			ctx := cmd.Context()

			sqlDB, err := openOrchDB(ctx, root.dbPath)
			if err != nil {
				return err
			}
			defer sqlDB.Close()

			s := store.NewOrchStore(sqlDB)
			task, attempt, err := s.GetTaskWithLatestAttempt(ctx, opts.runID, opts.taskID)
			if err != nil {
				return err
			}

			result, err := s.ReassignTask(ctx, store.ReassignInput{
				RunID:            opts.runID,
				TaskID:           opts.taskID,
				ToAgent:          opts.toAgent,
				Reason:           opts.reason,
				PrepareWorkspace: newAttemptReuseWorkspacePreparer(cmd, task, attempt),
			})
			if err != nil {
				return err
			}

			resp := protocol.Success{
				OK:      true,
				Command: "reassign",
				Data: map[string]any{
					"task":             result.Task,
					"attempt":          result.Attempt,
					"thread":           result.Thread,
					"message":          result.Message,
					"previous_attempt": result.PreviousAttempt,
				},
			}
			if root.json {
				return protocol.WriteJSON(cmd.OutOrStdout(), resp)
			}

			_, err = fmt.Fprintf(cmd.OutOrStdout(), "reassigned task %s to %s as attempt %d\n", result.Task.TaskID, result.Attempt.AssignedTo, result.Attempt.AttemptNo)
			return err
		},
	}

	cmd.Flags().StringVar(&opts.runID, "run", "", "Run ID")
	cmd.Flags().StringVar(&opts.taskID, "task", "", "Task ID")
	cmd.Flags().StringVar(&opts.toAgent, "to", "", "Destination worker agent")
	cmd.Flags().StringVar(&opts.reason, "reason", "", "Reason for reassignment")
	_ = cmd.MarkFlagRequired("run")
	_ = cmd.MarkFlagRequired("task")
	_ = cmd.MarkFlagRequired("to")

	return cmd
}
