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 error tools", () => {
  it("returns a stable assistant error when marking a missing notification as read", async () => {
    const update = vi.fn().mockRejectedValue({
      code: "P2025",
      message: "No record was found for an update.",
    });
    const ctx = createToolContext(
      {
        user: {
          findUnique: vi.fn().mockResolvedValue({ id: "user_1" }),
        },
        notification: {
          update,
        },
      },
      SystemRole.CONTROLLER,
    );

    const result = await executeTool(
      "mark_notification_read",
      JSON.stringify({ notificationId: "notif_missing" }),
      ctx,
    );

    expect(update).toHaveBeenCalledWith({
      where: { id: "notif_missing", userId: "user_1" },
      data: expect.objectContaining({
        readAt: expect.any(Date),
      }),
    });
    expect(JSON.parse(result.content)).toEqual({
      error: "Notification not found with the given criteria.",
    });
  });

  it("returns a stable assistant error when deleting a missing notification", async () => {
    const ctx = createToolContext(withUserLookup({
      notification: {
        findFirst: vi.fn().mockResolvedValue(null),
      },
    }), SystemRole.ADMIN);

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

    expect(JSON.parse(result.content)).toEqual({
      error: "Notification not found with the given criteria.",
    });
  });
});
