import { randomUUID } from "crypto";
import { logger } from "@formbricks/logger";
import {
  type FileStreamResult,
  type StorageError,
  StorageErrorCode,
  deleteFile as deleteFileFromS3,
  deleteFilesByPrefix,
  getFileStream,
  getSignedUploadUrl,
} from "@formbricks/storage";
import { Result, err, ok } from "@formbricks/types/error-handlers";
import { type TAccessType, getMimeTypeForFileName } from "@formbricks/types/storage";
import { sanitizeFileName } from "./utils";

const SAFE_FILE_PATH_SEGMENT = /^[A-Za-z0-9_-]+$/;

/**
 * Rejects relative path segments in a caller-supplied storage file name.
 *
 * Download/delete keys are built as `${id}/${accessType}/${fileName}`, and `fileName` arrives from the
 * `[...filePath]` route param, so `..` segments let a caller step out of their own
 * `workspaceId/accessType/` prefix and name another tenant's object — including a `private/` one via
 * the unauthenticated `public/` route. Object stores treat keys opaquely and SigV4 signs the
 * unnormalized path, so this does not resolve on a stock S3/MinIO backend; but whether one tenant can
 * name another's file must not depend on how the storage backend or any proxy in front of it happens
 * to treat dot segments. `fileName` legitimately contains `/` (upload nests it under
 * `filePathSegments`), so only the segments themselves are constrained.
 */
const hasTraversalSegment = (fileName: string): boolean =>
  fileName.split("/").some((segment) => segment === "." || segment === "..");

/**
 * `hasTraversalSegment` on the name as given and on one more decode of it.
 *
 * The key is built from the name as given: the route hands over the name its router has already decoded
 * once, and objects are stored under that name. The extra decode here never feeds the key. It only catches
 * a `%252e%252e` that arrives as `%2e%2e`, so the traversal guard does not depend on whether a proxy or
 * storage backend decodes paths. Only the `%2e` (dot) and `%2f` (slash) escapes are read, so a bare `%`
 * elsewhere in the name cannot stop the check, as a full `decodeURIComponent` would.
 */
const hasTraversalSegmentOrEncodedForm = (fileName: string): boolean =>
  hasTraversalSegment(fileName) ||
  hasTraversalSegment(fileName.replaceAll(/%2e/gi, ".").replaceAll(/%2f/gi, "/"));

/**
 * Presigns an upload. The object's `Content-Type` is derived from the file's extension and signed into
 * the policy, so the uploader cannot store (and the storage route cannot later echo) a type of its own.
 */
export const getSignedUrlForUpload = async (
  fileName: string,
  workspaceId: string,
  accessType: TAccessType,
  maxFileUploadSize: number = 1024 * 1024 * 10, // 10MB
  filePathSegments: string[] = []
): Promise<
  Result<
    {
      signedUrl: string;
      presignedFields: Record<string, string>;
      fileUrl: string;
    },
    StorageError
  >
> => {
  try {
    const safeFileName = sanitizeFileName(fileName);
    if (!safeFileName) {
      return err({ code: StorageErrorCode.InvalidInput });
    }

    if (filePathSegments.some((segment) => !SAFE_FILE_PATH_SEGMENT.test(segment))) {
      return err({ code: StorageErrorCode.InvalidInput });
    }

    const contentType = getMimeTypeForFileName(safeFileName);
    if (!contentType) {
      return err({ code: StorageErrorCode.InvalidInput });
    }

    const encodedFilePathSegments = filePathSegments.map((segment) => encodeURIComponent(segment));
    const fileNameWithoutExtension = safeFileName.split(".").slice(0, -1).join(".");
    const fileExtension = safeFileName.split(".").pop();

    const updatedFileName = `${fileNameWithoutExtension}--fid--${randomUUID()}.${fileExtension}`;
    const filePath = [workspaceId, accessType, ...filePathSegments].join("/");

    const signedUrlResult = await getSignedUploadUrl(
      updatedFileName,
      contentType,
      filePath,
      maxFileUploadSize
    );

    if (!signedUrlResult.ok) {
      return signedUrlResult;
    }

    // Return relative path - can be resolved to absolute URL at runtime when needed
    return ok({
      signedUrl: signedUrlResult.data.signedUrl,
      presignedFields: signedUrlResult.data.presignedFields,
      fileUrl: `/storage/${workspaceId}/${accessType}/${[
        ...encodedFilePathSegments,
        encodeURIComponent(updatedFileName),
      ].join("/")}`,
    });
  } catch (error) {
    logger.error({ error }, "Error getting signed url for upload");

    return err({
      code: StorageErrorCode.Unknown,
    });
  }
};

/**
 * Get a file stream for downloading/streaming files directly.
 * Use this instead of signed URL redirect for Next.js Image component compatibility.
 *
 * Tries the primary ID path first. If the file is not found and a fallbackId is provided,
 * retries with the fallback path. This supports backwards compatibility: new uploads use
 * workspaceId paths while old files may still live under environmentId paths.
 */
