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 broadcast list tools", () => {
  it("lists broadcasts through the real notification router path for manager users", async () => {
    const db = withUserLookup(
      {
        notificationBroadcast: {
          findMany: vi.fn().mockResolvedValue([
            {
              id: "broadcast_1",
              title: "Holiday planning reminder",
              body: "Please verify local calendars.",
              targetType: "role",
              targetValue: "MANAGER",
              category: "NOTIFICATION",
              priority: "HIGH",
              channel: "in_app",
              recipientCount: 7,
              createdAt: new Date("2026-03-30T10:00:00.000Z"),
              scheduledAt: null,
              sender: {
                id: "user_mgr",
                name: "Manager",
                email: "mgr@example.com",
              },
            },
          ]),
        },
      },
      "user_mgr",
    );
    const ctx = createToolContext(db, SystemRole.MANAGER);

    const result = await executeTool(
      "list_broadcasts",
      JSON.stringify({ limit: 10 }),
      ctx,
    );

    expect(JSON.parse(result.content)).toEqual([
      {
        id: "broadcast_1",
        title: "Holiday planning reminder",
        body: "Please verify local calendars.",
        targetType: "role",
        targetValue: "MANAGER",
        category: "NOTIFICATION",
        priority: "HIGH",
        channel: "in_app",
        recipientCount: 7,
        createdAt: "2026-03-30T10:00:00.000Z",
        scheduledAt: null,
        sender: {
          id: "user_mgr",
          name: "Manager",
          email: "mgr@example.com",
        },
      },
    ]);
    expect(db.notificationBroadcast.findMany).toHaveBeenCalledWith({
      orderBy: { createdAt: "desc" },
      take: 10,
      include: {
        sender: { select: { id: true, name: true, email: true } },
      },
    });
  });

  it("rejects broadcast listing for regular users through the backing router", async () => {
    const ctx = createToolContext(
      withUserLookup({
        notificationBroadcast: {
          findMany: vi.fn(),
        },
      }),
      SystemRole.USER,
    );

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

    expect(JSON.parse(result.content)).toEqual(
      expect.objectContaining({
        error: "You do not have permission to perform this action.",
      }),
    );
  });
});
