import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import { AuthenticationError, OperationNotAllowedError, UnknownError } from "@formbricks/types/errors";
import { TIntegrationGoogleSheets } from "@formbricks/types/integration/google-sheet";
import {
  GOOGLE_SHEET_INTEGRATION_INSUFFICIENT_PERMISSION,
  GOOGLE_SHEET_INTEGRATION_INSUFFICIENT_SCOPES,
  GOOGLE_SHEET_INTEGRATION_INVALID_GRANT,
} from "@/lib/googleSheet/constants";

vi.mock("@/lib/integration/service", () => ({ createOrUpdateIntegration: vi.fn() }));

vi.mock("@/lib/constants", () => ({
  GOOGLE_SHEETS_CLIENT_ID: "client-id",
  GOOGLE_SHEETS_CLIENT_SECRET: "client-secret",
  GOOGLE_SHEETS_REDIRECT_URL: "https://example.com/callback",
  GOOGLE_SHEET_MESSAGE_LIMIT: 50000,
}));

const sheetsMock = vi.hoisted(() => ({
  update: vi.fn(),
  append: vi.fn(),
}));

const spreadsheetsGetMock = vi.hoisted(() => vi.fn());

vi.mock("googleapis", () => ({
  google: {
    auth: {
      OAuth2: class {
        setCredentials() {}
      },
    },
    sheets: () => ({ spreadsheets: { values: sheetsMock, get: spreadsheetsGetMock } }),
  },
}));

const { getSpreadsheetNameById, writeData } = await import("@/lib/googleSheet/service");

/**
 * The shape the integration has once ENG-2078's redaction has blanked `config.key`: schema-valid,
 * because `ZGoogleCredential` types both tokens as `z.string()`, but unusable for auth.
 */
const redactedIntegration = {
  id: "integration1",
  type: "googleSheets",
  workspaceId: "ws1",
  config: {
    email: "owner@example.com",
    data: [],
    key: {
      scope: "https://www.googleapis.com/auth/spreadsheets",
      token_type: "Bearer",
      expiry_date: 0,
      access_token: "",
      refresh_token: "",
    },
  },
} as TIntegrationGoogleSheets;

/** An integration whose stored access token is still valid, so `authorize` never has to refresh. */
const authorizedIntegration = {
  id: "integration1",
  type: "googleSheets",
  workspaceId: "ws1",
  config: {
    email: "owner@example.com",
    data: [],
    key: {
      scope: "https://www.googleapis.com/auth/spreadsheets",
      token_type: "Bearer",
      expiry_date: Date.now() + 60 * 60 * 1000,
      access_token: "access-token",
      refresh_token: "refresh-token",
    },
  },
} as TIntegrationGoogleSheets;

// ENG-2303: a blank refresh token reached googleapis, which surfaced it as a bare "No refresh token is
// set." straight into a user-facing toast. It has to fail as the reconnect case instead, which the modal
// maps to a real message. Nothing here touches the network: the guard short-circuits before any Google
// call, which is also what keeps this test hermetic.
describe("getSpreadsheetNameById", () => {
  test("throws an invalid_grant AuthenticationError when the stored refresh token is blank", async () => {
    await expect(getSpreadsheetNameById(redactedIntegration, "sheet1")).rejects.toThrow(
      new AuthenticationError(GOOGLE_SHEET_INTEGRATION_INVALID_GRANT)
    );
  });

  // ENG-2807: Google's scope rejection never says "permission", so it slipped past the permission guard
  // and reached the user as a raw English Google string. It needs its own code, because the fix is to
  // reconnect and grant the Sheets scope, not to share the spreadsheet.
  describe("when Google rejects the spreadsheet fetch", () => {
    const rejectWith = (message: string) => {
      spreadsheetsGetMock.mockImplementation((_params, callback: (err: Error | null) => void) => {
        setTimeout(() => callback(new Error(message)), 0);
      });
    };

    beforeEach(() => {
      spreadsheetsGetMock.mockReset();
      vi.stubGlobal("fetch", vi.fn().mockResolvedValue({ ok: true }));
    });

    afterEach(() => {
      vi.unstubAllGlobals();
    });

    test("maps a missing OAuth scope to the insufficient_scopes code", async () => {
      rejectWith("Request had insufficient authentication scopes.");

      await expect(getSpreadsheetNameById(authorizedIntegration, "sheet1")).rejects.toThrow(
        new OperationNotAllowedError(GOOGLE_SHEET_INTEGRATION_INSUFFICIENT_SCOPES)
      );
    });

    test("keeps mapping a sharing problem to the insufficient_permission code", async () => {
      rejectWith("The caller does not have permission");

      await expect(getSpreadsheetNameById(authorizedIntegration, "sheet1")).rejects.toThrow(
        new OperationNotAllowedError(GOOGLE_SHEET_INTEGRATION_INSUFFICIENT_PERMISSION)
      );
    });

    test("wraps any other Google error in an UnknownError", async () => {
      rejectWith("Requested entity was not found.");

      await expect(getSpreadsheetNameById(authorizedIntegration, "sheet1")).rejects.toThrow(UnknownError);
    });
  });
});

