import "server-only";
import { z } from "zod";
import { prisma } from "@formbricks/database";
import { Prisma } from "@formbricks/database/prisma";
import { logger } from "@formbricks/logger";
import { ZId } from "@formbricks/types/common";
import { DatabaseError, InvalidInputError } from "@formbricks/types/errors";
import type { TSurveyFilterCriteria } from "@formbricks/types/surveys/types";
import type { TSurveyActorContext } from "@/lib/survey/visibility/actor-context";
import { type TSurveyVisibilityFilter, buildSurveyAccessWhere } from "@/lib/survey/visibility/predicate";
import { zPostgresText } from "@/lib/utils/postgres-text";
import { buildWhereClause } from "@/modules/survey/lib/utils";
import type { TSurvey } from "../types/surveys";
import {
  type TSurveyRow,
  getResponseCountsBySurveyIds,
  mapSurveyRowsToSurveys,
  surveySelect,
} from "./survey-record";

const SURVEY_LIST_CURSOR_VERSION = 1 as const;
const IN_PROGRESS_BUCKET = "inProgress" as const;
const OTHER_BUCKET = "other" as const;

// The cursor is unsigned client input whose strings go straight into the `WHERE` clause, so a NUL byte
// must fail decoding (400), not the query (500) — ENG-3550. Ids are cuid2 like every survey id, which
// rules a NUL out too; the name is free text, hence `zPostgresText`.
const ZDateCursor = z.object({
  version: z.literal(SURVEY_LIST_CURSOR_VERSION),
  sortBy: z.enum(["updatedAt", "createdAt"]),
  value: z.iso.datetime(),
  id: ZId,
});

const ZNameCursor = z.object({
  version: z.literal(SURVEY_LIST_CURSOR_VERSION),
  sortBy: z.literal("name"),
  value: zPostgresText(),
  id: ZId,
});

const ZRelevanceCursor = z.object({
  version: z.literal(SURVEY_LIST_CURSOR_VERSION),
  sortBy: z.literal("relevance"),
  bucket: z.enum([IN_PROGRESS_BUCKET, OTHER_BUCKET]),
  updatedAt: z.iso.datetime(),
  id: ZId,
});

const ZSurveyListPageCursor = z.union([ZDateCursor, ZNameCursor, ZRelevanceCursor]);

export type TSurveyListSort = NonNullable<TSurveyFilterCriteria["sortBy"]>;
export type TSurveyListPageCursor = z.infer<typeof ZSurveyListPageCursor>;
type TStandardSurveyListSort = Exclude<TSurveyListSort, "relevance">;
type TStandardSurveyListCursor = Extract<TSurveyListPageCursor, { sortBy: TStandardSurveyListSort }>;
type TRelevanceSurveyListCursor = Extract<TSurveyListPageCursor, { sortBy: "relevance" }>;
type TRelevanceBucket = typeof IN_PROGRESS_BUCKET | typeof OTHER_BUCKET;

export type TSurveyListPage = {
  surveys: TSurvey[];
  nextCursor: string | null;
};

type TGetSurveyListPageOptions = {
  limit: number;
  cursor: TSurveyListPageCursor | null;
  sortBy: TSurveyListSort;
  filterCriteria?: TSurveyFilterCriteria;
  /** ENG-3282: who is listing. Required, so no list can skip the visibility predicate. */
  actorContext: TSurveyActorContext;
  visibilityFilter?: TSurveyVisibilityFilter;
};

/** The workspace a page is drawn from, plus the visibility clauses every query on it must carry. */
type TSurveyListScope = Readonly<{ accessWhere: Prisma.SurveyWhereInput[]; workspaceId: string }>;

type TCursorDirection = "asc" | "desc";

export function normalizeSurveyListSort(sortBy?: TSurveyFilterCriteria["sortBy"]): TSurveyListSort {
  return sortBy ?? "updatedAt";
}

export function encodeSurveyListPageCursor(cursor: TSurveyListPageCursor): string {
  return Buffer.from(JSON.stringify(cursor), "utf8").toString("base64url");
}

