import { Hono } from "hono";
import { cors } from "hono/cors";
import type { Env } from "./types/env";
import { registerMetaRoutes } from "./routes/meta";
import { registerAccountRoutes } from "./routes/accounts";
import { registerCfProxyRoutes } from "./routes/cf-proxy";

/** 组装 Hono：仅注册 /api；静态资源由 Workers Assets + SPA 配置处理 */
export function createApp(): Hono<{ Bindings: Env }> {
  const app = new Hono<{ Bindings: Env }>();

  app.use(
    "/api/*",
    cors({
      origin: "*",
      allowMethods: ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "HEAD"],
      allowHeaders: [
        "Content-Type",
        "Authorization",
        "X-App-Token",
        "X-Requested-With",
      ],
      exposeHeaders: ["Content-Type"],
      maxAge: 86400,
    }),
  );

  registerMetaRoutes(app);
  registerAccountRoutes(app);
  registerCfProxyRoutes(app);

  return app;
}
