import { beforeEach, 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,
    approveEstimateVersion: vi.fn(),
    cloneEstimate: vi.fn(),
    commitDispoImportBatch: vi.fn(),
    countPlanningEntries: vi.fn().mockResolvedValue({ countsByRoleId: new Map() }),
    createEstimateExport: vi.fn(),
    createEstimatePlanningHandoff: vi.fn(),
    createEstimateRevision: vi.fn(),
    assessDispoImportReadiness: vi.fn(),
    loadResourceDailyAvailabilityContexts: vi.fn().mockResolvedValue(new Map()),
    getDashboardDemand: vi.fn().mockResolvedValue([]),
    getDashboardBudgetForecast: vi.fn().mockResolvedValue([]),
    getDashboardOverview: vi.fn(),
    getDashboardSkillGapSummary: vi.fn().mockResolvedValue({
      roleGaps: [],
      totalOpenPositions: 0,
      skillSupplyTop10: [],
      resourcesByRole: [],
    }),
    getDashboardProjectHealth: vi.fn().mockResolvedValue([]),
    getDashboardPeakTimes: vi.fn().mockResolvedValue([]),
    getDashboardTopValueResources: vi.fn().mockResolvedValue([]),
    getEstimateById: vi.fn(),
    listAssignmentBookings: vi.fn().mockResolvedValue([]),
    stageDispoImportBatch: vi.fn(),
    submitEstimateVersion: vi.fn(),
    updateEstimateDraft: vi.fn(),
  };
});

import { executeTool } from "../router/assistant-tools.js";
import { createToolContext } from "./assistant-tools-notification-test-helpers.js";

describe("assistant task creation tools success", () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it("creates a task for a user through the notification router", async () => {
    const db = {
      notification: {
        create: vi.fn().mockResolvedValue({ id: "task_2", userId: "user_2" }),
        findUnique: vi.fn().mockResolvedValue({
          id: "task_2",
          title: "Follow up",
          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_task_for_user",
      JSON.stringify({
        userId: "user_2",
        title: "Follow up",
        dueDate: "2026-04-03T11:00:00.000Z",
        channel: "in_app",
      }),
      ctx,
    );

    expect(db.notification.create).toHaveBeenCalledWith({
      data: expect.objectContaining({
        userId: "user_2",
        type: "TASK_CREATED",
        category: "TASK",
        taskStatus: "OPEN",
        title: "Follow up",
        dueDate: new Date("2026-04-03T11:00:00.000Z"),
        senderId: "user_1",
        channel: "in_app",
      }),
    });
    expect(db.notification.findUnique).toHaveBeenCalledWith({
      where: { id: "task_2" },
    });
    expect(JSON.parse(result.content)).toEqual(
      expect.objectContaining({
        success: true,
        taskId: "task_2",
        message: 'Created task "Follow up" for user_2.',
      }),
    );
  });
});
