import _ from 'lodash';
import selectn from 'selectn';
import { Router } from 'express';
import registrationStates from 'constants/registrationStates';
import riotApi from 'utils/riotApi';
import crypto from 'crypto';
import summoners, { findSummoner, insertSummoner } from 'db/summoners';
import matches, { findMatches } from 'db/matches';

/* --- DB/API Helpers --- */

function emailToken(summonerName) {
  const shasum = crypto.createHash('sha1');
  shasum.update(summonerName);
  return shasum.digest('hex');
}

function summonerFields(region, { id, name, profileIconId }) {
  return {
    id,
    region,
    summonerName: name,
    archiveEmailAddress: `replay+${emailToken(name)}@archive.gg`,
    profileIconUrl: riotApi.imgUrl('profileicon', profileIconId),
    registrationState: registrationStates.NOT_REGISTERED,
  };
}

function fetchSummonerFromApi(region, summonerName) {
  let summoner = {};

  return riotApi
    .summoner
    .byName(region, summonerName)
    .then(data => {
      _.assign(summoner, summonerFields(region, data));
      return riotApi.league.bySummoner(region, summoner.id);
    })
    .then(entries => {
      const rankedData = _.findWhere(entries, { queue: 'RANKED_SOLO_5x5' });
      if (rankedData) {
        summoner.division = `${_.capitalize(rankedData.tier.toLowerCase())} ${rankedData.entries[0].division}`;
      }
      return summoner;
    });
}

function fetchMatches(summoner) {
  return findMatches({ summonerId: summoner.id }).then(matches => {
    return {
      ...summoner,
      matches,
    };
  });
}

function fetchSummoner(region, summonerName) {
  return findSummoner({ region, summonerName }).then(summoner => {
    if (summoner) {
      return summoner;
    } else {
      return fetchSummonerFromApi(region, summonerName).then(insertSummoner);
    }
  }).then(fetchMatches);
}

/* --- Routes --- */

const routes = Router();

routes.get('/:region/:summonerName', (req, res) => {
  const { region, summonerName } = req.params;
  fetchSummoner(region, summonerName).then(summoner => {
    res.json(summoner);
  }).catch(error => {
    console.log('ERROR:', error);
    res.status(500).json(error);
  });
});

routes.patch('/:region/:summonerName', (req, res) => {
  const { region, summonerName } = req.params;
  const summoner = getSummoner({ region, summonerName });
  _.assign(summoner, req.body);
  res.json(summoner);
});

export default routes;

@@ -2286,19 +2286,21 @@
 moner = 
-g
+f
 et
+ch
 Summoner

Update fn name (doh!)