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 - errors", () => {
  it("returns a stable assistant error when notification dueDate is invalid", async () => {
    const ctx = createToolContext({}, SystemRole.MANAGER);

    const result = await executeTool(
      "create_notification",
      JSON.stringify({
        userId: "user_2",
        type: "TASK_CREATED",
        title: "Need review",
        dueDate: "not-a-datetime",
      }),
      ctx,
    );

    expect(JSON.parse(result.content)).toEqual({
      error: "Invalid dueDate: not-a-datetime",
    });
  });

  it("returns a stable assistant error when notification recipient user is missing", async () => {
    const ctx = createToolContext(
      {
        notification: {
          create: vi.fn().mockRejectedValue({
            code: "P2003",
            message: "Foreign key constraint failed",
            meta: { field_name: "Notification_userId_fkey" },
          }),
        },
      },
      SystemRole.MANAGER,
    );

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

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

  it("returns a stable assistant error when notification assignee user is missing", async () => {
    const ctx = createToolContext(
      {
        notification: {
          create: vi.fn().mockRejectedValue({
            code: "P2003",
            message: "Foreign key constraint failed",
            meta: { field_name: "Notification_assigneeId_fkey" },
          }),
        },
      },
      SystemRole.MANAGER,
    );

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

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

  it("returns a stable assistant error when notification sender user is missing", async () => {
    const ctx = createToolContext(
      {
        notification: {
          create: vi.fn().mockRejectedValue({
            code: "P2003",
            message: "Foreign key constraint failed",
            meta: { field_name: "Notification_senderId_fkey" },
          }),
        },
      },
      SystemRole.MANAGER,
    );

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

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