import { prisma } from "@formbricks/database";
import type { Prisma } from "@formbricks/database/prisma";
import { ZId } from "@formbricks/types/common";
import { InvalidInputError, ResourceNotFoundError, ValidationError } from "@formbricks/types/errors";
import { TBaseFilters, TSegmentFilter, TSegmentSurveyInteractionFilter } from "@formbricks/types/segment";
import type { TSurveyActorContext } from "@/lib/survey/visibility/actor-context";
import { andVisibleSurveys, buildHiddenSurveyWhere } from "@/lib/survey/visibility/predicate";
import {
  collectSegmentFilterSurveyIds,
  restoreHiddenSurveyIds,
} from "@/modules/ee/contacts/segments/lib/filter-survey-ids";
import { getSegment } from "@/modules/ee/contacts/segments/lib/segments";
import {
  SURVEY_WORKSPACE_LOOKUP_BATCH_SIZE,
  isResourceFilter,
} from "@/modules/ee/contacts/segments/lib/utils";

/**
 * Throws when a filter's referenced segment is the segment being checked, or (recursively) one of its
 * filters references it.
 */
const checkReferencedSegment = async (segmentIdFromRoot: string, segmentId: string) => {
  if (segmentIdFromRoot === segmentId) {
    throw new InvalidInputError("Recursive segment filter is not allowed");
  }

  const segment = await getSegment(segmentIdFromRoot);

  if (segment) {
    // recurse into this segment and check for recursive filters:
    const segmentFilters = segment.filters;

    if (segmentFilters) {
      await checkForRecursiveSegmentFilter(segmentFilters, segmentId);
    }
  }
};

/**
 * Checks if a segment filter contains a recursive reference to itself
 * @param filters - The filters to check for recursive references
 * @param segmentId - The ID of the segment being checked
 * @throws {InvalidInputError} When a recursive segment filter is detected
 */
export const checkForRecursiveSegmentFilter = async (filters: TBaseFilters, segmentId: string) => {
  for (const filter of filters) {
    const { resource } = filter;
    if (isResourceFilter(resource)) {
      if (resource.root.type === "segment") {
        await checkReferencedSegment(resource.root.segmentId, segmentId);
      }
    } else {
      await checkForRecursiveSegmentFilter(resource, segmentId);
    }
  }
};

/**
 * Collects all surveyIds referenced by "specific" scope survey-interaction filters in the (nested)
 * filter tree. Filters scoped to "any" survey contribute no ids (they only read workspace-visible
 * surveys), and nested segment filters are not followed: `segmentFiltersReadHiddenSurvey` does (ENG-6870).
 */
export const collectSurveyIdsFromSegmentFilters = (filters: TBaseFilters): string[] => {
  const surveyIds: string[] = [];

  for (const filter of filters) {
    const { resource } = filter;
    if (isResourceFilter(resource)) {
      if (resource.root.type === "surveyInteraction") {
        const { value } = resource as TSegmentSurveyInteractionFilter;
        if (value.surveyScope === "specific") {
          surveyIds.push(...value.surveyIds);
        }
      }
    } else {
      surveyIds.push(...collectSurveyIdsFromSegmentFilters(resource));
    }
  }

  return surveyIds;
};

/**
 * Ensures every id in `surveyIds` is a survey of `workspaceId` that `visibleSurveyWhere` admits (the
 * caller's visibility predicate, `getUserVisibleSurveyWhere`), in bounded batches — one query per
 * batch, never one per survey. A caller that has already passed `workspace.write` on that workspace and
 * is admitted here holds `survey.write` on each of them: the predicate admits a workspace-visible survey,
 * the owner's own restricted or pending one, or any survey for an organization owner/manager — the three
 * arms of `survey#write` (ENG-3282). An id it does not admit is rejected exactly like an unknown one.
 * @throws {InvalidInputError} When a survey is not found in the workspace or not visible to the caller
 */
const findSurveysInWorkspace = (
  surveyIds: ReadonlyArray<string>,
  workspaceId: string,
  visibleSurveyWhere: Prisma.SurveyWhereInput
) =>
  prisma.survey.findMany({
    where: { id: { in: [...surveyIds] }, workspaceId, ...andVisibleSurveys(visibleSurveyWhere) },
    select: { id: true },
  });

