<commit_before>"use strict";

var moment = require('moment-timezone');
var luid = require('./utils').luid;

moment.tz.setDefault("Asia/Tokyo");
moment.locale('ja', {
    weekdays: ["日曜日","月曜日","火曜日","水曜日","木曜日","金曜日","土曜日"],
    weekdaysShort: ["日","月","火","水","木","金","土"]
});

function ZangyoBot(controller) {
  var zangyo_bot = {};

  zangyo_bot.ranges = {
    today: "今日",
    yesterday: "昨日",
    day_before_yesterday: "一昨日",
    this_week: "今週",
    last_week: "先週",
    past_one_week: "過去一週間",
    this_month: "今月",
    last_month: "先月",
    month_before_last: "先々月"
  };

  zangyo_bot.filters = {
    all: "全て",
    approved: "承認済み",
    last: "承認済みで最後の"
  };

  zangyo_bot.applicationWizard = function(err, convo) {
    askApprover(convo);
  };

  function askApprover(convo) {
    convo.ask("プロマネは誰？ [@xxx]", function(response, convo) {
      if (response.text.match(/(cancel|キャンセル|やめる)/)) {
        convo.say('キャンセルしたよ！');
        convo.next();
      } else if (!response.text.match(/^\<\@[a-zA-Z0-9]+\>/g)) {
        convo.say('@xxx の形式でユーザーを１人指定してね。');
        convo.repeat();
        convo.next();
      } else {
        var zangyo = {}
        zangyo.id = "Z" + response.user + "-" + luid();
        zangyo.team = response.team;
        zangyo.date = moment().format("YYYY-MM-DD");
        zangyo.applicant = response.user;
        zangyo.approver = response.text.match(/^\<\@[a-zA-Z0-9]+\>/g)[0].slice(2, -1);
        askEndTime(convo, zangyo);
        convo.next();
      }
    });
  };

  function askEndTime(convo, zangyo) {
    convo.ask("何時に終わる？ [HH:MM]", function(response, convo) {
      if (response.text.match(/(cancel|キャンセル|やめる)/)) {
        convo.say('キャンセルしたよ！');
        convo.next();
      } else if (!response.text.match(/^([0-2]?[0-9]):([0-5]?[0-9])$/)) {
        convo.say('HH:MM の形式で時間を指定してね。29:59まで指定できるよ。');
        convo.repeat();
        convo.next();
      } else {
        zangyo.end_time = response.text;
        askReason(convo, zangyo);
        convo.next();
      }
    });
  };

  function askReason(convo, zangyo) {
    convo.ask("残業する理由は？", function(response, convo) {
      zangyo.reason = response.text;
      controller.storage.zangyos.save(zangyo);
      var summary = getApplicationSummary(zangyo, true);
      convo.say(summary);
      convo.next();
    });
  };

  zangyo_bot.createApplication = function(bot, message, approver, end_time, reason) {
    controller.storage.teams.get(message.team_id, function(err, team) {
      var api_data = {"token": team.token};
      bot.api.callAPI('users.list', api_data, function(err, data){
        if (data.members) {
          for (var i = 0; i < data.members.length; i++) {
            var member = data.members[i];

            if (!member.deleted && member.name == approver) {
              var zangyo = {}
              zangyo.id = "Z" + message.user_id + "-" + luid();
              zangyo.team = message.team_id;
              zangyo.date = moment().format("YYYY-MM-DD");
              zangyo.applicant = message.user_id;
              zangyo.approver = member.id;
              zangyo.end_time = end_time;
              zangyo.reason = reason;
              controller.storage.zangyos.save(zangyo);
              var summary = getApplicationSummary(zangyo, false);
              bot.replyPrivate(message, summary);
              break;
            }
            if (i == data.members.length-1) {
              bot.replyPrivate(message, '`Approver` is invalid!!');
            }
          }
        } else {
          bot.replyPrivate(message, '`Approver` is invalid!!');
        }
      });
    });
  };

  function getApplicationSummary(zangyo, can_redo) {
    var actions = [
      {
        "type": "button",
        "name": "apply",<commit_msg>id cant include hyphen<commit_after>"use strict";

var moment = require('moment-timezone');
var luid = require('./utils').luid;

moment.tz.setDefault("Asia/Tokyo");
moment.locale('ja', {
    weekdays: ["日曜日","月曜日","火曜日","水曜日","木曜日","金曜日","土曜日"],
    weekdaysShort: ["日","月","火","水","木","金","土"]
});

function ZangyoBot(controller) {
  var zangyo_bot = {};

  zangyo_bot.ranges = {
    today: "今日",
    yesterday: "昨日",
    day_before_yesterday: "一昨日",
    this_week: "今週",
    last_week: "先週",
    past_one_week: "過去一週間",
    this_month: "今月",
    last_month: "先月",
    month_before_last: "先々月"
  };

  zangyo_bot.filters = {
    all: "全て",
    approved: "承認済み",
    last: "承認済みで最後の"
  };

  zangyo_bot.applicationWizard = function(err, convo) {
    askApprover(convo);
  };

  function askApprover(convo) {
    convo.ask("プロマネは誰？ [@xxx]", function(response, convo) {
      if (response.text.match(/(cancel|キャンセル|やめる)/)) {
        convo.say('キャンセルしたよ！');
        convo.next();
      } else if (!response.text.match(/^\<\@[a-zA-Z0-9]+\>/g)) {
        convo.say('@xxx の形式でユーザーを１人指定してね。');
        convo.repeat();
        convo.next();
      } else {
        var zangyo = {}
        zangyo.id = "Z" + response.user + luid();
        zangyo.team = response.team;
        zangyo.date = moment().format("YYYY-MM-DD");
        zangyo.applicant = response.user;
        zangyo.approver = response.text.match(/^\<\@[a-zA-Z0-9]+\>/g)[0].slice(2, -1);
        askEndTime(convo, zangyo);
        convo.next();
      }
    });
  };

  function askEndTime(convo, zangyo) {
    convo.ask("何時に終わる？ [HH:MM]", function(response, convo) {
      if (response.text.match(/(cancel|キャンセル|やめる)/)) {
        convo.say('キャンセルしたよ！');
        convo.next();
      } else if (!response.text.match(/^([0-2]?[0-9]):([0-5]?[0-9])$/)) {
        convo.say('HH:MM の形式で時間を指定してね。29:59まで指定できるよ。');
        convo.repeat();
        convo.next();
      } else {
        zangyo.end_time = response.text;
        askReason(convo, zangyo);
        convo.next();
      }
    });
  };

  function askReason(convo, zangyo) {
    convo.ask("残業する理由は？", function(response, convo) {
      zangyo.reason = response.text;
      controller.storage.zangyos.save(zangyo);
      var summary = getApplicationSummary(zangyo, true);
      convo.say(summary);
      convo.next();
    });
  };

  zangyo_bot.createApplication = function(bot, message, approver, end_time, reason) {
    controller.storage.teams.get(message.team_id, function(err, team) {
      var api_data = {"token": team.token};
      bot.api.callAPI('users.list', api_data, function(err, data){
        if (data.members) {
          for (var i = 0; i < data.members.length; i++) {
            var member = data.members[i];

            if (!member.deleted && member.name == approver) {
              var zangyo = {}
              zangyo.id = "Z" + message.user_id + luid();
              zangyo.team = message.team_id;
              zangyo.date = moment().format("YYYY-MM-DD");
              zangyo.applicant = message.user_id;
              zangyo.approver = member.id;
              zangyo.end_time = end_time;
              zangyo.reason = reason;
              controller.storage.zangyos.save(zangyo);
              var summary = getApplicationSummary(zangyo, false);
              bot.replyPrivate(message, summary);
              break;
            }
            if (i == data.members.length-1) {
              bot.replyPrivate(message, '`Approver` is invalid!!');
            }
          }
        } else {
          bot.replyPrivate(message, '`Approver` is invalid!!');
        }
      });
    });
  };

  function getApplicationSummary(zangyo, can_redo) {
    var actions = [
      {
        "type": "button",
        "name": "apply",
{'fraction_non_alphanumeric': 0.17784295427276364, 'fraction_numerical': 0.005865884548726837, 'mean_word_length': 2.2845884413309983, 'pattern_counts': {'":': 6, '<': 9, '<?xml version=': 0, '>': 7, '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': '4778037', 'n_tokens_mistral': 2889, 'n_tokens_neox': 2844, 'n_words': 408}