// @flow

const bl = require('bl');
const { bufferEq } = require('buffer-equal-constant-time');
const { extractWebhookData } = require('./extractWebhookData');

const { sendToAmazeeioWebhooks } = require('./sendToAmazeeioWebhooks');
const { sendToAmazeeioLogs, initSendToAmazeeioLogs } = require('@amazeeio/lagoon-commons/src/logs');

import type { Logger } from '@amazeeio/lagoon-commons/src/local-logging';
import type { ChannelWrapper } from './types';

type Req = http$IncomingMessage;
type Res = http$ServerResponse;

type Cb = () => void;

type Options = {
  path: string,
  channelWrapperWebhooks: ChannelWrapper,
};

type Handler = (req: Req, res: Res, logger: Logger, cb: Cb) => void;

initSendToAmazeeioLogs();

function createReqHandler(options: Options): Handler {
  const {
    path,
    channelWrapperWebhooks,
  } = options;

  /**
   * Parses given request for webhook related data and will forward
   * it to given EventEmitter for further computation
   */
  function handler (req, res, logger, cb) {
    if (req.url.split('?').shift() !== path) {
      return cb();
    }

    const endWithError = (msg) => {
      res.writeHead(400, { 'content-type': 'application/json' });
      res.end(JSON.stringify({ error: msg }));

      logger.error('Error:', msg);
    };

    req.pipe(bl((err, data) => {
      if (err) {
        return endWithError(err.message);
      }

      try {
        const { method } = req;

        const webhookData = extractWebhookData(req, data.toString());

        const {
          webhooktype,
          event,
          giturl,
          uuid,
          body,
        } = webhookData;

        const meta = {
          webhooktype: webhooktype,
          event: event,
          giturl: giturl,
          rawbody: data.toString(),
        }
        console.log(`Calling sendToAmazeeioLogs`)
        sendToAmazeeioLogs('info', "", uuid, "webhooks:receive",  meta,
          `Received new ${webhooktype} webhook,  event: ${event}, giturl: ${giturl}`
        )

        sendToAmazeeioWebhooks(webhookData, channelWrapperWebhooks);

        res.writeHead(200, { 'content-type': 'application/json' });

        const responseData = JSON.stringify({ data: webhookData }, null, 2)
        res.end(responseData);
      }
      catch(e) {
        return endWithError(e.message);
      }
    }))
  }

  return handler;
}

module.exports = createReqHandler;

@@ -94,18 +94,16 @@
 );%0Aconst
- %7B
  extract
@@ -113,18 +113,16 @@
 hookData
- %7D
  = requi
@@ -151,26 +151,24 @@
 ta');%0A%0Aconst
- %7B
  sendToAmaze
@@ -178,18 +178,16 @@
 Webhooks
- %7D
  = requi

fix for extractWebhookData is not a function