// CostOverviewCards: the four §2 KPI cards and the savings-percent-of-cost
// sub, which must only appear when it is meaningful (savings known, total
// positive). Fabricated CostOverview.

import { describe, expect, it } from "vitest";
import { render, screen } from "@testing-library/react";
import { CostOverviewCards } from "@/components/regions/CostOverviewCards";
import type { CostOverview } from "@/engine/report-model";

const cost: CostOverview = {
  nodegroupCount: 5,
  nodeCount: 10,
  nodeCost: 880,
  nodegroupUnits: [],
  pvcCount: 3,
  pvcTotalGib: 450,
  pvcCost: 36,
  pvcRate: 0.08,
  total: 916,
};

describe("CostOverviewCards", () => {
  it("renders the four cost KPI cards with their subtitles", () => {
    render(<CostOverviewCards cost={cost} savings={91.6} />);
    expect(screen.getByText("Total monthly cost")).toBeInTheDocument();
    expect(screen.getByText("$916.00")).toBeInTheDocument();
    expect(screen.getByText("10 nodes + 3 vmstorage PVCs")).toBeInTheDocument();
    expect(screen.getByText("Node cost")).toBeInTheDocument();
    expect(screen.getByText("$880.00")).toBeInTheDocument();
    expect(screen.getByText("5 nodegroups")).toBeInTheDocument();
    expect(screen.getByText("vmstorage storage")).toBeInTheDocument();
    expect(screen.getByText("$36.00")).toBeInTheDocument();
    expect(screen.getByText("3 PVCs · 450 GiB")).toBeInTheDocument();
    expect(screen.getByText("Potential monthly savings")).toBeInTheDocument();
  });

  it("shows the savings percent of cost when the total is positive", () => {
    render(<CostOverviewCards cost={cost} savings={91.6} />);
    expect(screen.getByText("10.0% of cost")).toBeInTheDocument();
    expect(screen.getByText("$91.60")).toBeInTheDocument();
  });

  it("omits the percent sub when savings is unknown", () => {
    render(<CostOverviewCards cost={cost} savings={null} />);
    expect(screen.queryByText(/of cost/)).toBeNull();
    expect(screen.getByText("—")).toBeInTheDocument(); // money(null)
  });

  it("omits the percent sub when the total is zero (division guard)", () => {
    render(
      <CostOverviewCards
        cost={{ ...cost, total: 0, nodeCost: 0, pvcCost: 0 }}
        savings={50}
      />,
    );
    expect(screen.queryByText(/of cost/)).toBeNull();
  });
});
