import express from 'express';
const router = express.Router();

import authMiddleware from '../middleware/authMiddleware.js';

import Control from '../models/Control.js';
import Logs from '../libs/logs.js';

router.get('/update_code', authMiddleware, (req, res) => {
  Control.gitPullCurrent()
  .then(result => {
    res.sendSuccess(result);
  })
  .catch(err => {
    Logs.errDev('更新代码失败:', err);
    res.badRequest(err.message);
  });
});

router.get('/restart_sporttery', authMiddleware, (req, res) => {
  const hot = req.query.hot === 'true';
  Control.pm2RestartSporttery(hot)
  .then(result => {
    res.sendSuccess(result);
  })
  .catch(err => {
    Logs.errDev('重启 sporttery 服务失败:', err);
    res.badRequest(err.message);
  });
});

router.get('/restart_pinnacle', authMiddleware, (req, res) => {
  const hot = req.query.hot === 'true';
  Control.pm2RestartPinnacle(hot)
  .then(result => {
    res.sendSuccess(result);
  })
  .catch(err => {
    Logs.errDev('重启 pinnacle 服务失败:', err);
    res.badRequest(err.message);
  });
});

router.get('/release_web', authMiddleware, (req, res) => {
  Control.releaseWeb()
  .then(result => {
    res.sendSuccess(result);
  })
  .catch(err => {
    Logs.errDev('发布 web 服务失败:', err);
    res.badRequest(err.message);
  });
});

export default router;
