import { describe, it, expect } from "vitest";
import { buildHoroscope } from "./horoscope";

describe("buildHoroscope (iztro wrapper)", () => {
  it("computes decadal/yearly/age periods + Vietnamese hạn for 1990-3-15 Male @ 2026-5-30", () => {
    const h = buildHoroscope(
      { date: "1990-3-15", timeIndex: 0, gender: "Nam", calendar: "solar" },
      "2026-5-30"
    );

    // Born year 1990 = Canh Ngọ → birth branch must be Ngọ
    expect(h.birthBranch).toBe("Ngọ");
    // Vietnamese age (tuổi mụ) at 2026: 2026-1990+1 = 37
    expect(h.age).toBe(37);

    // Năm 2026 = Bính Ngọ
    expect(h.yearly.heavenlyStem).toBe("Bính");
    expect(h.yearly.earthlyBranch).toBe("Ngọ");

    // Đại hạn / Tiểu Hạn must have a valid period name
    expect(h.decadal.name).toBe("Đại Hạn");
    expect(h.ageHan.name).toBe("Tiểu Hạn");

    // Decadal timeline must cover ~12 entries sorted by ageFrom
    expect(h.decadalTimeline).toHaveLength(12);
    for (let i = 1; i < h.decadalTimeline.length; i++) {
      expect(h.decadalTimeline[i].ageFrom).toBeGreaterThan(h.decadalTimeline[i - 1].ageFrom);
    }
    // Exactly one entry should be flagged isCurrent.
    expect(h.decadalTimeline.filter((e) => e.isCurrent)).toHaveLength(1);

    // hạn VN: Ngọ thuộc trine Dần-Ngọ-Tuất, năm Ngọ KHÔNG nằm trong tam-tai-band (Thân/Dậu/Tuất)
    expect(h.hanVn.tamTai.active).toBe(false);
    // age 37 → mod9 = 1 → Kim Lâu Thân
    expect(h.hanVn.kimLau.active).toBe(true);
    if (h.hanVn.kimLau.active) {
      expect(h.hanVn.kimLau.kind).toBe("Thân");
    }
    // Born Ngọ, năm Ngọ → Thái Tuế phạm
    const thaiTue = h.hanVn.fixed.find((f) => f.name === "Thái Tuế");
    expect(thaiTue?.active).toBe(true);
  });
});
