/**
 * Docs module — collaborative documentation via Docmost.
 *
 * Wraps the Docmost instance as an external app embedded in the rSpace shell.
 */

import { Hono } from "hono";
import { renderShell, renderExternalAppShell } from "../../server/shell";
import { getModuleInfoList } from "../../shared/module";
import type { RSpaceModule } from "../../shared/module";

const routes = new Hono();

const DOCMOST_URL = "https://docs.cosmolocal.world";

routes.get("/api/health", (c) => {
	return c.json({ ok: true, module: "rdocs" });
});

routes.get("/", (c) => {
	const space = c.req.param("space") || "demo";
	const view = c.req.query("view");

	if (view === "demo") {
		return c.html(renderShell({
			title: `${space} — Docs | rSpace`,
			moduleId: "rdocs",
			spaceSlug: space,
			modules: getModuleInfoList(),
			theme: "dark",
			body: `<div style="max-width:640px;margin:0 auto;padding:3rem 1rem;text-align:center">
	<div style="font-size:3rem;margin-bottom:1rem">📝</div>
	<h2 style="font-size:1.5rem;margin-bottom:0.75rem;background:linear-gradient(135deg,#14b8a6,#22d3ee);-webkit-background-clip:text;-webkit-text-fill-color:transparent">rDocs</h2>
	<p style="color:#94a3b8;margin-bottom:2rem;line-height:1.6">Collaborative documentation powered by Docmost. Create wikis, knowledge bases, and shared documents for your community.</p>
	<a href="?" class="rapp-nav__btn--app-toggle" style="display:inline-block;padding:10px 24px;font-size:0.9rem">Open Docmost</a>
</div>`,
		}));
	}

	// Default: show the external app directly
	return c.html(renderExternalAppShell({
		title: `${space} — Docmost | rSpace`,
		moduleId: "rdocs",
		spaceSlug: space,
		modules: getModuleInfoList(),
		appUrl: DOCMOST_URL,
		appName: "Docmost",
		theme: "dark",
	}));
});

function renderDocsLanding(): string {
	return `<div style="max-width:640px;margin:0 auto;padding:3rem 1rem;text-align:center">
	<div style="font-size:3rem;margin-bottom:1rem">📝</div>
	<h2 style="font-size:1.5rem;margin-bottom:0.75rem;background:linear-gradient(135deg,#14b8a6,#22d3ee);-webkit-background-clip:text;-webkit-text-fill-color:transparent">rDocs</h2>
	<p style="color:#94a3b8;margin-bottom:2rem;line-height:1.6">Collaborative documentation powered by Docmost. Create wikis, knowledge bases, and shared documents for your community.</p>
</div>`;
}

export const docsModule: RSpaceModule = {
	id: "rdocs",
	name: "rDocs",
	icon: "📝",
	description: "Collaborative documentation and knowledge base",
	scoping: { defaultScope: 'global', userConfigurable: true },
	routes,
	landingPage: renderDocsLanding,
	standaloneDomain: "rdocs.online",
	externalApp: { url: DOCMOST_URL, name: "Docmost" },
	feeds: [
		{ id: "documents", name: "Documents", kind: "data", description: "Collaborative documents and wiki pages" },
	],
	acceptsFeeds: ["data"],
	outputPaths: [
		{ path: "documents", name: "Documents", icon: "📝", description: "Collaborative documents" },
		{ path: "wikis", name: "Wikis", icon: "📖", description: "Knowledge base wikis" },
	],
};
