import { AllocationStatus } from "@capakraken/shared";
import { describe, expect, it, vi } from "vitest";
import { projectRouter } from "../router/project.js";
import { createCallerFactory } from "../trpc.js";

const createCaller = createCallerFactory(projectRouter);

function createProtectedCaller(db: Record<string, unknown>) {
  return createCaller({
    session: {
      user: { email: "user@example.com", name: "User", image: null },
      expires: "2026-03-13T00:00:00.000Z",
    },
    db: db as never,
    dbUser: { id: "user_1", systemRole: "ADMIN", permissionOverrides: null },
  });
}

describe("project router planning counts", () => {
  it("returns planning entry counts in project.list", async () => {
    const db = {
      project: {
        findMany: vi.fn().mockResolvedValue([
          {
            id: "project_1",
            shortCode: "PRJ",
            name: "Project One",
            orderType: "CHARGEABLE",
            allocationType: "PROJECT",
            winProbability: 100,
            budgetCents: 100000,
            startDate: new Date("2026-03-17"),
            endDate: new Date("2026-03-28"),
            status: "ACTIVE",
            responsiblePerson: null,
            dynamicFields: {},
            staffingReqs: [],
            blueprintId: null,
            createdAt: new Date("2026-03-13"),
            updatedAt: new Date("2026-03-13"),
            _count: { allocations: 1 },
          },
        ]),
        count: vi.fn().mockResolvedValue(1),
      },
      allocation: {
        findMany: vi.fn().mockResolvedValue([
          {
            id: "legacy_demand",
            resourceId: null,
            projectId: "project_1",
            startDate: new Date("2026-03-17"),
            endDate: new Date("2026-03-18"),
            hoursPerDay: 8,
            percentage: 100,
            role: "FX",
            roleId: "role_fx",
            isPlaceholder: true,
            headcount: 2,
            dailyCostCents: 0,
            status: AllocationStatus.PROPOSED,
            metadata: {},
            createdAt: new Date("2026-03-13"),
            updatedAt: new Date("2026-03-13"),
          },
        ]),
      },
      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",
            demandRequirementId: null,
            resourceId: "resource_1",
            projectId: "project_1",
            startDate: new Date("2026-03-19"),
            endDate: new Date("2026-03-20"),
            hoursPerDay: 8,
            percentage: 100,
            role: "FX Lead",
            roleId: "role_fx",
            dailyCostCents: 32000,
            status: AllocationStatus.ACTIVE,
            metadata: {},
            createdAt: new Date("2026-03-13"),
            updatedAt: new Date("2026-03-13"),
          },
        ]),
      },
    };

    const caller = createProtectedCaller(db);
    const result = await caller.list({ limit: 50, page: 1 });

    expect(result.total).toBe(1);
    expect(result.projects).toHaveLength(1);
    expect(result.projects[0]?._count.allocations).toBe(2);
  });
});
