import { cors } from "@elysiajs/cors";
import { swagger } from "@elysiajs/swagger";
import Elysia, { t } from "elysia";
import { apiMiddleware } from "../middleware/apiMiddleware";
import { auth } from "../utils/auth";
import { apikey } from "./apikey";
import { complaint } from "./complaint";
import { dashboard } from "./dashboard";
import { division } from "./division";
import { event } from "./event";
import { noc } from "./noc";
import { profile } from "./profile";
import { resident } from "./resident";

const isProduction = process.env.NODE_ENV === "production";

const api = new Elysia({
	prefix: "/api",
})
	.use(cors())
	.get("/health", () => ({ ok: true }), {
		response: {
			200: t.Object({ ok: t.Boolean() }),
		},
	})
	.all("/auth/*", ({ request }) => auth.handler(request))
	.get(
		"/session",
		async ({ request }) => {
			const data = await auth.api.getSession({ headers: request.headers });
			return { data };
		},
		{
			response: {
				200: t.Object({ data: t.Any() }),
			},
		},
	)
	.use(noc)
	.use(apiMiddleware)
	.use(apikey)
	.use(profile)
	.use(division)
	.use(complaint)
	.use(resident)
	.use(event)
	.use(dashboard);

if (!isProduction) {
	api.use(
		swagger({
			path: "/docs",
			documentation: {
				info: {
					title: "Bun + React API",
					version: "1.0.0",
				},
			},
		}),
	);
}

export default api;
