package inbox

import (
	"fmt"

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

	"github.com/spf13/cobra"
)

type replyOptions struct {
	from        string
	to          string
	threadID    string
	kind        string
	summary     string
	body        string
	bodyFile    string
	payloadJSON string
	artifacts   artifactOptions
}

func newReplyCmd(root *rootOptions) *cobra.Command {
	opts := &replyOptions{}

	cmd := &cobra.Command{
		Use:   "reply",
		Short: "Reply inside an existing thread",
		RunE: func(cmd *cobra.Command, args []string) error {
			ctx := cmd.Context()

			from := opts.from
			if from == "" {
				from = root.agent
			}
			if from == "" {
				return protocol.InvalidInput("from agent is required", nil)
			}

			body, err := resolveBodyValue(opts.body, opts.bodyFile)
			if err != nil {
				return err
			}
			artifacts, err := resolveArtifacts(opts.artifacts)
			if err != nil {
				return err
			}

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

			s := store.NewInboxStore(sqlDB)
			thread, message, err := s.ReplyToThread(ctx, store.ReplyInput{
				ThreadID:    opts.threadID,
				FromAgent:   from,
				ToAgent:     opts.to,
				Kind:        opts.kind,
				Summary:     opts.summary,
				Body:        body,
				PayloadJSON: opts.payloadJSON,
				Artifacts:   artifacts,
			})
			if err != nil {
				return err
			}

			resp := protocol.Success{
				OK:      true,
				Command: "reply",
				Data: map[string]any{
					"thread":  thread,
					"message": message,
				},
			}

			if root.json {
				return protocol.WriteJSON(cmd.OutOrStdout(), resp)
			}

			_, err = fmt.Fprintf(cmd.OutOrStdout(), "replied on thread %s\n", thread.ThreadID)
			return err
		},
	}

	cmd.Flags().StringVar(&opts.from, "from", "", "Replying agent")
	cmd.Flags().StringVar(&opts.to, "to", "", "Receiving agent")
	cmd.Flags().StringVar(&opts.threadID, "thread", "", "Thread ID")
	cmd.Flags().StringVar(&opts.kind, "kind", "answer", "Reply kind")
	cmd.Flags().StringVar(&opts.summary, "summary", "", "Short reply summary")
	cmd.Flags().StringVar(&opts.body, "body", "", "Reply body")
	cmd.Flags().StringVar(&opts.bodyFile, "body-file", "", "Read reply body from file")
	cmd.Flags().StringVar(&opts.payloadJSON, "payload-json", "", "Structured payload JSON string")
	addArtifactFlags(cmd, &opts.artifacts)

	_ = cmd.MarkFlagRequired("thread")
	_ = cmd.MarkFlagRequired("to")
	_ = cmd.MarkFlagRequired("summary")

	return cmd
}
