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

import {
  buildBaseProject,
  buildBaseResource,
  createCreatedAssignment,
  createQuickAssignInput,
  createToolContext,
  executeTool,
  quickAssignPermissions,
  quickAssignRole,
} from "./assistant-tools-timeline-quick-assign-test-helpers.js";

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

  it("quick-assigns a timeline resource through the real timeline router mutation", async () => {
    const createdAssignment = createCreatedAssignment();
    const db = {
      project: {
        findUnique: vi.fn().mockResolvedValue(buildBaseProject()),
      },
      resource: {
        findUnique: vi.fn().mockResolvedValue(buildBaseResource()),
      },
      allocation: {
        findMany: vi.fn().mockResolvedValue([]),
        create: vi.fn(),
      },
      assignment: {
        findMany: vi.fn().mockResolvedValue([]),
        create: vi.fn().mockResolvedValue(createdAssignment),
      },
      vacation: {
        findMany: vi.fn().mockResolvedValue([]),
      },
      auditLog: {
        create: vi.fn().mockResolvedValue({}),
      },
      $transaction: vi.fn(async (callback: (tx: unknown) => unknown) => callback(db)),
    };
    const ctx = createToolContext(db, quickAssignPermissions, quickAssignRole);

    const result = await executeTool(
      "quick_assign_timeline_resource",
      JSON.stringify(createQuickAssignInput()),
      ctx,
    );

    expect(result.action).toEqual({ type: "invalidate", scope: ["allocation", "timeline", "project"] });
    expect(JSON.parse(result.content)).toEqual(
      expect.objectContaining({
        success: true,
        allocation: expect.objectContaining({
          id: "assignment_quick_1",
          projectId: "project_1",
          resourceId: "resource_1",
          hoursPerDay: 8,
        }),
      }),
    );
    expect(db.assignment.create).toHaveBeenCalled();
  });
});
