import { describe, expect, it } from "vitest";
import { AllocationStatus, type AllocationWithDetails } from "@capakraken/shared";
import {
  buildSplitAllocationReadModel,
} from "../index.js";

const project = {
  id: "project_1",
  name: "Project One",
  shortCode: "P1",
  status: "ACTIVE",
  startDate: new Date("2026-01-01"),
  endDate: new Date("2026-01-31"),
  orderType: "CHARGEABLE",
  budgetCents: 100_000,
  winProbability: 100,
  staffingReqs: [],
  responsiblePerson: "Alex",
} satisfies AllocationWithDetails["project"];

const resource = {
  id: "resource_1",
  displayName: "Ada Lovelace",
  eid: "E-1",
  lcrCents: 10_000,
  chapter: "Engineering",
  availability: null,
};

const roleEntity = {
  id: "role_1",
  name: "Engineer",
  color: "#123456",
};

describe("buildSplitAllocationReadModel", () => {
  it("builds demand and assignment entries from explicit rows", () => {
    const result = buildSplitAllocationReadModel({
      demandRequirements: [
        {
          id: "demand_1",
          projectId: project.id,
          startDate: new Date("2026-01-06"),
          endDate: new Date("2026-01-10"),
          hoursPerDay: 8,
          percentage: 100,
          role: "Engineer",
          roleId: roleEntity.id,
          headcount: 2,
          status: AllocationStatus.PROPOSED,
          metadata: { source: "explicit-demand" },
          createdAt: new Date("2026-01-01"),
          updatedAt: new Date("2026-01-02"),
          project,
          roleEntity,
        },
      ],
      assignments: [
        {
          id: "assignment_1",
          demandRequirementId: "demand_1",
          resourceId: resource.id,
          projectId: project.id,
          startDate: new Date("2026-01-06"),
          endDate: new Date("2026-01-10"),
          hoursPerDay: 8,
          percentage: 100,
          role: "Engineer",
          roleId: roleEntity.id,
          dailyCostCents: 80_000,
          status: AllocationStatus.PROPOSED,
          metadata: { source: "explicit-assignment" },
          createdAt: new Date("2026-01-01"),
          updatedAt: new Date("2026-01-02"),
          resource,
          project,
          roleEntity,
        },
      ],
    });

    expect(result.allocations).toHaveLength(2);
    expect(result.demands).toHaveLength(1);
    expect(result.assignments).toHaveLength(1);

    expect(result.demands[0]).toMatchObject({
      id: "demand_1",
      entityId: "demand_1",
      sourceAllocationId: "demand_1",
      requestedHeadcount: 2,
      metadata: { source: "explicit-demand" },
    });
    expect(result.assignments[0]).toMatchObject({
      id: "assignment_1",
      entityId: "assignment_1",
      sourceAllocationId: "assignment_1",
      resourceId: resource.id,
      metadata: { source: "explicit-assignment" },
    });
  });

  it("returns empty arrays when given no inputs", () => {
    const result = buildSplitAllocationReadModel({
      demandRequirements: [],
      assignments: [],
    });

    expect(result.allocations).toHaveLength(0);
    expect(result.demands).toHaveLength(0);
    expect(result.assignments).toHaveLength(0);
  });
});
