import { describe, expect, it } from "vitest";
import { calculateAge, calculateCoverage, formatSdg, paymentStatus } from "@/lib/calculations";
import { formatDate, formatMonthYear } from "@/lib/dates";

describe("sponsorship calculations", () => {
  it("calculates age dynamically around the birthday", () => {
    expect(calculateAge("2010-08-10", new Date("2026-08-09T12:00:00Z"))).toBe(15);
    expect(calculateAge("2010-08-10", new Date("2026-08-10T12:00:00Z"))).toBe(16);
  });

  it("does not invent an age when the birth date is missing", () => {
    expect(calculateAge(null)).toBeNull();
  });

  it("combines several donor contributions without exceeding 100 percent", () => {
    expect(calculateCoverage(50_000, [15_000, 20_000])).toEqual({
      required: 50_000,
      covered: 35_000,
      remaining: 15_000,
      percentage: 70
    });
    expect(calculateCoverage(50_000, [30_000, 30_000]).percentage).toBe(100);
  });

  it("supports multiple partial payments", () => {
    expect(paymentStatus(50_000, 0)).toBe("unpaid");
    expect(paymentStatus(50_000, 20_000)).toBe("partial");
    expect(paymentStatus(50_000, 50_000)).toBe("paid");
  });

  it("formats Sudanese pounds", () => {
    expect(formatSdg(50_000)).toContain("ج.س");
  });

  it("formats user-facing dates as day/month/year without shifting date-only values", () => {
    expect(formatDate("2021-12-14")).toBe("14/12/2021");
    expect(formatMonthYear(2026, 7)).toContain("يوليو");
  });
});
