package action

import (
	"context"
	"fmt"

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

type GuardianCommand struct {
	devopsClient devops.Interface
}

func newGuardianCommand(devopsClient devops.Interface) (*GuardianCommand, error) {
	return &GuardianCommand{devopsClient: devopsClient}, nil
}

func (cmd *GuardianCommand) Exec(ctx context.Context,
	actionExecInfo *entity.NotificationActionExecInfo,
	args ...string,
) (bool, string, error) {
	issueKey := actionExecInfo.Notification.Issue.Key
	analysisUUID, err := cmd.devopsClient.CreateAnalysis(issueKey)
	if err != nil {
		return false, fmt.Sprintf("failed to create analysis: %s", err.Error()), fmt.Errorf("fail to create analysis for issue %s, %w", issueKey, err)
	}
	return true, fmt.Sprintf("guardian analysis uuid: %s. \n please wait for the analysis to complete.", analysisUUID), nil
}
