const express = require('express');
const router = express.Router();

const Games = require('../models/GamesPs');

// 更新比赛列表
router.post('/update_games_list', (req, res) => {
  const { platform, mk, games } = req.body ?? {};
  Games.updateGamesList({ platform, mk, games })
  .then(() => {
    res.sendSuccess();
  })
  .catch(err => {
    res.badRequest(err.message);
  })
});

// 更新比赛盘口
router.post('/update_games_events', (req, res) => {
  const { platform, mk, games, outrights } = req.body ?? {};
  Games.updateGamesEvents({ platform, mk, games, outrights })
  .then(updateCount => {
    res.sendSuccess({ updateCount });
  })
  .catch(err => {
    res.badRequest(err.message);
  })
});

// 更新联赛列表
router.post('/update_leagues_list', (req, res) => {
  const { mk, leagues } = req.body ?? {};
  const updateCount = Games.updateLeaguesList({ mk, leagues });
  res.sendSuccess({ updateCount });
});

// 更新比赛结果
router.post('/update_games_result', (req, res) => {
  const { date, list } = req.body ?? {};
  Games.updateGamesResult({ date, list })
  .then(() => {
    res.sendSuccess();
  })
  .catch(err => {
    res.badRequest(err.message);
  });
});

// 获取筛选过的联赛
router.get('/get_filtered_leagues', (req, res) => {
  const { mk } = req.query;
  Games.getFilteredLeagues(mk)
  .then(filteredLeagues => {
    res.sendSuccess(filteredLeagues);
  })
  .catch(err => {
    res.badRequest(err.message);
  });
});

// 获取关联列表
router.get('/get_games_relation', (req, res) => {
  const { mk } = req.query;
  const gamesRelation = Games.getGamesRelation({ mk });
  res.sendSuccess(gamesRelation);
});

// 获取中单方案
router.get('/get_solutions', (req, res) => {
  Games.getSolutions()
  .then(({ solutions, gamesEvents }) => {
    res.sendSuccess({ solutions, gamesEvents });
  })
  .catch(err => {
    res.badRequest(err.message);
  });
});

// 获取综合利润方案
router.post('/calc_total_profit', (req, res) => {
  const [sid1, sid2, inner_base, inner_rebate] = req.body;
  Games.getTotalProfitWithSid(sid1, sid2, inner_base, inner_rebate)
  .then(totalProfit => {
    res.sendSuccess(totalProfit);
  })
  .catch(err => {
    res.badRequest(err.message);
  });
});

// 获取自定义综合利润
router.post('/calc_custom_total_profit', (req, res) => {
  const [betInfo1, betInfo2, fixed, inner_base, inner_rebate] = req.body;
  Games.getTotalProfitWithBetInfo(betInfo1, betInfo2, fixed, inner_base, inner_rebate)
  .then(totalProfit => {
    res.sendSuccess(totalProfit);
  })
  .catch(err => {
    res.badRequest(err.message);
  });
});

module.exports = router;