export const assertSurveysInWorkspace = async (
  surveyIds: ReadonlyArray<string>,
  workspaceId: string,
  visibleSurveyWhere: Prisma.SurveyWhereInput = {}
) => {
  const uniqueIds = Array.from(new Set(surveyIds));

  for (let i = 0; i < uniqueIds.length; i += SURVEY_WORKSPACE_LOOKUP_BATCH_SIZE) {
    const batch = uniqueIds.slice(i, i + SURVEY_WORKSPACE_LOOKUP_BATCH_SIZE);
    // Sequential on purpose: the first missing id rejects before any further query runs.
    const foundSurveys = await findSurveysInWorkspace(batch, workspaceId, visibleSurveyWhere); // NOSONAR

    const foundIds = new Set(foundSurveys.map((survey) => survey.id));
    const missingId = batch.find((id) => !foundIds.has(id));

    if (missingId) {
      throw new InvalidInputError(`Survey not found in workspace: ${missingId}`);
    }
  }
};

/**
 * Ensures every survey referenced by a "specific" survey-interaction filter belongs to the given
 * workspace. This is the tenancy guard for interaction filters — the runtime evaluation query is
 * already workspace-scoped, but we reject unknown/foreign ids at write time to avoid persisting
 * dead references. The deduplicated ids are looked up in bounded batches, sequentially: each batch
 * is checked before the next query runs, so the first missing id (in collection order) still
 * rejects, and no further queries are issued after a rejection. Callers pass ZSegmentFilters-parsed
 * trees, so the total is already capped (MAX_SEGMENT_SURVEY_INTERACTION_IDS_PER_TREE — a handful of
 * batches at most); the chunking is defense in depth for each query's parameter payload and for any
 * future caller that skips the parse.
 * @throws {InvalidInputError} When a referenced survey is not found in the workspace
 */
export const assertSurveyInteractionSurveyIds = (
  filters: TBaseFilters,
  workspaceId: string,
  visibleSurveyWhere: Prisma.SurveyWhereInput = {}
) => assertSurveysInWorkspace(collectSurveyIdsFromSegmentFilters(filters), workspaceId, visibleSurveyWhere);

/**
 * `filters` as saved, with the stored survey ids this caller may not see put back into the filters
 * they came from (see `restoreHiddenSurveyIds`). One query over the stored tree's ids.
 */
export const restoreHiddenSegmentSurveyIds = async (
  filters: TBaseFilters,
  storedFilters: TBaseFilters,
  workspaceId: string,
  visibleSurveyWhere: Prisma.SurveyWhereInput
): Promise<TBaseFilters> => {
  const storedIds = [...new Set(collectSegmentFilterSurveyIds(storedFilters))];
  if (storedIds.length === 0) return filters;
  const visible = await prisma.survey.findMany({
    where: { AND: [{ workspaceId, id: { in: storedIds } }, visibleSurveyWhere] },
    select: { id: true },
  });
  return restoreHiddenSurveyIds(filters, storedFilters, new Set(visible.map((survey) => survey.id)));
};

/**
 * Every survey id the tree's `specific` survey-interaction filters name, at any depth and through nested
 * segment filters, the way evaluation resolves them (`buildSegmentFilterWhereClause`): a segment is
 * followed once, and a missing one or another workspace's reads nothing.
 */
/** The survey ids a `specific` survey-interaction filter names; none for `any` or a half-built node. */
const specificSurveyIdsOf = (resource: TSegmentFilter): string[] => {
  // `any` scope only reads workspace-visible surveys (see `survey-interaction.ts`).
  const { value } = resource as Partial<TSegmentSurveyInteractionFilter>;
  if (value?.surveyScope !== "specific" || !Array.isArray(value.surveyIds)) return [];
  // A value that is not an id can name no survey, and must not reach an id-validated lookup (a draft may
  // have stored one): it reads nothing, like a dangling nested segment.
  return value.surveyIds.filter((surveyId): surveyId is string => ZId.safeParse(surveyId).success);
};

