import {
  DeleteObjectCommand,
  DeleteObjectsCommand,
  type DeleteObjectsCommandOutput,
  GetObjectCommand,
  HeadObjectCommand,
  paginateListObjectsV2,
} from "@aws-sdk/client-s3";
import { Upload } from "@aws-sdk/lib-storage";
import {
  type PresignedPost,
  type PresignedPostOptions,
  createPresignedPost,
} from "@aws-sdk/s3-presigned-post";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
import type { Readable } from "node:stream";
import { logger } from "@formbricks/logger";
import { createS3Client } from "./client";
import { S3_BUCKET_NAME } from "./constants";
import { countErrorCodes, toLoggableStorageError } from "./loggable-error";
import { type Result, type StorageError, StorageErrorCode, err, ok } from "./types/error";

/**
 * Get a signed URL for uploading a file to S3
 * @param fileName - The name of the file to upload
 * @param contentType - The content type of the file
 * @param filePath - The path to the file in S3
 * @param maxSize - The maximum size of the file to upload or undefined if no limit is desired
 * @returns A Result containing the signed URL and presigned fields or an error: StorageError
 */
export const getSignedUploadUrl = async (
  fileName: string,
  contentType: string,
  filePath: string,
  maxSize: number = 1024 * 1024 * 10 // 10MB
): Promise<
  Result<
    {
      signedUrl: string;
      presignedFields: PresignedPost["fields"];
    },
    StorageError
  >
