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 } from "./assistant-tools-notification-test-helpers.js";

describe("assistant broadcast send tool", () => {
  it("returns a stable assistant error when a scheduled broadcast resolves to no recipients", async () => {
    const create = vi.fn();
    const db = {
      user: {
        findMany: vi.fn().mockResolvedValue([]),
      },
      notificationBroadcast: {
        create,
      },
    };
    const ctx = createToolContext(db, SystemRole.MANAGER);

    const result = await executeTool(
      "send_broadcast",
      JSON.stringify({
        title: "Holiday planning update",
        targetType: "all",
        scheduledAt: "2099-04-10T08:00:00.000Z",
      }),
      ctx,
    );

    expect(db.user.findMany).toHaveBeenCalledWith({
      select: { id: true },
    });
    expect(create).not.toHaveBeenCalled();
    expect(JSON.parse(result.content)).toEqual({
      error: "No recipients matched the broadcast target.",
    });
  });

  it("returns invalidate metadata and broadcast identifiers for scheduled broadcasts", async () => {
    const scheduledAt = "2099-04-10T08:00:00.000Z";
    const create = vi.fn().mockResolvedValue({
      id: "broadcast_future",
      title: "Planned update",
      targetType: "all",
      scheduledAt: new Date(scheduledAt),
    });
    const db = {
      user: {
        findMany: vi.fn().mockResolvedValue([{ id: "user_2" }, { id: "user_3" }]),
      },
      notificationBroadcast: {
        create,
        update: vi.fn(),
      },
      notification: {
        create: vi.fn(),
      },
    };
    const ctx = createToolContext(db, SystemRole.ADMIN);

    const result = await executeTool(
      "send_broadcast",
      JSON.stringify({
        title: "Planned update",
        targetType: "all",
        scheduledAt,
      }),
      ctx,
    );

    expect(db.user.findMany).toHaveBeenCalledWith({
      select: { id: true },
    });
    expect(create).toHaveBeenCalledWith({
      data: expect.objectContaining({
        senderId: "user_1",
        title: "Planned update",
        targetType: "all",
        scheduledAt: new Date(scheduledAt),
      }),
    });
    expect(db.notification.create).not.toHaveBeenCalled();
    expect(db.notificationBroadcast.update).not.toHaveBeenCalled();
    expect(result.action).toEqual({ type: "invalidate", scope: ["notification"] });
    expect(JSON.parse(result.content)).toEqual(
      expect.objectContaining({
        success: true,
        broadcastId: "broadcast_future",
        recipientCount: 0,
        message: 'Broadcast "Planned update" created.',
      }),
    );
  });
});
