import { beforeEach, describe, expect, it, vi } from "vitest";
import { SystemRole } from "@capakraken/shared";
import {
  createToolContext,
  executeTool,
  withUserLookup,
} from "./assistant-tools-task-workflow-test-helpers.js";

describe("assistant task counts tool", () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it("returns task counts for the current user through the real router path", async () => {
    const db = withUserLookup({
      notification: {
        groupBy: vi.fn().mockResolvedValue([
          { taskStatus: "OPEN", _count: 4 },
          { taskStatus: "DONE", _count: 2 },
        ]),
        count: vi.fn().mockResolvedValue(3),
      },
    });
    const ctx = createToolContext(db, SystemRole.USER);

    const result = await executeTool("get_task_counts", JSON.stringify({}), ctx);

    expect(db.notification.groupBy).toHaveBeenCalledWith({
      by: ["taskStatus"],
      where: {
        OR: [{ userId: "user_1" }, { assigneeId: "user_1" }],
        category: { in: ["TASK", "APPROVAL"] },
      },
      _count: true,
    });
    expect(db.notification.count).toHaveBeenCalledWith({
      where: {
        OR: [{ userId: "user_1" }, { assigneeId: "user_1" }],
        category: { in: ["TASK", "APPROVAL"] },
        taskStatus: { in: ["OPEN", "IN_PROGRESS"] },
        dueDate: { lt: expect.any(Date) },
      },
    });
    expect(JSON.parse(result.content)).toEqual({
      open: 4,
      inProgress: 0,
      done: 2,
      dismissed: 0,
      overdue: 3,
    });
  });
});