> => {
  try {
    const s3Client = createS3Client();

    if (!s3Client) {
      logger.error("Failed to get signed upload URL: S3 client is not set");
      return err({
        code: StorageErrorCode.S3ClientError,
      });
    }

    const postConditions: PresignedPostOptions["Conditions"] = maxSize
      ? [["content-length-range", 0, maxSize]]
      : undefined;

    if (!S3_BUCKET_NAME) {
      logger.error("Failed to get signed upload URL: S3 bucket name is not set");
      return err({
        code: StorageErrorCode.S3CredentialsError,
      });
    }

    const { fields, url } = await createPresignedPost(s3Client, {
      Expires: 2 * 60, // 2 minutes
      Bucket: S3_BUCKET_NAME,
      Key: `${filePath}/${fileName}`,
      Fields: {
        "Content-Type": contentType,
        success_action_status: "201",
      },
      Conditions: postConditions,
    });

    return ok({
      signedUrl: url,
      presignedFields: fields,
    });
  } catch (error) {
    logger.error({ error: toLoggableStorageError(error) }, "Failed to get signed upload URL");

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

export interface SignedDownloadUrlOptions {
  /** Seconds the URL stays valid. Defaults to 30 minutes. */
  expiresIn?: number;
  /** Signed into the URL as `response-content-disposition`, so the browser saves under this name. */
  responseContentDisposition?: string;
}

/**
 * Get a signed URL for a file in S3
 * @param fileKey - The key of the file in S3
 * @param options - Expiry and response headers to sign into the URL
 * @returns A Result containing the signed URL or an error: StorageError
 */
export const getSignedDownloadUrl = async (
  fileKey: string,
  { expiresIn = 60 * 30, responseContentDisposition }: SignedDownloadUrlOptions = {}
): Promise<Result<string, StorageError>> => {
  try {
    const s3Client = createS3Client();

    if (!s3Client) {
      return err({
        code: StorageErrorCode.S3ClientError,
      });
    }

    if (!S3_BUCKET_NAME) {
      return err({
        code: StorageErrorCode.S3CredentialsError,
      });
    }

    // Check if file exists before generating signed URL
    const headObjectCommand = new HeadObjectCommand({
      Bucket: S3_BUCKET_NAME,
      Key: fileKey,
    });

    try {
      await s3Client.send(headObjectCommand);
    } catch (error: unknown) {
      logger.error({ error: toLoggableStorageError(error) }, "Failed to check if file exists");
      if (
        (error as Error).name === "NotFound" ||
        (error as { $metadata?: { httpStatusCode?: number } }).$metadata?.httpStatusCode === 404
      ) {
        return err({
          code: StorageErrorCode.FileNotFoundError,
        });
      }

      logger.warn(
        { error: toLoggableStorageError(error) },
        "HeadObject check failed; proceeding to sign download URL"
      );
    }

    const getObjectCommand = new GetObjectCommand({
      Bucket: S3_BUCKET_NAME,
      Key: fileKey,
      ResponseContentDisposition: responseContentDisposition,
    });

    return ok(await getSignedUrl(s3Client, getObjectCommand, { expiresIn }));
  } catch (error) {
    logger.error({ error: toLoggableStorageError(error) }, "Failed to get signed download URL");
    return err({
      code: StorageErrorCode.Unknown,
    });
  }
};

export interface FileStreamResult {
  body: ReadableStream<Uint8Array>;
  contentType: string;
  contentLength: number;
}

/**
 * Get a file stream from S3
 * Use this for streaming files directly to clients instead of redirecting to signed URLs
 * @param fileKey - The key of the file in S3
 * @returns A Result containing the file stream and metadata or an error: StorageError
 */
export const getFileStream = async (fileKey: string): Promise<Result<FileStreamResult, StorageError>> => {
  try {
    const s3Client = createS3Client();

    if (!s3Client) {
      return err({
        code: StorageErrorCode.S3ClientError,
      });
    }

    if (!S3_BUCKET_NAME) {
      return err({
        code: StorageErrorCode.S3CredentialsError,
      });
    }

    const getObjectCommand = new GetObjectCommand({
      Bucket: S3_BUCKET_NAME,
      Key: fileKey,
    });

    const response = await s3Client.send(getObjectCommand);

    if (!response.Body) {
      return err({
        code: StorageErrorCode.FileNotFoundError,
      });
    }

    // Convert the SDK stream to a web ReadableStream
    const webStream = response.Body.transformToWebStream();

    return ok({
      body: webStream,
      contentType: response.ContentType ?? "application/octet-stream",
      contentLength: response.ContentLength ?? 0,
    });
  } catch (error) {
    const errName = (error as { name?: string }).name;
    const httpStatusCode = (error as { $metadata?: { httpStatusCode?: number } }).$metadata?.httpStatusCode;
    if (errName === "NoSuchKey" || errName === "NotFound" || httpStatusCode === 404) {
      return err({
        code: StorageErrorCode.FileNotFoundError,
      });
    }
    logger.error({ error: toLoggableStorageError(error) }, "Failed to get file stream");
    return err({
      code: StorageErrorCode.Unknown,
    });
  }
};

/**
 * Multipart part size for {@link uploadFileStream}. Memory held per upload is roughly part size times
 * queue size, so a multi-GB stream costs ~32 MB rather than its own length.
 */
export const UPLOAD_PART_SIZE_BYTES = 16 * 1024 * 1024;
const UPLOAD_QUEUE_SIZE = 2;

export interface UploadFileStreamOptions {
  contentType: string;
  /** Stored on the object, so every download carries it even where a signed override is ignored. */
  contentDisposition?: string;
  /** Aborting cancels the upload and its multipart upload, so no parts are left billed in the bucket. */
  signal?: AbortSignal;
}

/**
 * Upload a stream of unknown length to S3 as a multipart upload.
 *
 * Server-side counterpart to the presigned POST: for objects the server produces itself, such as an
 * export archive, which can be larger than any single PUT allows.
 * @param fileKey - The key of the file in S3
 * @param body - The stream to upload; consumed exactly once
 * @param options - Object metadata and an abort signal
 * @returns A Result containing void or an error: StorageError
 */
export const uploadFileStream = async (
  fileKey: string,
  body: Readable,
  { contentType, contentDisposition, signal }: UploadFileStreamOptions
): Promise<Result<void, StorageError>> => {
  const s3Client = createS3Client();

  if (!s3Client) {
    return err({
      code: StorageErrorCode.S3ClientError,
    });
  }

  if (!S3_BUCKET_NAME) {
    return err({
      code: StorageErrorCode.S3CredentialsError,
    });
  }

  if (signal?.aborted) {
    body.destroy();
    return err({ code: StorageErrorCode.Unknown });
  }

  const upload = new Upload({
    client: s3Client,
    params: {
      Bucket: S3_BUCKET_NAME,
      Key: fileKey,
      Body: body,
      ContentType: contentType,
      ContentDisposition: contentDisposition,
      CacheControl: "no-store",
    },
    partSize: UPLOAD_PART_SIZE_BYTES,
    queueSize: UPLOAD_QUEUE_SIZE,
    // Aborts the multipart upload on failure; otherwise uploaded parts linger, invisible and billed.
    leavePartsOnError: false,
  });

  const onAbort = () => {
    void upload.abort().catch((error: unknown) => {
      logger.warn({ error: toLoggableStorageError(error) }, "Failed to abort upload");
    });
  };
  signal?.addEventListener("abort", onAbort, { once: true });

  try {
    await upload.done();
    return ok(undefined);
  } catch (error) {
    body.destroy();
    if (signal?.aborted) {
      logger.warn("Upload aborted");
    } else {
      logger.error({ error: toLoggableStorageError(error) }, "Failed to upload file stream");
    }
    return err({
      code: StorageErrorCode.Unknown,
    });
  } finally {
    signal?.removeEventListener("abort", onAbort);
  }
};

/**
 * Delete a file from S3
 * @param fileKey - The key of the file in S3 (e.g. "surveys/123/responses/456/file.pdf")
 * @returns A Result containing the void or an error: StorageError
 */
export const deleteFile = async (fileKey: string): Promise<Result<void, StorageError>> => {
  try {
    const s3Client = createS3Client();

    if (!s3Client) {
      return err({
        code: StorageErrorCode.S3ClientError,
      });
    }

    if (!S3_BUCKET_NAME) {
      return err({
        code: StorageErrorCode.S3CredentialsError,
      });
    }

    const deleteObjectCommand = new DeleteObjectCommand({
      Bucket: S3_BUCKET_NAME,
      Key: fileKey,
    });

    await s3Client.send(deleteObjectCommand);

    return ok(undefined);
  } catch (error) {
    logger.error({ error: toLoggableStorageError(error) }, "Failed to delete file");

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

/**
 * Delete all files by prefix
 * @param prefix - The prefix of the files to delete
 * @returns A Result containing the void or an error: StorageError
 */
export const deleteFilesByPrefix = async (prefix: string): Promise<Result<void, StorageError>> => {
  try {
    const s3Client = createS3Client();

    if (!s3Client) {
      return err({
        code: StorageErrorCode.S3ClientError,
      });
    }

    if (!S3_BUCKET_NAME) {
      return err({
        code: StorageErrorCode.S3CredentialsError,
      });
    }

    const normalizedPrefix = prefix.trim();
    if (!normalizedPrefix || normalizedPrefix === "/") {
      logger.error({ prefix }, "Refusing to delete files with an empty or root prefix");
      return err({
        code: StorageErrorCode.InvalidInput,
      });
    }

    const keys: { Key: string }[] = [];

    const paginator = paginateListObjectsV2(
      { client: s3Client },
      {
        Bucket: S3_BUCKET_NAME,
        Prefix: normalizedPrefix,
      }
    );

    for await (const page of paginator) {
      const pageKeys = page.Contents?.flatMap((obj) => (obj.Key ? [{ Key: obj.Key }] : [])) ?? [];
      keys.push(...pageKeys);
    }

    if (keys.length === 0) {
      return ok(undefined);
    }

    const deletionPromises: Promise<DeleteObjectsCommandOutput>[] = [];

    for (let i = 0; i < keys.length; i += 1000) {
      const batch = keys.slice(i, i + 1000);

      const deleteObjectsCommand = new DeleteObjectsCommand({
        Bucket: S3_BUCKET_NAME,
        Delete: {
          Objects: batch,
        },
      });

      deletionPromises.push(s3Client.send(deleteObjectsCommand));
    }

    const results = await Promise.all(deletionPromises);

    // Check for partial failures and log them
    let totalErrors = 0;
    let totalDeleted = 0;

    for (const result of results) {
      if (result.Deleted) {
        totalDeleted += result.Deleted.length;
        logger.debug({ count: result.Deleted.length }, "Successfully deleted objects in batch");
      }

      if (result.Errors && result.Errors.length > 0) {
        totalErrors += result.Errors.length;
        logger.error({ errorCodes: countErrorCodes(result.Errors) }, "Some objects failed to delete");
      }
    }

    // Log the issues
    if (totalErrors > 0) {
      logger.warn({ totalErrors, totalDeleted }, "Bulk delete completed with some failures");
    }

    return ok(undefined);
  } catch (error) {
    logger.error({ error: toLoggableStorageError(error) }, "Failed to delete files by prefix");

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