// ENG-2250: both writes used to `throw` from inside the node-style googleapis callback, so the error
// escaped `writeData`'s own `try/catch` and surfaced as an unhandled rejection while `writeData` itself
// resolved — the pipeline recorded a success and the customer's sheet quietly stopped filling up.
describe("writeData", () => {
  /**
   * googleapis never calls back on the same tick, and that is the whole bug: a synchronous callback
   * would carry the old `throw` back into `writeData`'s `try/catch` and hide it. Every mock here defers
   * like the real client does.
   */
  const respondAsync =
    (err: Error | null, onCall?: () => void) => (_params: unknown, callback: (err: Error | null) => void) => {
      setTimeout(() => {
        onCall?.();
        callback(err);
      }, 0);
    };

  beforeEach(() => {
    sheetsMock.update.mockReset();
    sheetsMock.append.mockReset();
    vi.stubGlobal("fetch", vi.fn().mockResolvedValue({ ok: true }));
  });

  afterEach(() => {
    vi.unstubAllGlobals();
  });

  test("rejects with an UnknownError when the append callback reports an error", async () => {
    sheetsMock.update.mockImplementation(respondAsync(null));
    sheetsMock.append.mockImplementation(respondAsync(new Error("The caller does not have permission")));

    await expect(writeData(authorizedIntegration, "sheet1", ["answer"], ["question"])).rejects.toThrow(
      new UnknownError("Error while appending data: The caller does not have permission")
    );
  });

  test("rejects with an UnknownError when the header update callback reports an error", async () => {
    sheetsMock.update.mockImplementation(respondAsync(new Error("Requested entity was not found")));

    await expect(writeData(authorizedIntegration, "sheet1", ["answer"], ["question"])).rejects.toThrow(
      new UnknownError("Error while appending data: Requested entity was not found")
    );
    expect(sheetsMock.append).not.toHaveBeenCalled();
  });

  test("resolves only after both writes have completed", async () => {
    const completed: string[] = [];
    sheetsMock.update.mockImplementation(respondAsync(null, () => completed.push("update")));
    sheetsMock.append.mockImplementation(respondAsync(null, () => completed.push("append")));

    await writeData(authorizedIntegration, "sheet1", ["answer"], ["question"]);

    expect(completed).toEqual(["update", "append"]);
  });

  /**
   * ENG-4050: a minimal model of the Sheets API. `update` writes cells at the range's top-left cell;
   * `append` follows Google's documented rule — find the table at the range and write below its last
   * row — and, like Google, writes AT the range when no cell there holds a value. Driven through
   * `writeData`, it shows whether consecutive responses survive as separate rows.
   */
  const createFakeSheet = () => {
    const rows: string[][] = [];
    const parseRange = (range: string) => {
      const [, startColumn, startRow, endColumn = startColumn] =
        /^([A-Z]+)(\d+)(?::([A-Z]+)\d+)?$/.exec(range) ?? [];
      const toIndex = (letters: string) =>
        [...letters].reduce((acc, ch) => acc * 26 + ch.charCodeAt(0) - 64, 0) - 1;
      return { row: Number(startRow) - 1, firstColumn: toIndex(startColumn), lastColumn: toIndex(endColumn) };
    };
    const rowHasValue = (row: number, from = 0, to = Infinity) =>
      (rows[row] ?? []).some((cell, column) => column >= from && column <= to && cell !== "");
    const writeRow = (row: number, values: string[]) => {
      rows[row] = values;
    };

    sheetsMock.update.mockImplementation(
      (params: { range: string; requestBody: { values: string[][] } }, callback: (err: null) => void) => {
        writeRow(parseRange(params.range).row, params.requestBody.values[0]);
        setTimeout(() => callback(null), 0);
      }
    );
    sheetsMock.append.mockImplementation(
      (params: { range: string; requestBody: { values: string[][] } }, callback: (err: null) => void) => {
        const { row, firstColumn, lastColumn } = parseRange(params.range);
        let target = row;
        if (rowHasValue(row, firstColumn, lastColumn)) {
          while (rowHasValue(target)) target++;
        }
        writeRow(target, params.requestBody.values[0]);
        setTimeout(() => callback(null), 0);
      }
    );

    return rows;
  };

  test("keeps every response as its own row when the first answer is empty", async () => {
    const rows = createFakeSheet();
    const header = ["Welcome (CTA)", "How did you hear about us?", "Rating"];

    await writeData(authorizedIntegration, "sheet1", ["", "Friend", "5"], header);
    await writeData(authorizedIntegration, "sheet1", ["", "Search", "3"], header);
    await writeData(authorizedIntegration, "sheet1", ["", "Ad", "4"], header);

    expect(rows).toEqual([header, ["", "Friend", "5"], ["", "Search", "3"], ["", "Ad", "4"]]);
  });

  test("keeps appending below earlier rows when only some responses start empty", async () => {
    const rows = createFakeSheet();
    const header = ["Question 1", "Question 2"];

    await writeData(authorizedIntegration, "sheet1", ["a", "b"], header);
    await writeData(authorizedIntegration, "sheet1", ["", "c"], header);
    await writeData(authorizedIntegration, "sheet1", ["d", "e"], header);

    expect(rows).toEqual([header, ["a", "b"], ["", "c"], ["d", "e"]]);
  });

  test("anchors the append on the header row the integration writes", async () => {
    createFakeSheet();
    const header = Array.from({ length: 28 }, (_, index) => `Column ${index + 1}`);

    await writeData(authorizedIntegration, "sheet1", Array(28).fill("x"), header);

    expect(sheetsMock.update.mock.calls[0][0]).toMatchObject({ range: "A1" });
    expect(sheetsMock.append.mock.calls[0][0]).toMatchObject({ range: "A1:AB1" });
  });
});
