import type { Handler } from "hono";
import { HTTPException } from "hono/http-exception";
import { Profile } from "../components/Profile.tsx";
import { fetchProfile } from "../lib/fetchProfile.ts";

export const getProfile: Handler<
  HonoEnv,
  "/profile/:user" | "/https://bsky.app/profile/:user"
> = async (c) => {
  const { user } = c.req.param();
  const agent = c.get("Agent");
  const { data, success } = await fetchProfile(agent, { user });
  if (!success) {
    throw new HTTPException(500, {
      message: "Failed to fetch the profile!",
    });
  }
  return c.html(
    <Profile
      profile={data}
      url={c.req.path}
      appDomain={process.env.FIXBLUESKY_APP_DOMAIN ?? "bsyy.app"}
    />,
  );
};
