/**
 * Terminal tests — tab bar, terminal creation, input, collapse/expand.
 */

describe("Terminal section", () => {
  it("should show the terminal tab bar", async () => {
    const tabBar = await $(".terminal-tabs");
    if (await tabBar.isExisting()) {
      expect(await tabBar.isDisplayed()).toBe(true);
    }
  });

  it("should have an add-tab button", async () => {
    const addBtn = await $(".tab-add-btn");
    if (await addBtn.isExisting()) {
      expect(await addBtn.isClickable()).toBe(true);
    }
  });

  it("should create a new terminal tab on add click", async () => {
    const addBtn = await $(".tab-add-btn");
    if (!(await addBtn.isExisting())) return;

    const tabsBefore = await $$(".terminal-tab");
    const countBefore = tabsBefore.length;

    await addBtn.click();
    await browser.pause(500);

    const tabsAfter = await $$(".terminal-tab");
    expect(tabsAfter.length).toBeGreaterThanOrEqual(countBefore + 1);
  });

  it("should show an xterm container", async () => {
    const xterm = await $(".xterm");
    if (await xterm.isExisting()) {
      expect(await xterm.isDisplayed()).toBe(true);
    }
  });

  it("should accept keyboard input in terminal", async () => {
    const xterm = await $(".xterm-helper-textarea");
    if (await xterm.isExisting()) {
      await xterm.click();
      await browser.keys("echo hello");
      // Just verify no crash; actual output verification needs PTY daemon
    }
  });

  it("should support collapse/expand toggle", async () => {
    const collapseBtn = await $(".terminal-collapse-btn");
    if (!(await collapseBtn.isExisting())) return;

    // Click to collapse
    await collapseBtn.click();
    await browser.pause(300);

    const terminalSection = await $(".terminal-section");
    const heightAfterCollapse = await terminalSection.getCSSProperty("height");

    // Click to expand
    await collapseBtn.click();
    await browser.pause(300);

    const heightAfterExpand = await terminalSection.getCSSProperty("height");
    // Heights should differ between collapsed and expanded states
    expect(heightAfterCollapse.value).not.toBe(heightAfterExpand.value);
  });

  it("should highlight active tab", async () => {
    const activeTabs = await $$(".terminal-tab.active");
    if (activeTabs.length > 0) {
      expect(activeTabs.length).toBe(1);
    }
  });

  it("should switch tabs on click", async () => {
    const tabs = await $$(".terminal-tab");
    if (tabs.length >= 2) {
      await tabs[1].click();
      await browser.pause(300);

      const activeClass = await tabs[1].getAttribute("class");
      expect(activeClass).toContain("active");
    }
  });

  it("should show close button on tab hover", async () => {
    const tabs = await $$(".terminal-tab");
    if (tabs.length === 0) return;

    await tabs[0].moveTo();
    await browser.pause(200);

    const closeBtn = await tabs[0].$(".tab-close");
    if (await closeBtn.isExisting()) {
      expect(await closeBtn.isDisplayed()).toBe(true);
    }
  });

  it("should close a tab on close button click", async () => {
    const tabs = await $$(".terminal-tab");
    if (tabs.length < 2) return;

    const countBefore = tabs.length;
    const lastTab = tabs[tabs.length - 1];
    await lastTab.moveTo();
    await browser.pause(200);

    const closeBtn = await lastTab.$(".tab-close");
    if (await closeBtn.isExisting()) {
      await closeBtn.click();
      await browser.pause(300);

      const tabsAfter = await $$(".terminal-tab");
      expect(tabsAfter.length).toBeLessThan(countBefore);
    }
  });
});
