/**
 * Task board tests — kanban columns, cards, create form, drag-drop.
 */

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

  it("should show the toolbar with title", async () => {
    const title = await $(".tb-title");
    if (await title.isExisting()) {
      expect(await title.getText()).toBe("Task Board");
    }
  });

  it("should have 5 kanban columns", async () => {
    const columns = await $$(".tb-column");
    if (columns.length > 0) {
      expect(columns.length).toBe(5);
    }
  });

  it("should show column headers with labels", async () => {
    const labels = await $$(".tb-col-label");
    if (labels.length === 0) return;

    const texts = await Promise.all(labels.map((l) => l.getText()));
    const expected = ["TO DO", "IN PROGRESS", "REVIEW", "DONE", "BLOCKED"];
    for (const exp of expected) {
      expect(texts.some((t) => t.toUpperCase().includes(exp))).toBe(true);
    }
  });

  it("should show column counts", async () => {
    const counts = await $$(".tb-col-count");
    if (counts.length > 0) {
      // Each column should have a count badge
      expect(counts.length).toBe(5);
    }
  });

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

  it("should toggle create form on add button click", async () => {
    const addBtn = await $(".tb-add-btn");
    if (!(await addBtn.isExisting())) return;

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

    const form = await $(".tb-create-form");
    if (await form.isExisting()) {
      expect(await form.isDisplayed()).toBe(true);

      // Close form
      await addBtn.click();
      await browser.pause(200);
    }
  });

  it("should show task count in toolbar", async () => {
    const count = await $(".tb-count");
    if (await count.isExisting()) {
      const text = await count.getText();
      expect(text).toMatch(/\d+ tasks?/);
    }
  });
});
