package action

import (
	"context"
	"fmt"
	"log/slog"

	"github.com/tidbcloud/jiraport/pkg/clients/jira"
	"github.com/tidbcloud/jiraport/pkg/entity"
)

type CommentCommand struct {
	logger *slog.Logger
	jira   jira.Interface
}

func NewCommentCommand(logger *slog.Logger, jira jira.Interface) (*CommentCommand, error) {
	return &CommentCommand{logger: logger, jira: jira}, nil
}

func (cmd *CommentCommand) Exec(ctx context.Context, actionExecInfo *entity.NotificationActionExecInfo, args ...string) (bool, string, error) {
	if len(args) == 0 {
		return true, "", nil
	}

	comment := args[0]
	err := cmd.jira.AddComment(actionExecInfo.Notification.IssueKey, comment)
	if err != nil {
		return false, "", err
	}

	msg := fmt.Sprintf("add comment \"%s\"", comment)

	return true, msg, nil
}
