import Elysia, { t } from "elysia";
import Swagger from "@elysiajs/swagger";
import html from "./index.html";
import Dashboard from "./server/routes/darmasaba";
import {apiAuth} from "./server/middlewares/apiAuth";
import Auth from "./server/routes/auth_route";
import ApiKeyRoute from "./server/routes/apikey_route";
import type { User } from "generated/prisma";
import WaRoute from "./server/routes/wa_route";
import WebhookRoute from "./server/routes/webhook_route";
import cors from "@elysiajs/cors";
import WaHookRoute from "./server/routes/wa_hook_route";
import FlowRoute from "./server/routes/flow_route";
import LogsRoute from "./server/routes/logs_route";
import { startClient } from "./server/lib/wa/wa_service";

const Docs = new Elysia().use(
  Swagger({
    path: "/docs",
  }),
);

const ApiUser = new Elysia({
  prefix: "/user",
}).get("/find", (ctx) => {
  const { user } = ctx as any;
  return {
    user: user as User,
  };
});

const Api = new Elysia({
  prefix: "/api",
})
  .use(apiAuth)
  .use(ApiKeyRoute)
  .use(Dashboard)
  .use(ApiUser)
  .use(WaRoute)
  .use(WebhookRoute)
  .use(FlowRoute);

const app = new Elysia()
  .use(cors())
  .use(Api)
  .use(Docs)
  .use(Auth)
  .use(WaHookRoute)
  .use(LogsRoute)
  .get("*", html)
  .listen(3000, () => {
    console.log("Server running at http://localhost:3000");
  });


if (process.env.NODE_ENV === "production") {
  startClient();
}

export type ServerApp = typeof app;
