// @vitest-environment jsdom
import { cleanup, fireEvent, waitFor, within } from "@testing-library/react";
import { afterEach, describe, expect, it } from "vitest";
import { loadPluginApp, renderSlot } from "@get-bb/plugin-sdk/testing/app";
import { COMPACT_VIEWPORT_QUERY } from "@/components/ui/hooks/use-compact-viewport";
import type {
  Project,
  Task,
  TaskMutationResult,
} from "../../shared/contract.js";
import {
  makeTask,
  rpcInput,
  labelsByProject,
  matchingTasks,
} from "../../test-fixtures.js";

window.matchMedia = (query: string) => ({
  matches: query === COMPACT_VIEWPORT_QUERY,
  media: query,
  onchange: null,
  addListener: () => {},
  removeListener: () => {},
  addEventListener: () => {},
  removeEventListener: () => {},
  dispatchEvent: () => false,
});
window.ResizeObserver ??= class {
  observe() {}
  unobserve() {}
  disconnect() {}
};
Element.prototype.scrollIntoView ??= () => {};

const app = await loadPluginApp(() => import("../../app"));

afterEach(cleanup);

const PROJECT_ID = "01HZZZZZZZZZZZZZZZZZZZZZP1";

const project = {
  id: PROJECT_ID,
  name: "Tasks Plugin",
  prefix: "TSK",
  nextTaskNumber: 5,
  color: "blue",
  folderId: null,
  linkedBbProjectId: null,
  createdAt: "2026-07-15T00:00:00.000Z",
};

const label = {
  id: "01HZZZZZZZZZZZZZZZZZZZZLBL",
  projectId: PROJECT_ID,
  name: "Bug",
  color: "#e5484d",
};

function task(overrides: Partial<Task> & Pick<Task, "id" | "number">): Task {
  return makeTask({
    projectId: PROJECT_ID,
    key: `TSK-${overrides.number}`,
    title: `Task ${overrides.number}`,
    position: overrides.number,
    ...overrides,
  });
}

interface Options {
  projects?: Project[];
  moveTaskToProject?: (input: Record<string, unknown>) => TaskMutationResult;
  updateTask?: (input: Record<string, unknown>) => TaskMutationResult;
}

function renderList(tasks: Task[], options: Options = {}) {
  return renderSlot(
    app.navPanels[0]!,
    { subPath: PROJECT_ID },
    {
      rpc: {
        listProjects: () => ({ projects: options.projects ?? [project] }),
        listFolders: () => ({ folders: [] }),
        listPresets: () => ({ presets: [] }),
        sidebarSummary: () => ({ projects: [] }),
        listLabels: () => ({ labels: [label] }),
        labelsByProject: labelsByProject([label]),
        listAllTasks: (raw: unknown) => ({ tasks: matchingTasks(tasks, raw) }),
        listTaskCardMeta: () => ({ cards: [] }),
        listTaskThreads: () => ({ taskThreads: [] }),
        listComments: () => ({ comments: [] }),
        listAttachments: () => ({ attachments: [] }),
        moveTaskToProject: (raw) => {
          if (!options.moveTaskToProject) throw new Error("Unexpected move");
          return options.moveTaskToProject(rpcInput(raw));
        },
        updateTask: (raw) => {
          const input = rpcInput(raw);
          if (options.updateTask) return options.updateTask(input);
          const current = tasks.find((entry) => entry.id === input.taskId)!;
          return { ok: true, task: { ...current, ...input } };
        },
      },
    },
  );
}

async function rowFor(slot: ReturnType<typeof renderList>, key: string) {
  await slot.findByText(key);
  const row = slot.container.querySelector(`[data-task-key="${key}"]`);
  if (row === null) throw new Error(`row ${key} not found`);
  return row as HTMLElement;
}

