import { beforeEach, describe, expect, test, vi } from "vitest";
import { StorageErrorCode } from "@formbricks/storage";
import type { TSurvey } from "@formbricks/types/surveys/types";
import type { TAttachmentEntry } from "@/modules/storage/lib/collect-response-attachments";
import { getFileStreamForDownload } from "@/modules/storage/service";
import { MAX_ATTACHMENT_BYTES } from "./constants";
import {
  buildAttachmentArchiveFileName,
  createAttachmentArchive,
  streamAttachmentsAsZip,
  writeAttachmentArchive,
} from "./export-attachments";
import { buildAttachmentManifestCsv } from "./manifest";

vi.mock("server-only", () => ({}));

vi.mock("@formbricks/logger", () => ({
  logger: { warn: vi.fn(), error: vi.fn() },
}));

vi.mock("@/modules/storage/service", () => ({
  getFileStreamForDownload: vi.fn(),
}));

// Mocked so these tests can assert the manifest *rows* the streaming stage produces. Asserting the
// rendered CSV through the archive's bytes would only work while entries are stored uncompressed;
// manifest.test.ts covers the rendering itself.
vi.mock("./manifest", () => ({
  buildAttachmentManifestCsv: vi.fn(() => Promise.resolve("manifest-csv")),
}));

const mockedGetFileStream = vi.mocked(getFileStreamForDownload);
const mockedBuildManifest = vi.mocked(buildAttachmentManifestCsv);

/** The rows handed to the manifest builder — statuses and sizes, independent of ZIP encoding. */
const manifestRows = () => mockedBuildManifest.mock.calls.at(-1)?.[0] ?? [];

const survey = { id: "survey-1", workspaceId: "ws-own" } as unknown as TSurvey;
const NOW = new Date("2026-09-01T12:00:00.000Z");

const okEntry = (overrides: Partial<TAttachmentEntry> = {}): TAttachmentEntry => ({
  zipPath: "2026-09-01T10-00-00_res-1/2_Upload a photo/photo.jpg",
  responseId: "res-1",
  responseCreatedAt: new Date("2026-09-01T10:00:00.000Z"),
  elementId: "el-upload",
  elementLabel: "Upload a photo",
  originalFileName: "photo.jpg",
  status: "ok",
  storage: { storageId: "ws-own", accessType: "private", fileName: "photo.jpg" },
  ...overrides,
});

const streamOf = (content: string) => ({
  ok: true as const,
  data: {
    body: new ReadableStream<Uint8Array>({
      start(controller) {
        controller.enqueue(new TextEncoder().encode(content));
        controller.close();
      },
    }),
    contentType: "image/jpeg",
    contentLength: content.length,
  },
});

/**
 * Reads the whole archive so entry *names* can be asserted. Names sit uncompressed in each local file
 * header and in the central directory whatever the compression level, so these assertions survive a
 * change from stored to deflated entries. Anything about an entry's contents is asserted through the
 * mocked manifest rows instead.
 */
const readArchive = async (response: Response): Promise<string> =>
  Buffer.from(await response.arrayBuffer()).toString("binary");

describe("buildAttachmentArchiveFileName", () => {
  test("names the archive after the survey and the UTC date", () => {
    expect(buildAttachmentArchiveFileName(survey, NOW)).toBe("survey-1-attachments-2026-09-01.zip");
  });
});

describe("writeAttachmentArchive", () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  /** Writes the archive while draining it, as the upload does, and returns the writer's summary. */
  const writeAndDrain = async (entries: TAttachmentEntry[]) => {
    const { archive, activeSource } = createAttachmentArchive();
    const chunks: Buffer[] = [];
    archive.on("data", (chunk: Buffer) => chunks.push(chunk));
    // Listened for before writing: the archive can end before `finalize()` resolves.
    const ended = new Promise((resolve) => archive.once("end", resolve));
    const summary = await writeAttachmentArchive(entries, archive, activeSource);
    await ended;
    return { summary, size: archive.pointer(), bytes: Buffer.concat(chunks) };
  };

  test("reports what the archive holds", async () => {
    mockedGetFileStream.mockResolvedValue(streamOf("photo-bytes"));

    const { summary, size, bytes } = await writeAndDrain([
      okEntry(),
      okEntry({ status: "skipped_foreign_workspace", storage: undefined, responseId: "res-2" }),
    ]);

    expect(summary).toEqual({ appendedFiles: 1, attachmentBytes: "photo-bytes".length, truncated: false });
    // The archive's own size, which is what the export records and the user downloads.
    expect(size).toBe(bytes.length);
  });

  test("flags a truncated archive", async () => {
    mockedGetFileStream.mockResolvedValue({
      ok: true,
      data: {
        body: streamOf("x").data.body,
        contentType: "image/jpeg",
        contentLength: MAX_ATTACHMENT_BYTES + 1,
      },
    } as never);

    const { summary } = await writeAndDrain([okEntry()]);

    expect(summary).toEqual({ appendedFiles: 0, attachmentBytes: 0, truncated: true });
  });
});

