import type { Handler } from "hono";
import { HTTPException } from "hono/http-exception";
import { Post } from "../components/Post.tsx";
import { fetchPost } from "../lib/fetchPostData.ts";

export const getPost: Handler<
  HonoEnv,
  | "/profile/:user/post/:post"
  | "/https://bsky.app/profile/:user/post/:post"
> = async (c) => {
  const { user, post } = c.req.param();
  const agent = c.get("Agent");
  const { data, success } = await fetchPost(agent, { user, post });
  if (!success) {
    throw new HTTPException(500, {
      message: "Failed to fetch the post!",
    });
  }

  return c.html(
    <Post
      post={data.posts[0]}
      url={c.req.path}
      appDomain={process.env.FIXBLUESKY_APP_DOMAIN ?? "bsyy.app"}
    />,
  );
};
