import { Hono } from "hono/tiny";
import { createRoute } from "../../modules/api-utils/createRoute";
import { API_BASE_URL } from "../../constants";
import cuid from "cuid";

/* /v1/auth/uri */
export default createRoute("get", ["/uri"], (c) => {
  c.status(200);

  const callbackKey = cuid();
  const redirectUri = encodeURIComponent(`${API_BASE_URL}/v1/auth/redirect`);
  const msUri = `https://login.microsoftonline.com/${process.env.MICROSOFT_TENANT_ID}/oauth2/v2.0/authorize?client_id=${process.env.MICROSOFT_CLIENT_ID}&response_type=code&redirect_uri=${redirectUri}&scope=openid+User.Read&state=${callbackKey}&prompt=login`;

  return c.json({
    status: 200,
    message: "Successfully fetch Auth URI",
    data: {
      uri: msUri,
      callbackKey: callbackKey,
    },
    successful: true,
  });
});