/**
 * The filters of a nested segment as evaluation reads them, or `null` when it reads nothing: a dangling or
 * malformed reference matches nobody, and another workspace's segment is ignored
 * (`buildSegmentFilterWhereClause`).
 */
const getNestedSegmentFilters = async (
  segmentId: string,
  workspaceId: string
): Promise<TBaseFilters | null> => {
  try {
    const nested = await getSegment(segmentId);
    return nested.workspaceId === workspaceId ? nested.filters : null;
  } catch (error) {
    if (error instanceof ResourceNotFoundError || error instanceof ValidationError) return null;
    throw error;
  }
};

const collectReachableSurveyIds = async (
  filters: TBaseFilters,
  workspaceId: string,
  surveyIds: Set<string>,
  visitedSegmentIds: Set<string>
): Promise<void> => {
  // Draft saves skip filter validation (`updateSurveyDraft`), so every node is read defensively: a
  // half-built one names no survey.
  if (!Array.isArray(filters)) return;
  for (const filter of filters) {
    const resource = filter?.resource;
    if (Array.isArray(resource)) {
      await collectReachableSurveyIds(resource, workspaceId, surveyIds, visitedSegmentIds);
    } else if (resource && isResourceFilter(resource)) {
      await collectFromLeaf(resource, workspaceId, surveyIds, visitedSegmentIds);
    }
  }
};

const collectFromLeaf = async (
  resource: TSegmentFilter,
  workspaceId: string,
  surveyIds: Set<string>,
  visitedSegmentIds: Set<string>
): Promise<void> => {
  if (resource.root?.type === "surveyInteraction") {
    for (const surveyId of specificSurveyIdsOf(resource)) surveyIds.add(surveyId);
    return;
  }
  if (resource.root?.type !== "segment") return;

  const { segmentId } = resource.root;
  if (typeof segmentId !== "string" || visitedSegmentIds.has(segmentId)) return;
  visitedSegmentIds.add(segmentId);

  const nestedFilters = await getNestedSegmentFilters(segmentId, workspaceId);
  if (nestedFilters)
    await collectReachableSurveyIds(nestedFilters, workspaceId, surveyIds, visitedSegmentIds);
};

const findHiddenSurvey = (where: Prisma.SurveyWhereInput) =>
  prisma.survey.findFirst({ where, select: { id: true } });

/** `collectReachableSurveyIds` from the top of a tree. */
const getReachableSurveyIds = async (
  filters: TBaseFilters,
  workspaceId: string,
  segmentId?: string
): Promise<Set<string>> => {
  const reachable = new Set<string>();
  await collectReachableSurveyIds(filters, workspaceId, reachable, new Set(segmentId ? [segmentId] : []));
  return reachable;
};

/** Whether any of `surveyIds` is a survey of `workspaceId` that `buildHiddenSurveyWhere` hides from `actor`. */
const anySurveyHiddenFrom = async (
  surveyIds: ReadonlyArray<string>,
  workspaceId: string,
  hiddenSurveyWhere: Prisma.SurveyWhereInput
): Promise<boolean> => {
  for (let i = 0; i < surveyIds.length; i += SURVEY_WORKSPACE_LOOKUP_BATCH_SIZE) {
    const batch = surveyIds.slice(i, i + SURVEY_WORKSPACE_LOOKUP_BATCH_SIZE);
    // Sequential on purpose: the first hidden survey decides, and no further query runs.
    const batchWhere: Prisma.SurveyWhereInput = { id: { in: batch }, workspaceId, AND: [hiddenSurveyWhere] };
    if (await findHiddenSurvey(batchWhere)) return true; // NOSONAR
  }
  return false;
};

/**
 * ENG-6870: whether evaluating `filters` would tell `actor` who saw, started or completed a survey hidden
 * from them — that is, whether any survey the tree names, directly or through nested segment filters, is
 * one `buildHiddenSurveyWhere` hides from `actor`. Listing a segment's contacts, or targeting a survey
 * whose responses the actor reads, is only allowed when it is not.
 *
 * An organization owner or manager sees every survey and costs no query. Otherwise it is the nested
 * segment lookups (which `getSegment`'s request cache shares with the evaluation that follows) and one
 * query per batch of ids.
 *
 * @param segmentId - the segment whose filters these are, if saved, so a self-reference is not followed
 */
