import { Route, Server } from '../../../class';

export default class Keys extends Route {
  constructor(server: Server) {
    super(server);
    this.conf = {
      path: '/keys',
    };
  }

  protected pre() {
    this.router.all('*', (_req, res, next) => {
      res.setHeader('Access-Control-Allow-Origin', '*');
      res.setHeader('Access-Control-Allow-Methods', '*');
      res.setHeader('Access-Control-Allow-Headers', '*');
      next();
    });
  }

  public bind() {
    this.pre();

    this.router.get('/x509/:userID', async (req, res) => {
      const memberDoc = await this.server.client.db.Member.findOne({ userID: req.params.userID }).lean().exec();
      const member = this.mainGuild.members.get(req.params.userID);
      if (!member || !memberDoc || !memberDoc?.x509) return res.status(404).json({ code: this.constants.codes.NOT_FOUND, message: this.constants.messages.NOT_FOUND });
      return res.status(200).json({
        code: this.constants.codes.SUCCESS,
        message: memberDoc.x509,
      });
    });
  }
}
