import { getServerSession } from "next-auth";
import { notFound, redirect } from "next/navigation";
import { AuthenticationError, AuthorizationError } from "@formbricks/types/errors";
import { hasOrganizationAccess } from "@/lib/auth";
import { getEnvironments } from "@/lib/environment/service";
import { getProject } from "@/lib/project/service";
import { authOptions } from "@/modules/auth/lib/authOptions";

export const GET = async (_: Request, context: { params: Promise<{ projectId: string }> }) => {
  const params = await context?.params;
  const projectId = params.projectId;
  if (!projectId) return notFound();
  // check auth
  const session = await getServerSession(authOptions);
  if (!session) throw new AuthenticationError("Not authenticated");
  const project = await getProject(projectId);
  if (!project) return notFound();
  const hasAccess = await hasOrganizationAccess(session.user.id, project.organizationId);
  if (!hasAccess) throw new AuthorizationError("Unauthorized");
  // redirect to project's production environment
  const environments = await getEnvironments(project.id);
  const prodEnvironment = environments.find((e) => e.type === "production");
  if (!prodEnvironment) return notFound();
  return redirect(`/environments/${prodEnvironment.id}/`);
};