export const getFileStreamForDownload = async (
  fileName: string,
  primaryId: string,
  accessType: TAccessType,
  fallbackId?: string
): Promise<Result<FileStreamResult, StorageError>> => {
  try {
    if (hasTraversalSegmentOrEncodedForm(fileName)) {
      return err({ code: StorageErrorCode.InvalidInput });
    }

    // `fileName` is already decoded (once, by the route's router), which is how the object is stored.
    // Decoding it again would turn a stored `%20` into a space and throw on a bare `%`.
    const primaryKey = `${primaryId}/${accessType}/${fileName}`;

    const streamResult = await getFileStream(primaryKey);

    if (!streamResult.ok && streamResult.error.code === StorageErrorCode.FileNotFoundError && fallbackId) {
      const fallbackKey = `${fallbackId}/${accessType}/${fileName}`;
      return await getFileStream(fallbackKey);
    }

    return streamResult;
  } catch (error) {
    logger.error({ error }, "Error getting file stream for download");

    return err({
      code: StorageErrorCode.Unknown,
    });
  }
};

// Deletes a file from S3. Tries the primary ID path first; if the file is not found and a
// fallbackId is provided, retries with the fallback path (backwards compat for old environmentId paths).
export const deleteFile = async (
  primaryId: string,
  accessType: TAccessType,
  fileName: string,
  fallbackId?: string
) => {
  // Same reasoning as the download path: a `..` segment would let an authorized caller delete objects
  // outside the workspace prefix they were authorized against. An empty segment or a `\` is refused too:
  // a backend or proxy that merges `//` or maps `\` to `/` would turn `surveys//{other}/…` into another
  // survey's key. Real keys never hold either, so refusing only leaves an object behind.
  if (
    hasTraversalSegmentOrEncodedForm(fileName) ||
    fileName.includes("\\") ||
    fileName.split("/").includes("")
  ) {
    return err({ code: StorageErrorCode.InvalidInput });
  }

  const result = await deleteFileFromS3(`${primaryId}/${accessType}/${fileName}`);

  if (!result.ok && result.error.code === StorageErrorCode.FileNotFoundError && fallbackId) {
    return await deleteFileFromS3(`${fallbackId}/${accessType}/${fileName}`);
  }

  return result;
};

// Deletes all files for a workspace — cleans up both workspaceId-prefixed (new uploads) and
// environmentId-prefixed (legacy uploads) paths. Errors are not thrown; callers should check results.
export const deleteFilesByWorkspaceId = async (workspaceId: string, environmentIds: string[]) => {
  const results = await Promise.all([
    deleteFilesByPrefix(workspaceId),
    ...environmentIds.map((envId) => deleteFilesByPrefix(envId)),
  ]);

  // Return the first error if any, otherwise success
  for (const result of results) {
    if (!result.ok) {
      return result;
    }
  }

  return results[0];
};

/**
 * Best-effort storage cleanup for a workspace that has just been deleted.
 *
 * Both callers (workspace deletion and organization deletion) run this *after* the database cascade
 * has committed, so there is nothing left to roll back and no row left pointing at these objects.
 * A storage failure — S3 unconfigured, bucket unreachable, objects already gone — is therefore
 * logged and swallowed: reporting the deletion as failed would be a lie, since the records really
 * are gone. The cost of that choice is orphaned objects when storage is down, which is the same
 * trade-off `deleteWorkspace` has always made.
 */
export const deleteWorkspaceFilesBestEffort = async (workspace: {
  id: string;
  legacyEnvironmentId?: string | null;
}): Promise<void> => {
  // Pre-#8044 uploads are keyed by the environment id the workspace was migrated from, so both
  // prefixes have to go. A null id is dropped here and never reaches deleteFilesByPrefix.
  const legacyPrefixes = [workspace.legacyEnvironmentId].filter((prefix): prefix is string =>
    Boolean(prefix)
  );

  try {
    const result = await deleteFilesByWorkspaceId(workspace.id, legacyPrefixes);

    if (!result.ok) {
      logger.error({ error: result.error, workspaceId: workspace.id }, "Error deleting S3 files");
    }
  } catch (error) {
    logger.error({ error, workspaceId: workspace.id }, "Error deleting S3 files");
  }
};

/**
 * Delete a deleted survey's whole upload folder. Returns whether it worked, so the deletion-cleanup drain
 * can retry a failure; logs it either way.
 *
 * Since #8044 the client upload route keys every response upload as
 * `{workspaceId}/private/surveys/{surveyId}/elements/{elementId}/{file}`, so this one prefix reaches
 * files a scan of `response.data` cannot: answers to an upload element since removed from the survey,
 * and files a respondent uploaded but never submitted. Pre-#8044 uploads sit flat under
 * `{prefix}/private/` with no survey in the key, so for those the response scan remains the only way.
 *
 * Only for a survey that is already deleted. On a live survey (a response reset) the same sweep would
 * also take the upload of a respondent who is mid-survey. The prefix is built from the survey's own
 * row, never from response data, and the trailing slash keeps one survey id from matching another
 * that starts with it. The drain checks the survey is gone before calling this.
 */
export const deleteSurveyUploadFolder = async ({
  workspaceId,
  surveyId,
}: {
  workspaceId: string;
  surveyId: string;
}): Promise<boolean> => {
  try {
    const result = await deleteFilesByPrefix(`${workspaceId}/private/surveys/${surveyId}/`);
    if (!result.ok) {
      logger.error({ error: result.error, workspaceId, surveyId }, "Error deleting a survey's S3 files");
    }
    return result.ok;
  } catch (error) {
    logger.error({ error, workspaceId, surveyId }, "Error deleting a survey's S3 files");
    return false;
  }
};