export const segmentFiltersReadHiddenSurvey = async (
  filters: TBaseFilters,
  workspaceId: string,
  actor: TSurveyActorContext,
  segmentId?: string
): Promise<boolean> => {
  const hiddenSurveyWhere = buildHiddenSurveyWhere(actor);
  if (!hiddenSurveyWhere) return false;
  const reachable = await getReachableSurveyIds(filters, workspaceId, segmentId);
  return anySurveyHiddenFrom([...reachable], workspaceId, hiddenSurveyWhere);
};

export const SEGMENT_FILTERS_READ_HIDDEN_SURVEY_MESSAGE =
  "Segment filters reference a survey you don't have access to";

/**
 * `segmentFiltersReadHiddenSurvey` for a write: a caller may only save, attach or copy survey-interaction
 * filters they could evaluate themselves.
 * @throws {InvalidInputError} When the filters read a survey hidden from the caller
 */
export const assertSegmentSurveyFiltersVisible = async (
  filters: TBaseFilters,
  workspaceId: string,
  actor: TSurveyActorContext,
  segmentId?: string
): Promise<void> => {
  if (await segmentFiltersReadHiddenSurvey(filters, workspaceId, actor, segmentId)) {
    throw new InvalidInputError(SEGMENT_FILTERS_READ_HIDDEN_SURVEY_MESSAGE);
  }
};

/**
 * Every survey id a set of stored segments reads, nested segments followed, in one walk with one visited
 * set: each segment is read at most once however many of them share a nested chain, so the work is
 * bounded by the workspace's segments rather than by references times chain length.
 */
export const getSurveyIdsReadBySegments = async (
  segments: ReadonlyArray<{ id: string; filters: TBaseFilters }>,
  workspaceId: string
): Promise<Set<string>> => {
  const reachable = new Set<string>();
  const visited = new Set(segments.map((segment) => segment.id));
  for (const segment of segments) {
    // Sequential on purpose: the shared visited set is what keeps the walk linear.
    await collectReachableSurveyIds(segment.filters, workspaceId, reachable, visited); // NOSONAR
  }
  return reachable;
};

/** The survey ids `filters` reads that `storedFilters` did not, nested segments followed in both. */
export const getAddedSegmentSurveyIds = async (
  filters: TBaseFilters,
  storedFilters: TBaseFilters,
  workspaceId: string,
  segmentId?: string
): Promise<string[]> => {
  const [next, stored] = await Promise.all([
    getReachableSurveyIds(filters, workspaceId, segmentId),
    getReachableSurveyIds(storedFilters, workspaceId, segmentId),
  ]);
  return [...next].filter((surveyId) => !stored.has(surveyId));
};

/**
 * `assertSegmentSurveyFiltersVisible` for an edit of a tree that is already stored: only the surveys the
 * edit adds — named directly or through a nested segment it adds — must be visible to the caller. A
 * survey the stored tree already read stays, as `restoreHiddenSegmentSurveyIds` keeps it: the caller did
 * not add it, and refusing it would stop them editing anything else in a segment an administrator wrote.
 * @throws {InvalidInputError} When the edit adds a survey hidden from the caller
 */
export const assertAddedSegmentSurveyFiltersVisible = async (
  filters: TBaseFilters,
  storedFilters: TBaseFilters,
  workspaceId: string,
  actor: TSurveyActorContext,
  segmentId?: string
): Promise<void> => {
  const hiddenSurveyWhere = buildHiddenSurveyWhere(actor);
  if (!hiddenSurveyWhere) return;
  const added = await getAddedSegmentSurveyIds(filters, storedFilters, workspaceId, segmentId);
  if (await anySurveyHiddenFrom(added, workspaceId, hiddenSurveyWhere)) {
    throw new InvalidInputError(SEGMENT_FILTERS_READ_HIDDEN_SURVEY_MESSAGE);
  }
};
