<commit_before>package app.restlike.rim

import app.ServiceFactory._
import app.restlike.common.Colours._
import app.restlike.common._

object Commander {
  def process(value: String, who: String, currentModel: Model, refProvider: RefProvider, token: String): Out = {
    val bits = value.split(" ").map(_.trim).filterNot(_.isEmpty)
    val cmd = In(bits.headOption, if (bits.isEmpty) Nil else bits.tail.toList)

    if (!cmd.head.getOrElse("").equals("aka") && !currentModel.knows_?(who)) return Out(Messages.notAuthorised(who), None, Nil)

    def aka = currentModel.aka(who)

    //TODO: be nice of the help could be driven off this ...
    cmd match {
      case In(None, Nil) => onShowBoard(currentModel, aka)
      case In(Some("aka"), List(myAka)) => onAka(who, myAka, currentModel)
      case In(Some("tags"), Nil) => onShowTagPriority(who, currentModel)
      case In(Some("tags"), args) if args.nonEmpty && args.head == "=" => onSetTagPriority(who, args.drop(1), currentModel)
      case In(Some("help"), Nil) => onHelp(currentModel, aka)
      case In(Some("+"), args) => onAddIssue(args, currentModel, refProvider)
      case In(Some("+/"), args) => onAddAndBeginIssue(args, currentModel, refProvider, aka)
      case In(Some("+//"), args) => onAddAndForwardIssue(args, currentModel, refProvider, aka)
      case In(Some("+!"), args) => onAddAndEndIssue(args, currentModel, refProvider, aka)
      case In(Some("?"), Nil) => onQueryIssues(currentModel, Nil, aka)
      case In(Some("?"), terms) => onQueryIssues(currentModel, terms, aka)
      case In(Some("."), Nil) => onShowBacklog(currentModel, aka)
      case In(Some("^"), providedTags) => onShowBoardManagementSummary(currentModel, providedTags, aka, sanitise = false)
      case In(Some("^_"), providedTags) => onShowBoardManagementSummary(currentModel, providedTags, aka, sanitise = true)
      case In(Some(release), args) if args.nonEmpty && args.head == "^" => onShowReleaseManagementSummary(release, currentModel, args.drop(1), aka, sanitise = false)
      case In(Some(release), args) if args.nonEmpty && args.head == "^_" => onShowReleaseManagementSummary(release, currentModel, args.drop(1), aka, sanitise = true)
      case In(Some(ref), List("-")) => onRemoveIssue(ref, currentModel)
      case In(Some(ref), args) if args.nonEmpty && args.head == "=" => onEditIssue(ref, args.drop(1), currentModel, aka)
      case In(Some(ref), List("/")) => onForwardIssue(ref, currentModel, aka)
      case In(Some(ref), List("/!")) => onFastForwardIssue(ref, currentModel, aka)
      case In(Some(ref), List(".")) => onBackwardIssue(ref, currentModel, aka)
      case In(Some(ref), List(".!")) => onFastBackwardIssue(ref, currentModel, aka)
      case In(Some(ref), List("%")) => onUnblockIssue(ref, currentModel, aka)
      case In(Some(ref), args) if args.size > 1 && args.head == "%" => onBlockIssue(ref, args.drop(1), currentModel, aka)
      case In(Some(ref), List("@")) => onOwnIssue(who, ref, currentModel, aka)
      case In(Some(ref), List("@-")) => onDisownIssue(who, ref, currentModel, aka)
      case In(Some(ref), args) if args.size == 2 && args.head == "@=" => onAssignIssue(args.drop(1).head.toUpperCase, ref, currentModel, aka)
      case In(Some("@"), Nil) => onShowWhoIsDoingWhat(currentModel)
      case In(Some(ref), args) if args.nonEmpty && args.size > 1 && args.head == ":" => onTagIssue(ref, args.drop(1), currentModel, aka)
      case In(Some(ref), args) if args.nonEmpty && args.size > 1 && args.head == ":-" => onDetagIssue(ref, args.drop(1), currentModel, aka)
      case In(Some(":"), Nil) => onShowTags(currentModel)
      case In(Some(oldTag), args) if args.nonEmpty && args.size == 2 && args.head == ":=" => onMigrateTag(oldTag, args.drop(1).head, currentModel)
      case In(Some(tagToDelete), args) if args.nonEmpty && args.size == 1 && args.head == ":--" => onDeleteTagUsages(tagToDelete, currentModel)
      case In(Some(":"), args) if args.nonEmpty && args.size == 1 => onShowAllForTag(args.head, currentModel)
      case In(Some(":-"), Nil) => onShowUntagged(currentModel, aka)
      case In(Some("±"), List(tag)) => onRelease(tag, currentModel, aka)
      case In(Some("±"), Nil) => onShowReleases(currentModel, aka)
//      case In(Some("note"), args) if args.nonEmpty && args.size == 1 => onShowReleaseNote(args.head, currentModel)
      case In(Some(ref), Nil) => onShowHistoryIssue(ref, currentModel, token)
      case In(head, tail) => onUnknownCommand(head, tail)
    }
  }

