import { Hono } from "hono/tiny";
import { createRoute } from "../../modules/api-utils/createRoute";
import { sessions } from "../../managers/sessions";

/* /v1/auth/refresh */
export default createRoute("post", ["/refresh"], async (c) => {
  c.status(201);

  const refreshToken = c.req.header("x-refresh-token") || "";

  const session = await sessions.fetch({
    refreshToken: refreshToken,
  });

  const newAccessToken = await session.refresh(refreshToken);

  return c.json({
    status: 201,
    message: "Token refreshed successfully!",
    data: {
      accessToken: newAccessToken,
      refreshToken,
    },
    successful: true,
  });
});
