import { validationMetadatasToSchemas } from "@odit/class-validator-jsonschema";
import express, { Application } from "express";
import path from 'path';
import { getMetadataArgsStorage } from "routing-controllers";
import { generateSpec } from '../apispec';

/**
 * Loader for everything openapi related - from creating the schema to serving it via a static route and swaggerUiExpress.
 * All auth schema related stuff also has to be configured here
 */
export default async (app: Application) => {
  const storage = getMetadataArgsStorage();
  const schemas = validationMetadatasToSchemas({
    refPointerPrefix: "#/components/schemas/",
  });

  //Spec creation based on the previously created schemas
  const spec = generateSpec(storage, schemas);
  app.get(["/docs/openapi.json", "/docs/swagger.json"], (req, res) => {
    res.json(spec);
  });
  app.use('/docs', express.static(path.join(__dirname, '../static/docs'), { index: "index.html", extensions: ['html'] }));
  return app;
};