describe("inline row editing", () => {
  it("exposes the complete key when a row label is constrained", async () => {
    const current = task({
      id: "01HZT1ZZZZZZZZZZZZZZZZZZT1",
      number: 12345,
      key: "VALIDATION-12345",
    });
    const slot = renderList([current]);
    const row = await rowFor(slot, current.key);
    expect(within(row).getByText(current.key).getAttribute("title")).toBe(
      current.key,
    );
  });

  it("searches task keys and titles without case or surrounding whitespace", async () => {
    const slot = renderList([
      task({ id: "01HZT1ZZZZZZZZZZZZZZZZZZT1", number: 1, title: "Repair toolbar" }),
      task({ id: "01HZT1ZZZZZZZZZZZZZZZZZZT2", number: 2, title: "Check layout" }),
    ]);
    await slot.findByText("TSK-1");
    const search = slot.getByRole("searchbox", { name: "Search tasks" });
    fireEvent.change(search, { target: { value: "  tSk-2  " } });
    expect(slot.queryByText("TSK-1")).toBeNull();
    expect(slot.getByText("TSK-2")).toBeTruthy();
    fireEvent.change(search, { target: { value: "TOOLBAR" } });
    expect(slot.getByText("TSK-1")).toBeTruthy();
    expect(slot.queryByText("TSK-2")).toBeNull();
    fireEvent.change(search, { target: { value: "nothing-matches" } });
    expect(slot.getByText("No tasks match these filters")).toBeTruthy();
    fireEvent.click(slot.getByRole("button", { name: "Clear filters" }));
    expect(slot.getByText("TSK-1")).toBeTruthy();
    expect(slot.getByText("TSK-2")).toBeTruthy();
    expect(search).toHaveProperty("value", "");
  });

  it("opens a windowed row using its task key", async () => {
    const slot = renderList([
      task({ id: "01HZT1ZZZZZZZZZZZZZZZZZZT1", number: 1 }),
    ]);
    const row = await rowFor(slot, "TSK-1");
    fireEvent.click(
      within(row).getByRole("button", { name: "Open TSK-1: Task 1" }),
    );
    expect(slot.navigateCalls).toContainEqual({
      method: "toPluginPanel",
      path: "tasks",
      options: { subPath: "task/TSK-1" },
    });
  });

  it("moves a windowed row through its project context menu", async () => {
    const current = task({ id: "01HZT1ZZZZZZZZZZZZZZZZZZT1", number: 1 });
    const target = {
      ...project,
      id: "01HZZZZZZZZZZZZZZZZZZZZZP2",
      name: "Personal",
      prefix: "HOME",
    };
    const slot = renderList([current], {
      projects: [project, target],
      moveTaskToProject: () => ({
        ok: true,
        task: { ...current, projectId: target.id, key: "HOME-5", number: 5 },
      }),
    });
    const row = await rowFor(slot, "TSK-1");
    fireEvent.contextMenu(row);
    fireEvent.click(
      await slot.findByRole("menuitem", { name: "Move to project" }),
    );
    fireEvent.click(await slot.findByRole("menuitem", { name: /Personal/ }));
    await waitFor(() =>
      expect(slot.rpcCalls).toContainEqual({
        method: "moveTaskToProject",
        input: { taskId: current.id, projectId: target.id },
      }),
    );
  });

  it("optimistically applies a status change and persists it", async () => {
    const slot = renderList([
      task({ id: "01HZT1ZZZZZZZZZZZZZZZZZZT1", number: 1 }),
    ]);
    const row = await rowFor(slot, "TSK-1");

    fireEvent.click(
      within(row).getByRole("button", {
        name: /Change status, currently Todo/,
      }),
    );
    const drawer = await slot.findByRole("dialog", { name: "Change status" });
    fireEvent.click(
      await within(drawer).findByRole("menuitem", { name: /Done/ }),
    );

    await waitFor(() =>
      expect(
        slot.rpcCalls.some(
          (call) =>
            call.method === "updateTask" &&
            (call.input as { status?: string }).status === "done",
        ),
      ).toBe(true),
    );
    await waitFor(() =>
      expect(
        within(
          slot.container.querySelector('[data-task-key="TSK-1"]')!,
        ).getByRole("button", { name: /Change status, currently Done/ }),
      ).toBeTruthy(),
    );
  });

  it("optimistically applies a priority change", async () => {
    const slot = renderList([
      task({ id: "01HZT1ZZZZZZZZZZZZZZZZZZT1", number: 1 }),
    ]);
    const row = await rowFor(slot, "TSK-1");

    fireEvent.click(
      within(row).getByRole("button", {
        name: /Set priority, currently No priority/,
      }),
    );
    const drawer = await slot.findByRole("dialog", { name: "Set priority" });
    fireEvent.click(
      await within(drawer).findByRole("menuitem", { name: /Urgent/ }),
    );

    await waitFor(() =>
      expect(
        slot.rpcCalls.some(
          (call) =>
            call.method === "updateTask" &&
            (call.input as { priority?: string }).priority === "urgent",
        ),
      ).toBe(true),
    );
    await waitFor(() =>
      expect(
        within(
          slot.container.querySelector('[data-task-key="TSK-1"]')!,
        ).getByRole("button", { name: /currently Urgent/ }),
      ).toBeTruthy(),
    );
  });

  it("rolls back and surfaces an error when the mutation fails", async () => {
    const slot = renderList(
      [task({ id: "01HZT1ZZZZZZZZZZZZZZZZZZT1", number: 1 })],
      {
        updateTask: () => ({
          ok: false,
          error: { code: "task_parent_invalid", message: "Server rejected it" },
        }),
      },
    );
    const row = await rowFor(slot, "TSK-1");

    fireEvent.click(
      within(row).getByRole("button", {
        name: /Change status, currently Todo/,
      }),
    );
    const drawer = await slot.findByRole("dialog", { name: "Change status" });
    fireEvent.click(
      await within(drawer).findByRole("menuitem", { name: /Done/ }),
    );

    await slot.findByText("Server rejected it");
    await waitFor(() =>
      expect(
        within(
          slot.container.querySelector('[data-task-key="TSK-1"]')!,
        ).getByRole("button", { name: /Change status, currently Todo/ }),
      ).toBeTruthy(),
    );
  });

  it("only issues a mutation when the value actually changes", async () => {
    const slot = renderList([
      task({ id: "01HZT1ZZZZZZZZZZZZZZZZZZT1", number: 1, status: "todo" }),
    ]);
    const row = await rowFor(slot, "TSK-1");

    fireEvent.click(
      within(row).getByRole("button", {
        name: /Change status, currently Todo/,
      }),
    );
    const drawer = await slot.findByRole("dialog", { name: "Change status" });
    fireEvent.click(
      await within(drawer).findByRole("menuitem", { name: /Todo/ }),
    );

    await waitFor(() => expect(slot.queryByRole("dialog")).toBeNull());
    expect(slot.rpcCalls.some((call) => call.method === "updateTask")).toBe(
      false,
    );
  });
});
