import { describe, expect, it, vi } from "vitest";
import { SystemRole } from "@capakraken/shared";

vi.mock("@capakraken/application", async (importOriginal) => {
  const actual = await importOriginal<typeof import("@capakraken/application")>();
  return {
    ...actual,
    getDashboardBudgetForecast: vi.fn().mockResolvedValue([]),
    getDashboardPeakTimes: vi.fn().mockResolvedValue([]),
    listAssignmentBookings: vi.fn().mockResolvedValue([]),
  };
});

import { executeTool } from "../router/assistant-tools.js";
import { createToolContext, withUserLookup } from "./assistant-tools-notification-test-helpers.js";

describe("assistant notification inbox read tools", () => {
  it("returns the unread notification count for the current user", async () => {
    const db = withUserLookup({
      notification: {
        count: vi.fn().mockResolvedValue(5),
      },
    });
    const ctx = createToolContext(db, SystemRole.USER);

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

    expect(JSON.parse(result.content)).toEqual({ count: 5 });
    expect(db.notification.count).toHaveBeenCalledWith(
      expect.objectContaining({
        where: { userId: "user_1", readAt: null },
      }),
    );
  });

  it("lists notifications for the current session user through the real notification router path", async () => {
    const db = {
      user: {
        findUnique: vi.fn().mockResolvedValue({ id: "user_1" }),
      },
      notification: {
        findMany: vi.fn().mockResolvedValue([
          {
            id: "note_1",
            title: "Check staffing",
            body: "Review the new staffing suggestion",
            readAt: null,
            createdAt: new Date("2026-03-29T08:00:00.000Z"),
          },
        ]),
      },
    };
    const ctx = createToolContext(db, SystemRole.CONTROLLER);

    const result = await executeTool(
      "list_notifications",
      JSON.stringify({ unreadOnly: true, limit: 5 }),
      ctx,
    );

    expect(db.user.findUnique).toHaveBeenCalledWith({
      where: { email: "user@example.com" },
      select: { id: true },
    });
    expect(db.notification.findMany).toHaveBeenCalledWith({
      where: {
        userId: "user_1",
        readAt: null,
      },
      orderBy: { createdAt: "desc" },
      take: 5,
    });
    expect(JSON.parse(result.content)).toEqual([
      expect.objectContaining({
        id: "note_1",
        title: "Check staffing",
      }),
    ]);
  });
});