  private def onUnknownCommand(head: Option[String], tail: List[String]) =
    Out(customRed(Messages.eh) + " " + head.getOrElse("") + " " + tail.mkString(" ") :: Nil, None, Nil)

  private def onShowBoard(currentModel: Model, aka: String) = Out(Presentation.board(currentModel, Nil, aka), None, Nil)

  private def onHelp(currentModel: Model, aka: String) = Out(Messages.help(aka), None, Nil)

  private def onShowReleases(currentModel: Model, aka: String) = {
    val all = currentModel.released.reverse.flatMap(Presentation.release(currentModel, _, Some(aka)))
    val result = if (all.isEmpty) Messages.success(s"no releases found")
    else all
    Out(result, None, Nil)
  }

  private def onShowWhoIsDoingWhat(currentModel: Model) = {
    val akas = currentModel.akas
    val all = akas.map(aka => {
      val issues = currentModel.issues.filter(_.by == Some(aka))
      Presentation.issuesForUser(currentModel, aka, SortByStatus(issues, currentModel))
    })

    val result = if (all.isEmpty) Messages.success(s"nobody is doing anything")
    else all
    Out(result, None, Nil)
  }

  private def onShowHistoryIssue(ref: String, currentModel: Model, token: String) = {
    val all = Rim.history(token)
      .filter(_.ref == Some(ref))
      .filter(_.action.isDefined)
      .filter(_.who.isDefined)
      .filter(_.when.isDefined)

    println(s"$ref:${all.size}")

    val issue = currentModel.findIssue(ref)

//    val akas = currentModel.akas
//    val all = akas.map(aka => {
//      val issues = currentModel.issues.filter(_.by == Some(aka))
//      Presentation.issuesForUser(currentModel, aka, SortByStatus(issues, currentModel))
//    })
    //TODO: show error if ref does not exist

    val result = if (issue.isEmpty) Messages.notFound(ref)
    else if (all.isEmpty) Messages.problem(s"no history for: $ref")
    else List(issue.get.render(currentModel)) ::: all.map(h => s" ${dateFormats().today(h.when.get)} > ${currentModel.aka(h.who.get)} ${h.action.get} -> $h").toList<commit_msg>tart up display a bit more<commit_after>package app.restlike.rim

import app.ServiceFactory._
import app.restlike.common.Colours._
import app.restlike.common._

object Commander {
  def process(value: String, who: String, currentModel: Model, refProvider: RefProvider, token: String): Out = {
    val bits = value.split(" ").map(_.trim).filterNot(_.isEmpty)
    val cmd = In(bits.headOption, if (bits.isEmpty) Nil else bits.tail.toList)

    if (!cmd.head.getOrElse("").equals("aka") && !currentModel.knows_?(who)) return Out(Messages.notAuthorised(who), None, Nil)

    def aka = currentModel.aka(who)

    //TODO: be nice of the help could be driven off this ...
    cmd match {
      case In(None, Nil) => onShowBoard(currentModel, aka)
      case In(Some("aka"), List(myAka)) => onAka(who, myAka, currentModel)
      case In(Some("tags"), Nil) => onShowTagPriority(who, currentModel)
      case In(Some("tags"), args) if args.nonEmpty && args.head == "=" => onSetTagPriority(who, args.drop(1), currentModel)
      case In(Some("help"), Nil) => onHelp(currentModel, aka)
      case In(Some("+"), args) => onAddIssue(args, currentModel, refProvider)
      case In(Some("+/"), args) => onAddAndBeginIssue(args, currentModel, refProvider, aka)
      case In(Some("+//"), args) => onAddAndForwardIssue(args, currentModel, refProvider, aka)
      case In(Some("+!"), args) => onAddAndEndIssue(args, currentModel, refProvider, aka)
      case In(Some("?"), Nil) => onQueryIssues(currentModel, Nil, aka)
      case In(Some("?"), terms) => onQueryIssues(currentModel, terms, aka)
      case In(Some("."), Nil) => onShowBacklog(currentModel, aka)
      case In(Some("^"), providedTags) => onShowBoardManagementSummary(currentModel, providedTags, aka, sanitise = false)
      case In(Some("^_"), providedTags) => onShowBoardManagementSummary(currentModel, providedTags, aka, sanitise = true)
      case In(Some(release), args) if args.nonEmpty && args.head == "^" => onShowReleaseManagementSummary(release, currentModel, args.drop(1), aka, sanitise = false)
      case In(Some(release), args) if args.nonEmpty && args.head == "^_" => onShowReleaseManagementSummary(release, currentModel, args.drop(1), aka, sanitise = true)
      case In(Some(ref), List("-")) => onRemoveIssue(ref, currentModel)
      case In(Some(ref), args) if args.nonEmpty && args.head == "=" => onEditIssue(ref, args.drop(1), currentModel, aka)
      case In(Some(ref), List("/")) => onForwardIssue(ref, currentModel, aka)
      case In(Some(ref), List("/!")) => onFastForwardIssue(ref, currentModel, aka)
      case In(Some(ref), List(".")) => onBackwardIssue(ref, currentModel, aka)
      case In(Some(ref), List(".!")) => onFastBackwardIssue(ref, currentModel, aka)
      case In(Some(ref), List("%")) => onUnblockIssue(ref, currentModel, aka)
      case In(Some(ref), args) if args.size > 1 && args.head == "%" => onBlockIssue(ref, args.drop(1), currentModel, aka)
      case In(Some(ref), List("@")) => onOwnIssue(who, ref, currentModel, aka)
      case In(Some(ref), List("@-")) => onDisownIssue(who, ref, currentModel, aka)
      case In(Some(ref), args) if args.size == 2 && args.head == "@=" => onAssignIssue(args.drop(1).head.toUpperCase, ref, currentModel, aka)
      case In(Some("@"), Nil) => onShowWhoIsDoingWhat(currentModel)
      case In(Some(ref), args) if args.nonEmpty && args.size > 1 && args.head == ":" => onTagIssue(ref, args.drop(1), currentModel, aka)
      case In(Some(ref), args) if args.nonEmpty && args.size > 1 && args.head == ":-" => onDetagIssue(ref, args.drop(1), currentModel, aka)
      case In(Some(":"), Nil) => onShowTags(currentModel)
      case In(Some(oldTag), args) if args.nonEmpty && args.size == 2 && args.head == ":=" => onMigrateTag(oldTag, args.drop(1).head, currentModel)
      case In(Some(tagToDelete), args) if args.nonEmpty && args.size == 1 && args.head == ":--" => onDeleteTagUsages(tagToDelete, currentModel)
      case In(Some(":"), args) if args.nonEmpty && args.size == 1 => onShowAllForTag(args.head, currentModel)
      case In(Some(":-"), Nil) => onShowUntagged(currentModel, aka)
      case In(Some("±"), List(tag)) => onRelease(tag, currentModel, aka)
      case In(Some("±"), Nil) => onShowReleases(currentModel, aka)
//      case In(Some("note"), args) if args.nonEmpty && args.size == 1 => onShowReleaseNote(args.head, currentModel)
      case In(Some(ref), Nil) => onShowHistoryIssue(ref, currentModel, token)
      case In(head, tail) => onUnknownCommand(head, tail)
    }
  }

  private def onUnknownCommand(head: Option[String], tail: List[String]) =
    Out(customRed(Messages.eh) + " " + head.getOrElse("") + " " + tail.mkString(" ") :: Nil, None, Nil)

  private def onShowBoard(currentModel: Model, aka: String) = Out(Presentation.board(currentModel, Nil, aka), None, Nil)

  private def onHelp(currentModel: Model, aka: String) = Out(Messages.help(aka), None, Nil)

  private def onShowReleases(currentModel: Model, aka: String) = {
    val all = currentModel.released.reverse.flatMap(Presentation.release(currentModel, _, Some(aka)))
    val result = if (all.isEmpty) Messages.success(s"no releases found")
    else all
    Out(result, None, Nil)
  }

  private def onShowWhoIsDoingWhat(currentModel: Model) = {
    val akas = currentModel.akas
    val all = akas.map(aka => {
      val issues = currentModel.issues.filter(_.by == Some(aka))
      Presentation.issuesForUser(currentModel, aka, SortByStatus(issues, currentModel))
    })

    val result = if (all.isEmpty) Messages.success(s"nobody is doing anything")
    else all
    Out(result, None, Nil)
  }

  private def onShowHistoryIssue(ref: String, currentModel: Model, token: String) = {
    val all = Rim.history(token)
      .filter(_.ref == Some(ref))
      .filter(_.action.isDefined)
      .filter(_.who.isDefined)
      .filter(_.when.isDefined)

    println(s"$ref:${all.size}")

    val issue = currentModel.findIssue(ref)

//    val akas = currentModel.akas
//    val all = akas.map(aka => {
//      val issues = currentModel.issues.filter(_.by == Some(aka))
//      Presentation.issuesForUser(currentModel, aka, SortByStatus(issues, currentModel))
//    })
    //TODO: show error if ref does not exist

    val result = if (issue.isEmpty) Messages.notFound(ref)
    else if (all.isEmpty) Messages.problem(s"no history for: $ref")
    else List(issue.get.render(currentModel)) ::: all.map(h => s" > ${dateFormats().today(h.when.get)}: ${currentModel.aka(h.who.get)} ${h.action.get}").toList
{'fraction_non_alphanumeric': 0.16938461538461538, 'fraction_numerical': 0.0026153846153846153, 'mean_word_length': 4.257177517185604, 'pattern_counts': {'":': 14, '<': 3, '<?xml version=': 0, '>': 98, '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': '1453043', 'n_tokens_mistral': 4391, 'n_tokens_neox': 4172, 'n_words': 997}