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 reminder creation tools - validation errors", () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it("returns a stable assistant error when reminder creation receives an invalid datetime", async () => {
    const ctx = createToolContext(
      {
        user: {
          findUnique: vi.fn().mockResolvedValue({ id: "user_1" }),
        },
      },
      SystemRole.ADMIN,
    );

    const result = await executeTool(
      "create_reminder",
      JSON.stringify({
        title: "Submit report",
        remindAt: "not-a-datetime",
      }),
      ctx,
    );

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

  it("returns stable assistant errors for reminder validation edge cases", async () => {
    const cases = [
      {
        payload: {
          title: "   ",
          remindAt: "2026-04-01T09:00:00.000Z",
        },
        expected: "Reminder title is required.",
      },
      {
        payload: {
          title: "x".repeat(201),
          remindAt: "2026-04-01T09:00:00.000Z",
        },
        expected: "Reminder title must be at most 200 characters.",
      },
      {
        payload: {
          title: "Submit report",
          body: "x".repeat(2001),
          remindAt: "2026-04-01T09:00:00.000Z",
        },
        expected: "Reminder body must be at most 2000 characters.",
      },
      {
        payload: {
          title: "Submit report",
          remindAt: "2026-04-01T09:00:00.000Z",
          recurrence: "yearly",
        },
        expected: "Invalid recurrence: yearly. Valid values: daily, weekly, monthly.",
      },
    ] as const;

    for (const testCase of cases) {
      const ctx = createToolContext({}, SystemRole.ADMIN);
      const result = await executeTool(
        "create_reminder",
        JSON.stringify(testCase.payload),
        ctx,
      );

      expect(JSON.parse(result.content)).toEqual({ error: testCase.expected });
    }
  });
});
