import { initDriver, getDriver } from "../helpers/driver";
import { TestIDs } from "../helpers/selectors";
import { waitForTestId } from "../helpers/utils";

describe("App Launch", () => {
  beforeAll(async () => {
    await initDriver();
    const driver = getDriver();

    // Dismiss Expo Go banner by tapping near the top of the screen
    await driver.pause(3000);
    const { width } = await driver.getWindowSize();
    await driver.touchAction({ action: "tap", x: Math.round(width / 2), y: 100 });
    await driver.pause(500);
  });

  it("should launch the app and show login or chat screen", async () => {
    const driver = getDriver();

    // Wait for either the login screen or the chat screen to appear
    // Try login first with a long timeout (app needs to fully load)
    try {
      await waitForTestId(driver, TestIDs.LOGIN_TITLE, 30_000);
      // On login screen — verify elements
      const identifierInput = await waitForTestId(
        driver,
        TestIDs.LOGIN_IDENTIFIER_INPUT,
      );
      expect(await identifierInput.isDisplayed()).toBe(true);

      const passwordInput = await waitForTestId(
        driver,
        TestIDs.LOGIN_PASSWORD_INPUT,
      );
      expect(await passwordInput.isDisplayed()).toBe(true);

      const loginButton = await waitForTestId(driver, TestIDs.LOGIN_BUTTON);
      expect(await loginButton.isDisplayed()).toBe(true);
    } catch {
      // Not on login — should be on chat screen (auto-login)
      const chatInput = await waitForTestId(
        driver,
        TestIDs.CHAT_MESSAGE_INPUT,
        30_000,
      );
      expect(await chatInput.isDisplayed()).toBe(true);
    }
  });
});
