/**
 * Communications tab tests — channels, DMs, message area, send form.
 */

describe("Communications tab", () => {
  it("should render the comms tab container", async () => {
    const comms = await $(".comms-tab");
    if (await comms.isExisting()) {
      expect(await comms.isDisplayed()).toBe(true);
    }
  });

  it("should show mode toggle bar with Channels and DMs", async () => {
    const modeBar = await $(".comms-mode-bar");
    if (!(await modeBar.isExisting())) return;

    const buttons = await $$(".mode-btn");
    expect(buttons.length).toBe(2);

    const texts = await Promise.all(buttons.map((b) => b.getText()));
    expect(texts).toContain("Channels");
    expect(texts).toContain("DMs");
  });

  it("should highlight the active mode button", async () => {
    const activeBtn = await $(".mode-btn.active");
    if (await activeBtn.isExisting()) {
      expect(await activeBtn.isDisplayed()).toBe(true);
    }
  });

  it("should show the comms sidebar", async () => {
    const sidebar = await $(".comms-sidebar");
    if (await sidebar.isExisting()) {
      expect(await sidebar.isDisplayed()).toBe(true);
    }
  });

  it("should show channel list with hash prefix", async () => {
    const hashes = await $$(".ch-hash");
    if (hashes.length > 0) {
      const text = await hashes[0].getText();
      expect(text).toBe("#");
    }
  });

  it("should show message area", async () => {
    const messages = await $(".comms-messages");
    if (await messages.isExisting()) {
      expect(await messages.isDisplayed()).toBe(true);
    }
  });

  it("should show the message input bar", async () => {
    const inputBar = await $(".msg-input-bar");
    if (await inputBar.isExisting()) {
      expect(await inputBar.isDisplayed()).toBe(true);
    }
  });

  it("should have a send button that is disabled when input is empty", async () => {
    const sendBtn = await $(".msg-send-btn");
    if (!(await sendBtn.isExisting())) return;

    const disabled = await sendBtn.getAttribute("disabled");
    // When input is empty, send button should be disabled
    expect(disabled).not.toBeNull();
  });

  it("should switch to DMs mode on DMs button click", async () => {
    const buttons = await $$(".mode-btn");
    if (buttons.length < 2) return;

    // Click DMs button
    await buttons[1].click();
    await browser.pause(300);

    const cls = await buttons[1].getAttribute("class");
    expect(cls).toContain("active");

    // Switch back to channels for other tests
    await buttons[0].click();
    await browser.pause(300);
  });
});
