<commit_before>package bot

import (
	"fmt"
	"strconv"
	"strings"

	"github.com/nlopes/slack"
	"github.com/scrumpolice/scrumpolice/scrum"
)

// HandleMessage handle a received message for scrums and returns if the bot shall continue to process the message or stop
// continue = true
// stop = false
func (b *Bot) HandleScrumMessage(event *slack.MessageEvent) bool {
	// "start scrum [team] [date]"
	// [team == first and only team]
	// [date == yesterday]
	// starting scrum for team [team] date [date]. if you want to abort say stop scrum

	// this module only takes case in private messages
	if event.Channel[0] != 'D' {
		return true
	}

	if context, ok := b.userContexts[event.User]; ok {
		return context.HandleMessage(event)
	}

	if strings.HasPrefix(strings.ToLower(event.Text), "start scrum") {
		return b.startScrum(event)
	}

	if strings.ToLower(event.Text) == "restart scrum" {
		return b.restartScrum(event)
	}

	return true
}

func (b *Bot) restartScrum(event *slack.MessageEvent) bool {
	user, err := b.slackBotAPI.GetUserInfo(event.User)
	if err != nil {
		return false
	}

	if !b.scrum.DeleteLastReport(user.Name) {
		b.slackBotAPI.PostMessage(event.Channel, "Nothing to restart, let's get out", slack.PostMessageParameters{AsUser: true})
		return false
	}

	b.slackBotAPI.PostMessage(event.Channel, "Your last report was deleted, you can `start scrum` a new one again", slack.PostMessageParameters{AsUser: true})
	return false
}

func (b *Bot) startScrum(event *slack.MessageEvent) bool {
	// can we infer team (aka does the user only has one team)
	// b.scrum.GetTeamForUser(event.User)
	user, err := b.slackBotAPI.GetUserInfo(event.User)
	if err != nil {
		b.slackBotAPI.PostMessage(event.Channel, "There was an error starting your scrum, please try again", slack.PostMessageParameters{AsUser: true})
		return false
	}
	username := user.Name

	teams := b.scrum.GetTeamsForUser(username)
	if len(teams) == 0 {
		b.slackBotAPI.PostMessage(event.Channel, "You're not part of a team, no point in doing a scrum report", slack.PostMessageParameters{AsUser: true})
	}

	if len(teams) == 1 {
		return b.choosenTeam(event, username, teams[0])
	}

	return b.chooseTeam(event, username, teams)
}

func (b *Bot) chooseTeam(event *slack.MessageEvent, username string, teams []string) bool {
	choices := make([]string, len(teams))<commit_msg>Sort the teams name so they are not in random order on each scrum, it makes it easier to complete the scrum without selecting the same team twice with a different index<commit_after>package bot

import (
	"fmt"
	"sort"
	"strconv"
	"strings"

	"github.com/nlopes/slack"
	"github.com/scrumpolice/scrumpolice/scrum"
)

// HandleMessage handle a received message for scrums and returns if the bot shall continue to process the message or stop
// continue = true
// stop = false
func (b *Bot) HandleScrumMessage(event *slack.MessageEvent) bool {
	// "start scrum [team] [date]"
	// [team == first and only team]
	// [date == yesterday]
	// starting scrum for team [team] date [date]. if you want to abort say stop scrum

	// this module only takes case in private messages
	if event.Channel[0] != 'D' {
		return true
	}

	if context, ok := b.userContexts[event.User]; ok {
		return context.HandleMessage(event)
	}

	if strings.HasPrefix(strings.ToLower(event.Text), "start scrum") {
		return b.startScrum(event)
	}

	if strings.ToLower(event.Text) == "restart scrum" {
		return b.restartScrum(event)
	}

	return true
}

func (b *Bot) restartScrum(event *slack.MessageEvent) bool {
	user, err := b.slackBotAPI.GetUserInfo(event.User)
	if err != nil {
		return false
	}

	if !b.scrum.DeleteLastReport(user.Name) {
		b.slackBotAPI.PostMessage(event.Channel, "Nothing to restart, let's get out", slack.PostMessageParameters{AsUser: true})
		return false
	}

	b.slackBotAPI.PostMessage(event.Channel, "Your last report was deleted, you can `start scrum` a new one again", slack.PostMessageParameters{AsUser: true})
	return false
}

func (b *Bot) startScrum(event *slack.MessageEvent) bool {
	// can we infer team (aka does the user only has one team)
	// b.scrum.GetTeamForUser(event.User)
	user, err := b.slackBotAPI.GetUserInfo(event.User)
	if err != nil {
		b.slackBotAPI.PostMessage(event.Channel, "There was an error starting your scrum, please try again", slack.PostMessageParameters{AsUser: true})
		return false
	}
	username := user.Name

	teams := b.scrum.GetTeamsForUser(username)
	if len(teams) == 0 {
		b.slackBotAPI.PostMessage(event.Channel, "You're not part of a team, no point in doing a scrum report", slack.PostMessageParameters{AsUser: true})
	}

	if len(teams) == 1 {
		return b.choosenTeam(event, username, teams[0])
	}

	return b.chooseTeam(event, username, teams)
}

func (b *Bot) chooseTeam(event *slack.MessageEvent, username string, teams []string) bool {
	choices := make([]string, len(teams))
	sort.Strings(teams)
{'fraction_non_alphanumeric': 0.12136121361213612, 'fraction_numerical': 0.0016400164001640015, 'mean_word_length': 4.379272326350606, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '75467', 'n_tokens_mistral': 1707, 'n_tokens_neox': 1640, 'n_words': 492}