export function decodeSurveyListPageCursor(
  encodedCursor: string,
  sortBy: TSurveyListSort
): TSurveyListPageCursor {
  try {
    const decodedJson = Buffer.from(encodedCursor, "base64url").toString("utf8");
    const parsedCursor = ZSurveyListPageCursor.parse(JSON.parse(decodedJson));

    if (parsedCursor.sortBy !== sortBy) {
      throw new InvalidInputError("The cursor does not match the requested sort order.");
    }

    return parsedCursor;
  } catch (error) {
    if (error instanceof InvalidInputError) {
      throw error;
    }

    throw new InvalidInputError("The cursor is invalid.");
  }
}

function getSurveyOrderBy(sortBy: TStandardSurveyListSort): Prisma.SurveyOrderByWithRelationInput[] {
  switch (sortBy) {
    case "name":
      return [{ name: "asc" }, { id: "asc" }];
    case "createdAt":
      return [{ createdAt: "desc" }, { id: "desc" }];
    case "updatedAt":
    default:
      return [{ updatedAt: "desc" }, { id: "desc" }];
  }
}

function buildDateCursorWhere(
  field: "createdAt" | "updatedAt",
  cursorValue: string,
  cursorId: string,
  direction: TCursorDirection
): Prisma.SurveyWhereInput {
  const comparisonOperator = direction === "desc" ? "lt" : "gt";
  const cursorDate = new Date(cursorValue);

  return {
    OR: [
      { [field]: { [comparisonOperator]: cursorDate } },
      {
        [field]: cursorDate,
        id: { [comparisonOperator]: cursorId },
      },
    ],
  };
}

function buildNameCursorWhere(cursorValue: string, cursorId: string): Prisma.SurveyWhereInput {
  return {
    OR: [
      { name: { gt: cursorValue } },
      {
        name: cursorValue,
        id: { gt: cursorId },
      },
    ],
  };
}

function buildStandardCursorWhere(
  sortBy: TStandardSurveyListSort,
  cursor: TStandardSurveyListCursor
): Prisma.SurveyWhereInput {
  switch (sortBy) {
    case "name":
      return buildNameCursorWhere(cursor.value, cursor.id);
    case "createdAt":
      return buildDateCursorWhere("createdAt", cursor.value, cursor.id, "desc");
    case "updatedAt":
    default:
      return buildDateCursorWhere("updatedAt", cursor.value, cursor.id, "desc");
  }
}

function buildBaseWhere(
  scope: TSurveyListScope,
  filterCriteria?: TSurveyFilterCriteria,
  extraWhere?: Prisma.SurveyWhereInput
): Prisma.SurveyWhereInput {
  // The access clauses go INTO the AND array: spreading a second `AND` over `buildWhereClause`'s
  // would silently replace every filter it holds.
  const { AND: filterClauses } = buildWhereClause(filterCriteria);
  return {
    workspaceId: scope.workspaceId,
    AND: [...(Array.isArray(filterClauses) ? filterClauses : []), ...scope.accessWhere],
    ...extraWhere,
  };
}

function getStandardNextCursor(survey: TSurveyRow, sortBy: TStandardSurveyListSort): TSurveyListPageCursor {
  switch (sortBy) {
    case "name":
      return {
        version: SURVEY_LIST_CURSOR_VERSION,
        sortBy,
        value: survey.name,
        id: survey.id,
      };
    case "createdAt":
      return {
        version: SURVEY_LIST_CURSOR_VERSION,
        sortBy,
        value: survey.createdAt.toISOString(),
        id: survey.id,
      };
    case "updatedAt":
    default:
      return {
        version: SURVEY_LIST_CURSOR_VERSION,
        sortBy,
        value: survey.updatedAt.toISOString(),
        id: survey.id,
      };
  }
}

function getRelevanceNextCursor(survey: TSurveyRow, bucket: TRelevanceBucket): TSurveyListPageCursor {
  return {
    version: SURVEY_LIST_CURSOR_VERSION,
    sortBy: "relevance",
    bucket,
    updatedAt: survey.updatedAt.toISOString(),
    id: survey.id,
  };
}

