import { Hono } from 'hono';
import type { Bindings } from '../types';
import { OPENAPI_SPEC } from '../lib/openapi';

export const docsRouter = new Hono<{ Bindings: Bindings }>();

// GET /openapi.json
docsRouter.get('/openapi.json', (c) => {
  return c.json(OPENAPI_SPEC);
});

// GET /docs — Swagger UI
docsRouter.get('/docs', (c) => {
  const specStr = JSON.stringify(OPENAPI_SPEC);
  const htmlStr = `<!doctype html>
<html>
  <head>
    <title>Cypher Executor API Docs</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist@4/swagger-ui.css">
    <style>html { box-sizing: border-box; overflow: -moz-scrollbars-vertical; overflow-y: scroll; } *, *:before, *:after { box-sizing: inherit; } body { margin:0; padding:0; }</style>
  </head>
  <body>
    <div id="swagger-ui"></div>
    <script src="https://unpkg.com/swagger-ui-dist@4/swagger-ui-bundle.js"> </script>
    <script src="https://unpkg.com/swagger-ui-dist@4/swagger-ui-standalone-preset.js"> </script>
    <script>
    window.onload = () => {
      window.ui = SwaggerUIBundle({
        spec: ${specStr},
        dom_id: '#swagger-ui',
        deepLinking: true,
        presets: [
          SwaggerUIBundle.presets.apis,
          SwaggerUIStandalonePreset
        ],
        plugins: [
          SwaggerUIBundle.plugins.DownloadUrl
        ],
        layout: "BaseLayout"
      })
    }
  </script>
  </body>
</html>
  `;
  return c.html(htmlStr);
});
