import { getServerSession } from "next-auth";
import { TEnvironment } from "@formbricks/types/environment";
import { TSurvey } from "@formbricks/types/surveys/types";
import { TTag } from "@formbricks/types/tags";
import { getProjectByEnvironmentId } from "@/lib/project/service";
import { getResponsesByContactId } from "@/lib/response/service";
import { getSurveys } from "@/lib/survey/service";
import { getUser } from "@/lib/user/service";
import { findMatchingLocale } from "@/lib/utils/locale";
import { getTranslate } from "@/lingodotdev/server";
import { authOptions } from "@/modules/auth/lib/authOptions";
import { getProjectPermissionByUserId } from "@/modules/ee/teams/lib/roles";
import { ResponseTimeline } from "./response-timeline";

interface ResponseSectionProps {
  environment: TEnvironment;
  contactId: string;
  environmentTags: TTag[];
}

export const ResponseSection = async ({ environment, contactId, environmentTags }: ResponseSectionProps) => {
  const responses = await getResponsesByContactId(contactId);
  const surveyIds = responses?.map((response) => response.surveyId) || [];
  const surveys: TSurvey[] = surveyIds.length === 0 ? [] : ((await getSurveys(environment.id)) ?? []);
  const session = await getServerSession(authOptions);

  const t = await getTranslate();
  if (!session) {
    throw new Error(t("common.session_not_found"));
  }

  const user = await getUser(session.user.id);

  if (!user) {
    throw new Error(t("common.user_not_found"));
  }

  if (!responses) {
    throw new Error(t("environments.contacts.no_responses_found"));
  }

  const project = await getProjectByEnvironmentId(environment.id);

  if (!project) {
    throw new Error(t("common.workspace_not_found"));
  }

  const projectPermission = await getProjectPermissionByUserId(session.user.id, project.id);

  const locale = await findMatchingLocale();

  return (
    <ResponseTimeline
      user={user}
      surveys={surveys}
      responses={responses}
      environment={environment}
      environmentTags={environmentTags}
      locale={locale}
      projectPermission={projectPermission}
    />
  );
};
