/**
 * rAuctions module — community auctions with USDC bidding.
 *
 * Embeds the standalone rauctions.online Next.js app via externalApp iframe.
 * Provides a hub page listing active auctions proxied from the rauctions API.
 */

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

const RAUCTIONS_URL = process.env.RAUCTIONS_URL || "https://rauctions.online";

const routes = new Hono();

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

	if (view === "app") {
		return c.html(renderExternalAppShell({
			title: `${spaceSlug} — Auctions | rSpace`,
			moduleId: "rauctions",
			spaceSlug,
			modules: getModuleInfoList(),
			appUrl: RAUCTIONS_URL,
			appName: "rAuctions",
			theme: "dark",
		}));
	}

	return c.html(renderShell({
		title: `${spaceSlug} — Auctions | rSpace`,
		moduleId: "rauctions",
		spaceSlug,
		modules: getModuleInfoList(),
		theme: "dark",
		body: `<div class="rapp-nav" style="padding:0 1rem;margin-top:8px">
			<span class="rapp-nav__title">Auctions</span>
			<a href="?view=app" class="rapp-nav__btn--app-toggle">Open Full App</a>
		</div>
		<folk-auctions-hub space="${spaceSlug}"></folk-auctions-hub>`,
		scripts: `<script type="module" src="/modules/rauctions/folk-auctions-hub.js"></script>`,
	}));
});

// ── API: proxy health ──
routes.get("/api/health", (c) => {
	return c.json({ status: "ok", service: "rauctions" });
});

export const auctionsModule: RSpaceModule = {
	id: "rauctions",
	name: "rAuctions",
	icon: "🏛",
	description: "Live auctions with USDC bidding",
	scoping: { defaultScope: 'space', userConfigurable: true },
	routes,
	landingPage: renderLanding,
	standaloneDomain: "rauctions.online",
	externalApp: { url: RAUCTIONS_URL, name: "rAuctions" },
	outputPaths: [
		{ path: "live", name: "Live Auctions", icon: "🔨", description: "Currently active auctions" },
		{ path: "ended", name: "Ended", icon: "🏆", description: "Completed auction results" },
	],
};