async function findSurveyRows(
  scope: TSurveyListScope,
  limit: number,
  sortBy: TStandardSurveyListSort,
  filterCriteria?: TSurveyFilterCriteria,
  cursor?: TStandardSurveyListCursor | null,
  extraWhere?: Prisma.SurveyWhereInput
): Promise<TSurveyRow[]> {
  const cursorWhere = cursor ? buildStandardCursorWhere(sortBy, cursor) : undefined;

  return prisma.survey.findMany({
    where: buildBaseWhere(scope, filterCriteria, {
      ...extraWhere,
      ...cursorWhere,
    }),
    select: surveySelect,
    orderBy: getSurveyOrderBy(sortBy),
    take: limit + 1,
  });
}

function getLastSurveyRow(rows: TSurveyRow[]): TSurveyRow | null {
  return rows.at(-1) ?? null;
}

function getPageRows<T>(rows: T[], limit: number): { pageRows: T[]; hasMore: boolean } {
  const hasMore = rows.length > limit;
  return {
    pageRows: hasMore ? rows.slice(0, limit) : rows,
    hasMore,
  };
}

async function buildSurveyListPage(
  rows: TSurveyRow[],
  cursor: TSurveyListPageCursor | null
): Promise<TSurveyListPage> {
  const responseCountsBySurveyId = await getResponseCountsBySurveyIds(rows.map((survey) => survey.id));

  return {
    surveys: mapSurveyRowsToSurveys(rows, responseCountsBySurveyId),
    nextCursor: cursor ? encodeSurveyListPageCursor(cursor) : null,
  };
}

async function getStandardSurveyListPage(
  scope: TSurveyListScope,
  options: TGetSurveyListPageOptions & { sortBy: TStandardSurveyListSort }
): Promise<TSurveyListPage> {
  const surveyRows = await findSurveyRows(
    scope,
    options.limit,
    options.sortBy,
    options.filterCriteria,
    options.cursor as TStandardSurveyListCursor | null
  );

  const { pageRows, hasMore } = getPageRows(surveyRows, options.limit);
  const lastRow = getLastSurveyRow(pageRows);

  return await buildSurveyListPage(
    pageRows,
    hasMore && lastRow ? getStandardNextCursor(lastRow, options.sortBy) : null
  );
}

async function findRelevanceRows(
  scope: TSurveyListScope,
  limit: number,
  filterCriteria: TSurveyFilterCriteria | undefined,
  bucket: TRelevanceBucket,
  cursor: TRelevanceSurveyListCursor | null
): Promise<TSurveyRow[]> {
  const statusWhere: Prisma.SurveyWhereInput =
    bucket === IN_PROGRESS_BUCKET ? { status: "inProgress" } : { status: { not: "inProgress" } };
  const cursorWhere = cursor
    ? buildDateCursorWhere("updatedAt", cursor.updatedAt, cursor.id, "desc")
    : undefined;

  return prisma.survey.findMany({
    where: buildBaseWhere(scope, filterCriteria, {
      ...statusWhere,
      ...cursorWhere,
    }),
    select: surveySelect,
    orderBy: getSurveyOrderBy("updatedAt"),
    take: limit + 1,
  });
}

async function hasMoreRelevanceRowsInOtherBucket(
  scope: TSurveyListScope,
  filterCriteria?: TSurveyFilterCriteria
): Promise<boolean> {
  const otherRows = await findRelevanceRows(scope, 1, filterCriteria, OTHER_BUCKET, null);
  return otherRows.length > 0;
}

function getRelevanceCursor(cursor: TSurveyListPageCursor | null): TRelevanceSurveyListCursor | null {
  if (cursor && cursor.sortBy !== "relevance") {
    throw new InvalidInputError("The cursor does not match the requested sort order.");
  }

  return cursor;
}

function getRelevanceBucketCursor(
  cursor: TRelevanceSurveyListCursor | null,
  bucket: TRelevanceBucket
): TRelevanceSurveyListCursor | null {
  return cursor?.bucket === bucket ? cursor : null;
}

function shouldReadInProgressBucket(cursor: TRelevanceSurveyListCursor | null): boolean {
  return !cursor || cursor.bucket === IN_PROGRESS_BUCKET;
}

