import { describe, expect, it } from "vitest";
import {
  diffText,
  FACILITY_STATUS_LABEL,
  overallDiff,
} from "@/lib/reports/facility-performance-labels";
import type { FacilityPerformance, KpiValue } from "@/lib/types";

/**
 * 「変化」列とステータスの表示名（D-163）。期待値は監督確定の表 C6〜C10。
 * 画面（FacilityPerformanceTable）から1文字も変えずに移したので、画面の挙動もこれで固定される。
 */
function kpi(diff: number | null): KpiValue {
  return { value: 90, prev: 90, diff };
}

function item(opDiff: number | null, cvDiff: number | null): FacilityPerformance {
  return {
    facility_id: 1,
    facility_name: "メガバリューマート中央本店",
    serial_number: "PUDU-CC1-0001",
    operation_rate: kpi(opDiff),
    launch_rate: kpi(null),
    completion_rate: kpi(cvDiff),
    error_rate: kpi(null),
    status: "good",
    consecutive_problem_days: 0,
  };
}

describe("overallDiff / diffText — 変化列", () => {
  const cases: Array<[string, number | null, number | null, string]> = [
    ["C6 稼働率 +2.4・完了率 +1.0 → round(1.7)=2 で ▲2", 2.4, 1.0, "▲2"],
    ["C7 -0.4・+0.2 → round(-0.1) は -0。-0 === 0 なので →", -0.4, 0.2, "→"],
    ["C8 稼働率の diff が null → →", null, 5, "→"],
    ["C8 完了率の diff が null → →", 5, null, "→"],
    ["C9 -3・-2 → round(-2.5) = -2。絶対値で ▼2", -3, -2, "▼2"],
  ];

  it.each(cases)("%s", (_name, op, cv, expected) => {
    expect(diffText(overallDiff(item(op, cv)))).toBe(expected);
  });

  it("C7 の中間値は -0（Object.is で確かめる。diffText が === 0 で拾う前提）", () => {
    expect(Object.is(overallDiff(item(-0.4, 0.2)), -0)).toBe(true);
  });

  it("C8 どちらかが null なら overallDiff は null", () => {
    expect(overallDiff(item(null, null))).toBeNull();
  });
});

describe("FACILITY_STATUS_LABEL — ステータスの表示名", () => {
  it("C10 good / warning / bad / no_data → 良好 / 注意 / 要改善 / 判定なし", () => {
    expect(FACILITY_STATUS_LABEL).toEqual({
      good: "良好",
      warning: "注意",
      bad: "要改善",
      no_data: "判定なし",
    });
  });
});
