import { describe, expect, it } from "vitest";
import {
  buildReportWorkbookSheets,
  buildResourceMonthExplainabilitySheetRows,
} from "./reportBuilderExplainability.js";

describe("reportBuilderExplainability", () => {
  it("builds a readable explainability sheet for resource month reports", () => {
    const rows = buildResourceMonthExplainabilitySheetRows(
      {
        entity: "resource_month",
        periodMonth: "2026-01",
        locationContextColumns: ["countryCode", "federalState"],
        holidayMetricColumns: ["monthlyPublicHolidayCount"],
        absenceMetricColumns: ["monthlyAbsenceHoursDeduction"],
        capacityMetricColumns: ["monthlyBaseAvailableHours", "monthlySahHours"],
        chargeabilityMetricColumns: ["monthlyActualChargeabilityPct"],
        missingRecommendedColumns: ["countryName", "monthlyTargetHours"],
        notes: ["SAH is holiday-adjusted."],
      },
      (column) => ({
        countryCode: "Country Code",
        federalState: "Federal State",
        monthlyPublicHolidayCount: "Holiday Dates",
        monthlyAbsenceHoursDeduction: "Absence Hours Deduction",
        monthlyBaseAvailableHours: "Base Available Hours",
        monthlySahHours: "SAH",
        monthlyActualChargeabilityPct: "Actual Chargeability (%)",
        countryName: "Country",
        monthlyTargetHours: "Target Hours",
      }[column] ?? column),
    );

    expect(rows).toEqual([
      ["Resource Month Explainability"],
      ["Period Month", "2026-01"],
      ["Location Context Columns", "Country Code", "Federal State"],
      ["Holiday Metric Columns", "Holiday Dates"],
      ["Absence Metric Columns", "Absence Hours Deduction"],
      ["Capacity Metric Columns", "Base Available Hours", "SAH"],
      ["Chargeability Metric Columns", "Actual Chargeability (%)"],
      ["Missing Recommended Columns", "Country", "Target Hours"],
      [],
      ["Notes"],
      ["SAH is holiday-adjusted."],
    ]);
  });

  it("adds grouped report rows and an explainability sheet to workbook output", () => {
    const sheets = buildReportWorkbookSheets({
      columns: ["displayName", "monthlySahHours"],
      rows: [
        { displayName: "Alice", monthlySahHours: 160 },
        { displayName: "Bob", monthlySahHours: 152 },
      ],
      groups: [{ key: "BY", label: "Bayern", rowCount: 2, startIndex: 0 }],
      groupBy: "federalState",
      explainability: {
        entity: "resource_month",
        periodMonth: "2026-01",
        locationContextColumns: [],
        holidayMetricColumns: [],
        absenceMetricColumns: [],
        capacityMetricColumns: [],
        chargeabilityMetricColumns: [],
        missingRecommendedColumns: [],
        notes: [],
      },
      resolveColumnLabel: (column) => ({
        displayName: "Name",
        monthlySahHours: "SAH",
        federalState: "Federal State",
      }[column] ?? column),
    });

    expect(sheets[0]).toEqual({
      name: "Report",
      rows: [
        ["Name", "SAH"],
        ["Federal State: Bayern (2)", ""],
        ["Alice", 160],
        ["Bob", 152],
      ],
    });
    expect(sheets[1]?.name).toBe("Explainability");
  });
});
