import * as md5 from "md5";
import { NAME_MAX_LENGTH, NAME_MIN_LENGTH } from "./base";
import { LambdaInvoker } from "./LambdaInvoker";

/**
 * @todo: Avoid redundant manual invocations, using ip filter.
 */
export class ActivityReportInvoker extends LambdaInvoker {
  public resolvePayload(args: any) {
    const { name, namespace, version, status } = args;
    const id = md5((namespace || "none") + name);
    const type = status;
    const value = version;

    return JSON.stringify({ id, type, value });
  }

  public resolveResponse(response: any) {
    return true;
  }
}

const executionReport = new ActivityReportInvoker("activities", "log", {
  type: "object",
  properties: {
    name: {
      type: "string",
      format: "name",
      maxLength: NAME_MAX_LENGTH,
      minLength: NAME_MIN_LENGTH,
    },
    namespace: {
      type: "string",
      format: "name",
      maxLength: NAME_MAX_LENGTH,
      minLength: NAME_MIN_LENGTH,
    },
    version: {
      type: "string",
      format: "semver",
    },
    status: {
      type: "string",
      enum: ["SUCCESSFUL", "FAILED"],
    },
  },
  required: ["name", "version", "status"],
});

const getActivities = new LambdaInvoker("activities", "list", {
  type: "object",
  properties: {
    id: { type: "string" },
  },
  required: ["id"],
});

export { getActivities, executionReport };