describe("streamAttachmentsAsZip", () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  test("sets the streaming download headers", async () => {
    mockedGetFileStream.mockResolvedValue(streamOf("x"));

    const response = streamAttachmentsAsZip({ entries: [okEntry()], survey, now: NOW });

    expect(response.status).toBe(200);
    expect(response.headers.get("Content-Type")).toBe("application/zip");
    expect(response.headers.get("Content-Disposition")).toBe(
      `attachment; filename="survey-1-attachments-2026-09-01.zip"; filename*=UTF-8''survey-1-attachments-2026-09-01.zip`
    );
    expect(response.headers.get("Cache-Control")).toBe("no-store");

    // Drained even though this test asserts nothing about the bytes. `streamAttachmentsAsZip` starts
    // its pipeline when it is called, not when the body is read, so an unconsumed response keeps
    // running into the next test and appends a second `buildAttachmentManifestCsv` call there — which
    // reads `.at(-1)`, and so sees whichever call happened to land last. This is the only call site in
    // the file that does not already go through `readArchive`.
    await readArchive(response);
  });

  test("writes each attachment at its zip path and appends a manifest", async () => {
    mockedGetFileStream.mockResolvedValue(streamOf("photo-bytes"));

    const archive = await readArchive(streamAttachmentsAsZip({ entries: [okEntry()], survey, now: NOW }));

    expect(archive).toContain("2026-09-01T10-00-00_res-1/2_Upload a photo/photo.jpg");
    expect(archive).toContain("manifest.csv");
    // The manifest row carries the response link and the size storage reported.
    expect(manifestRows()).toEqual([
      expect.objectContaining({ responseId: "res-1", status: "ok", bytes: "photo-bytes".length }),
    ]);
  });

  test("asks storage for the decoded file name under the URL's storage id", async () => {
    mockedGetFileStream.mockResolvedValue(streamOf("x"));

    await readArchive(
      streamAttachmentsAsZip({
        entries: [
          okEntry({ storage: { storageId: "ws-own", accessType: "private", fileName: "my photo.jpg" } }),
        ],
        survey,
        now: NOW,
      })
    );

    expect(mockedGetFileStream).toHaveBeenCalledWith("my photo.jpg", "ws-own", "private", "ws-own");
  });

  test("records a file storage confirms is gone as missing", async () => {
    // The 200 and its headers are already flushed by the time storage is asked, so a missing object
    // cannot become an error status — it has to surface in the manifest.
    mockedGetFileStream.mockResolvedValue({
      ok: false,
      error: { code: StorageErrorCode.FileNotFoundError },
    } as never);

    const archive = await readArchive(streamAttachmentsAsZip({ entries: [okEntry()], survey, now: NOW }));

    expect(archive).toContain("manifest.csv");
    expect(manifestRows()).toEqual([expect.objectContaining({ status: "missing_in_storage" })]);
  });

  test("does not call an outage a missing file", async () => {
    // Saying "missing" for a credentials or client error would have the manifest assert that intact
    // attachments were lost.
    mockedGetFileStream.mockResolvedValue({
      ok: false,
      error: { code: StorageErrorCode.S3CredentialsError },
    } as never);

    await readArchive(streamAttachmentsAsZip({ entries: [okEntry()], survey, now: NOW }));

    expect(manifestRows()).toEqual([expect.objectContaining({ status: "unavailable_in_storage" })]);
  });

  test("carries the collector's skipped entries into the manifest without asking storage", async () => {
    // Drained rather than inspected: the point is that nothing was fetched and the row still lands.
    await readArchive(
      streamAttachmentsAsZip({
        entries: [okEntry({ status: "skipped_foreign_workspace", zipPath: "", storage: undefined })],
        survey,
        now: NOW,
      })
    );

    expect(mockedGetFileStream).not.toHaveBeenCalled();
    expect(manifestRows()).toEqual([expect.objectContaining({ status: "skipped_foreign_workspace" })]);
  });

  test("truncates before appending an object that would cross the byte ceiling", async () => {
    // Two half-cap objects: the first fits, the second must be refused rather than appended and only
    // then noticed, which would have produced an archive twice the limit.
    const half = Math.floor(MAX_ATTACHMENT_BYTES / 2) + 1;
    mockedGetFileStream.mockResolvedValue({
      ok: true,
      data: { body: streamOf("x").data.body, contentType: "image/jpeg", contentLength: half },
    } as never);

    const archive = await readArchive(
      streamAttachmentsAsZip({
        entries: [
          okEntry(),
          okEntry({ zipPath: "2026-09-01T10-00-00_res-2/2_Upload a photo/photo.jpg", responseId: "res-2" }),
        ],
        survey,
        now: NOW,
      })
    );

    expect(archive).toContain("_TRUNCATED.txt");
    // The first was appended; the second was fetched, refused, and never written.
    expect(archive).toContain("2026-09-01T10-00-00_res-1/");
    expect(archive).not.toContain("2026-09-01T10-00-00_res-2/");
    // The dropped file still gets a row: it is the only thing naming what is missing, since
    // _TRUNCATED.txt carries a count alone.
    expect(manifestRows()).toEqual([
      expect.objectContaining({ responseId: "res-1", status: "ok" }),
      expect.objectContaining({ responseId: "res-2", status: "skipped_export_truncated" }),
    ]);
  });

  test("keeps a collector rejection's own reason when truncation drops it too", async () => {
    // The entry after the ceiling was already refused by the collector. Relabelling it
    // `skipped_export_truncated` would tell the reader the export ran out of room, hiding that the file
    // sits in another workspace and would never have been exported at any size.
    mockedGetFileStream.mockResolvedValue({
      ok: true,
      data: {
        body: streamOf("x").data.body,
        contentType: "image/jpeg",
        contentLength: MAX_ATTACHMENT_BYTES + 1,
      },
    } as never);

    await readArchive(
      streamAttachmentsAsZip({
        entries: [
          okEntry(),
          okEntry({ responseId: "res-2", status: "skipped_foreign_workspace", storage: undefined }),
        ],
        survey,
        now: NOW,
      })
    );

    expect(manifestRows()).toEqual([
      expect.objectContaining({ responseId: "res-1", status: "skipped_export_truncated" }),
      expect.objectContaining({ responseId: "res-2", status: "skipped_foreign_workspace" }),
    ]);
  });

  test("cancels the storage body it refuses, instead of leaving the connection open", async () => {
    const cancel = vi.fn().mockResolvedValue(undefined);
    const body = { cancel } as unknown as ReadableStream<Uint8Array>;
    mockedGetFileStream.mockResolvedValue({
      ok: true,
      data: { body, contentType: "image/jpeg", contentLength: MAX_ATTACHMENT_BYTES + 1 },
    } as never);

    await readArchive(streamAttachmentsAsZip({ entries: [okEntry()], survey, now: NOW }));

    expect(cancel).toHaveBeenCalledTimes(1);
  });

  test("refuses a single object larger than the whole budget", async () => {
    mockedGetFileStream.mockResolvedValue({
      ok: true,
      data: {
        body: streamOf("x").data.body,
        contentType: "image/jpeg",
        contentLength: MAX_ATTACHMENT_BYTES + 1,
      },
    } as never);

    const archive = await readArchive(streamAttachmentsAsZip({ entries: [okEntry()], survey, now: NOW }));

    expect(archive).toContain("_TRUNCATED.txt");
    expect(archive).not.toContain("2026-09-01T10-00-00_res-1/2_Upload a photo/photo.jpg");
  });

  test("still produces a manifest when there is nothing to write", async () => {
    const archive = await readArchive(streamAttachmentsAsZip({ entries: [], survey, now: NOW }));

    expect(archive).toContain("manifest.csv");
    expect(mockedGetFileStream).not.toHaveBeenCalled();
  });
});
