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 mutation tools", () => {
  it("marks all unread notifications as read through the notification router", async () => {
    const db = {
      user: {
        findUnique: vi.fn().mockResolvedValue({ id: "user_1" }),
      },
      notification: {
        updateMany: vi.fn().mockResolvedValue({ count: 3 }),
      },
    };
    const ctx = createToolContext(db, SystemRole.CONTROLLER);

    const result = await executeTool("mark_notification_read", "{}", ctx);

    expect(db.notification.updateMany).toHaveBeenCalledWith({
      where: { userId: "user_1", readAt: null },
      data: { readAt: expect.any(Date) },
    });
    expect(JSON.parse(result.content)).toEqual(
      expect.objectContaining({
        success: true,
        message: "All unread notifications marked as read.",
      }),
    );
  });

  it("deletes notifications through the router and invalidates notification queries", async () => {
    const db = withUserLookup({
      notification: {
        findFirst: vi.fn().mockResolvedValue({
          id: "notif_9",
          userId: "user_1",
          category: "NOTIFICATION",
          senderId: "user_mgr",
        }),
        delete: vi.fn().mockResolvedValue({ id: "notif_9" }),
      },
    });
    const ctx = createToolContext(db, SystemRole.USER);

    const result = await executeTool(
      "delete_notification",
      JSON.stringify({ id: "notif_9" }),
      ctx,
    );

    expect(db.notification.delete).toHaveBeenCalledWith({ where: { id: "notif_9" } });
    expect(result.action).toEqual({ type: "invalidate", scope: ["notification"] });
    expect(result.data).toEqual({
      success: true,
      id: "notif_9",
      message: "Deleted notification notif_9.",
    });
  });
});
