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 notification creation tools - success", () => {
  it("creates a notification through the notification router", async () => {
    const db = {
      notification: {
        create: vi.fn().mockResolvedValue({ id: "notification_1", userId: "user_2" }),
        findUnique: vi.fn().mockResolvedValue({
          id: "notification_1",
          title: "Need review",
          userId: "user_2",
          category: "TASK",
        }),
      },
      user: {
        findUnique: vi.fn().mockResolvedValue({ id: "user_2", email: "user2@example.com", name: "User Two" }),
      },
    };
    const ctx = createToolContext(db, SystemRole.ADMIN);

    const result = await executeTool(
      "create_notification",
      JSON.stringify({
        userId: "user_2",
        type: "TASK_CREATED",
        title: "Need review",
        category: "TASK",
        taskStatus: "OPEN",
        dueDate: "2026-04-02T09:30:00.000Z",
        channel: "in_app",
      }),
      ctx,
    );

    expect(db.notification.create).toHaveBeenCalledWith({
      data: expect.objectContaining({
        userId: "user_2",
        type: "TASK_CREATED",
        title: "Need review",
        category: "TASK",
        taskStatus: "OPEN",
        dueDate: new Date("2026-04-02T09:30:00.000Z"),
        senderId: "user_1",
        channel: "in_app",
      }),
    });
    expect(db.notification.findUnique).toHaveBeenCalledWith({
      where: { id: "notification_1" },
    });
    expect(JSON.parse(result.content)).toEqual(
      expect.objectContaining({
        success: true,
        notificationId: "notification_1",
        message: 'Created notification "Need review".',
      }),
    );
    expect(result.action).toEqual({
      type: "invalidate",
      scope: ["notification"],
    });
  });

  it("defaults task-like notifications to OPEN when the caller omits taskStatus", async () => {
    const db = {
      notification: {
        create: vi.fn().mockResolvedValue({ id: "notification_2", userId: "user_2" }),
        findUnique: vi.fn().mockResolvedValue({
          id: "notification_2",
          title: "Need review",
          userId: "user_2",
          category: "TASK",
          taskStatus: "OPEN",
        }),
      },
      user: {
        findUnique: vi.fn().mockResolvedValue({ id: "user_2", email: "user2@example.com", name: "User Two" }),
      },
    };
    const ctx = createToolContext(db, SystemRole.ADMIN);

    const result = await executeTool(
      "create_notification",
      JSON.stringify({
        userId: "user_2",
        type: "TASK_CREATED",
        title: "Need review",
        category: "TASK",
        channel: "in_app",
      }),
      ctx,
    );

    expect(db.notification.create).toHaveBeenCalledWith({
      data: expect.objectContaining({
        userId: "user_2",
        category: "TASK",
        taskStatus: "OPEN",
      }),
    });
    expect(JSON.parse(result.content)).toEqual(
      expect.objectContaining({
        success: true,
        notificationId: "notification_2",
      }),
    );
  });
});
