import { Hono } from "hono";

import { rpcApp } from "./routes/rpc";
import { clientEntry } from "./routes/client-entry";

const app = new Hono();

// Allow all hosts for Tailscale Funnel
app.use("*", async (c, next) => {
  // Accept requests from any host
  return next();
});

// Health check endpoint
app.get("/health", (c) => {
  return c.json({ status: "ok", timestamp: new Date().toISOString() });
});

// Legal config endpoint (temporary fix for RPC issue)
app.get("/api/legal-config", async (c) => {
  try {
    const { getLegalConfig } = await import("./lib/legal-config");
    const config = getLegalConfig();
    return c.json(config);
  } catch (error) {
    console.error("Legal config error:", error);
    return c.json({ error: "Failed to load legal config" }, 500);
  }
});

// Security.txt endpoint (RFC 9116)
app.get("/.well-known/security.txt", (c) => {
  const securityContact = process.env.SECURITY_CONTACT || "security@example.com";
  const securityText = `Contact: ${securityContact}
Expires: 2025-12-31T23:59:59.000Z
Preferred-Languages: de, en
Canonical: https://${process.env.DOMAIN || 'localhost:5173'}/.well-known/security.txt

# Security Policy
# Please report security vulnerabilities responsibly by contacting us via email.
# We will respond to security reports within 48 hours.
# 
# Scope: This security policy applies to the Stargirlnails booking system.
# 
# Rewards: We appreciate security researchers who help us improve our security.
# While we don't have a formal bug bounty program, we may offer recognition
# for significant security improvements.
`;

  return c.text(securityText, 200, {
    "Content-Type": "text/plain; charset=utf-8",
    "Cache-Control": "public, max-age=86400", // Cache for 24 hours
  });
});

app.route("/rpc", rpcApp);
app.get("/*", clientEntry);

export default app;
