import { expect } from "@playwright/test";
import { logger } from "@formbricks/logger";
import { test } from "../../lib/fixtures";
import { loginAndGetApiKey } from "../../lib/utils";
import { SURVEYS_API_URL } from "../constants";

test.describe("API Tests", () => {
  let surveyId: string;
  let workspaceId: string;
  let apiKey: string;

  test("API Tests", async ({ page, users, request }) => {
    try {
      ({ workspaceId, apiKey } = await loginAndGetApiKey(page, users));
    } catch (error) {
      logger.error(error, "Error during login and getting API key");
      throw error;
    }

    await test.step("Create Survey from API", async () => {
      const response = await request.post(SURVEYS_API_URL, {
        headers: {
          "Content-Type": "application/json",
          "x-api-key": apiKey,
        },
        data: {
          workspaceId: workspaceId,
          type: "link",
          name: "My new Survey from API",
          questions: [
            {
              id: "jpvm9b73u06xdrhzi11k2h76",
              type: "openText",
              headline: {
                default: "What would you like to know?",
              },
              required: true,
              inputType: "text",
              subheader: {
                default: "This is an example survey.",
              },
              placeholder: {
                default: "Type your answer here...",
              },
              charLimit: {
                enabled: false,
              },
            },
          ],
        },
      });

      expect(response.ok()).toBeTruthy();
      const responseBody = await response.json();
      expect(responseBody.data.name).toEqual("My new Survey from API");
      expect(responseBody.data.workspaceId).toEqual(workspaceId);

      surveyId = responseBody.data.id;
    });

    await test.step("List Surveys from API", async () => {
      const response = await request.get(SURVEYS_API_URL, {
        headers: {
          "x-api-key": apiKey,
        },
      });
      expect(response.ok()).toBeTruthy();
      const responseBody = await response.json();
      const createdSurvey = responseBody.data.find(
        (survey: { name: string }) => survey.name === "My new Survey from API"
      );

      expect(createdSurvey).toBeDefined();
      expect(responseBody.data.length).toBeGreaterThanOrEqual(1);

      if (!createdSurvey) {
        throw new Error("Expected created survey to be defined");
      }

      surveyId = createdSurvey.id;
    });

    await test.step("Get Survey by ID from API", async () => {
      const responseSurvey = await request.get(`${SURVEYS_API_URL}/${surveyId}`, {
        headers: {
          "content-type": "application/json",
          "x-api-key": apiKey,
        },
      });
      expect(responseSurvey.ok()).toBeTruthy();
      const responseBodySurvey = await responseSurvey.json();

      expect(responseBodySurvey.data.id).toEqual(surveyId);
    });

    await test.step("Updated Survey by ID from API", async () => {
      const response = await request.put(`${SURVEYS_API_URL}/${surveyId}`, {
        headers: {
          "Content-Type": "application/json",
          "x-api-key": apiKey,
        },
        data: {
          name: "My updated Survey from API",
        },
      });

      expect(response.ok()).toBeTruthy();
      const responseBody = await response.json();
      expect(responseBody.data.name).toEqual("My updated Survey from API");
    });

    await test.step("Delete Survey by ID from API", async () => {
      const response = await request.delete(`${SURVEYS_API_URL}/${surveyId}`, {
        headers: {
          "Content-Type": "application/json",
          "x-api-key": apiKey,
        },
      });
      expect(response.ok()).toBeTruthy();
      const responseBody = await response.json();
      expect(responseBody.data.name).toEqual("My updated Survey from API");

      const responseSurvey = await request.get(`${SURVEYS_API_URL}/${surveyId}`, {
        headers: {
          "content-type": "application/json",
          "x-api-key": apiKey,
        },
      });
      expect(responseSurvey.ok()).toBeFalsy();
    });
  });
});
