import { AllocationStatus } from "@capakraken/shared";
import { describe, expect, it, vi } from "vitest";
import { loadProjectPlanningReadModel } from "../router/project-planning-read-model.js";

describe("loadProjectPlanningReadModel", () => {
  it("applies active-only filters to demand and assignment loaders", async () => {
    const demandFindMany = vi.fn().mockResolvedValue([]);
    const assignmentFindMany = vi.fn().mockResolvedValue([]);

    await loadProjectPlanningReadModel(
      {
        demandRequirement: { findMany: demandFindMany },
        assignment: { findMany: assignmentFindMany },
      } as never,
      { projectId: "project_1", activeOnly: true },
    );

    expect(demandFindMany).toHaveBeenCalledWith(
      expect.objectContaining({
        where: { projectId: "project_1", status: { not: AllocationStatus.CANCELLED } },
      }),
    );
    expect(assignmentFindMany).toHaveBeenCalledWith(
      expect.objectContaining({
        where: { projectId: "project_1", status: { not: AllocationStatus.CANCELLED } },
      }),
    );
  });

  it("builds a split read model from demand and assignment rows", async () => {
    const result = await loadProjectPlanningReadModel(
      {
        demandRequirement: {
          findMany: vi.fn().mockResolvedValue([
            {
              id: "demand_1",
              projectId: "project_1",
              startDate: new Date("2026-03-17"),
              endDate: new Date("2026-03-18"),
              hoursPerDay: 8,
              percentage: 100,
              role: "FX",
              roleId: "role_fx",
              headcount: 2,
              status: AllocationStatus.PROPOSED,
              metadata: {},
              createdAt: new Date("2026-03-13"),
              updatedAt: new Date("2026-03-13"),
            },
          ]),
        },
        assignment: {
          findMany: vi.fn().mockResolvedValue([
            {
              id: "assignment_1",
              resourceId: "resource_1",
              projectId: "project_1",
              startDate: new Date("2026-03-17"),
              endDate: new Date("2026-03-18"),
              hoursPerDay: 8,
              percentage: 100,
              role: "Lead",
              roleId: "role_lead",
              dailyCostCents: 32000,
              status: AllocationStatus.ACTIVE,
              metadata: {},
              createdAt: new Date("2026-03-13"),
              updatedAt: new Date("2026-03-13"),
              resource: {
                id: "resource_1",
                displayName: "Alice",
                eid: "E-001",
                chapter: "CGI",
                lcrCents: 5000,
                availability: { monday: 8 },
              },
            },
          ]),
        },
      } as never,
      { projectId: "project_1" },
    );

    expect(result.readModel.demands.map((entry) => entry.sourceAllocationId)).toEqual(["demand_1"]);
    expect(result.readModel.assignments.map((entry) => entry.sourceAllocationId)).toEqual([
      "assignment_1",
    ]);
    expect(result.readModel.allocations.map((entry) => entry.id)).toEqual([
      "demand_1",
      "assignment_1",
    ]);
  });
});