async function buildRelevancePage(
  rows: TSurveyRow[],
  bucket: TRelevanceBucket | null
): Promise<TSurveyListPage> {
  const lastRow = getLastSurveyRow(rows);

  return await buildSurveyListPage(rows, bucket && lastRow ? getRelevanceNextCursor(lastRow, bucket) : null);
}

async function getInProgressRelevanceStep(
  scope: TSurveyListScope,
  limit: number,
  filterCriteria: TSurveyFilterCriteria | undefined,
  cursor: TRelevanceSurveyListCursor | null
): Promise<{ pageRows: TSurveyRow[]; remaining: number; response: TSurveyListPage | null }> {
  const inProgressRows = await findRelevanceRows(
    scope,
    limit,
    filterCriteria,
    IN_PROGRESS_BUCKET,
    getRelevanceBucketCursor(cursor, IN_PROGRESS_BUCKET)
  );
  const { pageRows, hasMore } = getPageRows(inProgressRows, limit);

  return {
    pageRows,
    remaining: limit - pageRows.length,
    response: hasMore ? await buildRelevancePage(pageRows, IN_PROGRESS_BUCKET) : null,
  };
}

async function buildInProgressOnlyRelevancePage(
  scope: TSurveyListScope,
  rows: TSurveyRow[],
  filterCriteria: TSurveyFilterCriteria | undefined,
  cursor: TRelevanceSurveyListCursor | null
): Promise<TSurveyListPage> {
  const hasOtherRows =
    rows.length > 0 &&
    shouldReadInProgressBucket(cursor) &&
    (await hasMoreRelevanceRowsInOtherBucket(scope, filterCriteria));

  return await buildRelevancePage(rows, hasOtherRows ? IN_PROGRESS_BUCKET : null);
}

async function getRelevanceSurveyListPage(
  scope: TSurveyListScope,
  options: TGetSurveyListPageOptions & { sortBy: "relevance" }
): Promise<TSurveyListPage> {
  const relevanceCursor = getRelevanceCursor(options.cursor);
  const pageRows: TSurveyRow[] = [];
  let remaining = options.limit;

  if (shouldReadInProgressBucket(relevanceCursor)) {
    const inProgressStep = await getInProgressRelevanceStep(
      scope,
      remaining,
      options.filterCriteria,
      relevanceCursor
    );
    pageRows.push(...inProgressStep.pageRows);

    if (inProgressStep.response) {
      return inProgressStep.response;
    }

    remaining = inProgressStep.remaining;
  }

  if (remaining <= 0) {
    return await buildInProgressOnlyRelevancePage(scope, pageRows, options.filterCriteria, relevanceCursor);
  }

  const otherRows = await findRelevanceRows(
    scope,
    remaining,
    options.filterCriteria,
    OTHER_BUCKET,
    getRelevanceBucketCursor(relevanceCursor, OTHER_BUCKET)
  );
  const { pageRows: otherPageRows, hasMore: hasMoreOther } = getPageRows(otherRows, remaining);
  pageRows.push(...otherPageRows);

  return await buildRelevancePage(pageRows, hasMoreOther ? OTHER_BUCKET : null);
}

export async function getSurveyListPage(
  workspaceId: string,
  options: TGetSurveyListPageOptions
): Promise<TSurveyListPage> {
  const scope: TSurveyListScope = {
    accessWhere: buildSurveyAccessWhere(options.actorContext, options.visibilityFilter),
    workspaceId,
  };
  try {
    if (options.sortBy === "relevance") {
      return await getRelevanceSurveyListPage(scope, {
        ...options,
        sortBy: "relevance",
      });
    }

    return await getStandardSurveyListPage(scope, {
      ...options,
      sortBy: options.sortBy,
    });
  } catch (error) {
    if (error instanceof InvalidInputError) {
      throw error;
    }

    if (error instanceof Prisma.PrismaClientKnownRequestError) {
      logger.error(error, "Error getting paginated surveys");
      throw new DatabaseError(error.message);
    }

    throw error;
  }
}
