import { beforeEach, describe, expect, it, vi } from "vitest";

import {
  batchQuickAssignPermissions,
  batchQuickAssignRole,
  buildBaseProject,
  buildBaseResource,
  createBatchQuickAssignInput,
  createToolContext,
  executeTool,
} from "./assistant-tools-timeline-batch-quick-assign-test-helpers.js";

describe("assistant timeline batch quick-assign tools", () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it("batch quick-assigns timeline resources through the real timeline router mutation", async () => {
    const db = {
      project: {
        findUnique: vi.fn().mockResolvedValue(buildBaseProject()),
      },
      resource: {
        findUnique: vi
          .fn()
          .mockImplementation(async ({ where }: { where: { id: string } }) => buildBaseResource(where.id)),
      },
      assignment: {
        findMany: vi.fn().mockResolvedValue([]),
        create: vi
          .fn()
          .mockResolvedValueOnce({ id: "assignment_batch_1" })
          .mockResolvedValueOnce({ id: "assignment_batch_2" }),
      },
      auditLog: {
        create: vi.fn().mockResolvedValue({}),
      },
      vacation: {
        findMany: vi.fn().mockResolvedValue([]),
      },
      $transaction: vi.fn(async (callback: (tx: unknown) => unknown) => callback(db)),
    };
    const ctx = createToolContext(db, batchQuickAssignPermissions, batchQuickAssignRole);

    const result = await executeTool(
      "batch_quick_assign_timeline_resources",
      JSON.stringify(createBatchQuickAssignInput()),
      ctx,
    );

    expect(result.action).toEqual({ type: "invalidate", scope: ["allocation", "timeline", "project"] });
    expect(JSON.parse(result.content)).toEqual(
      expect.objectContaining({
        success: true,
        count: 2,
      }),
    );
    expect(db.assignment.create).toHaveBeenCalledTimes(2);
  });
});
