import { describe, it, expect } from "vitest";
import { analyzeFengShui, computeCungMenh, getGroup, _internal } from "./bat-trach";

describe("Bát Trạch · cung mệnh", () => {
  // Reference values cross-checked against multiple Vietnamese phong thủy
  // tables (Tả Ao, Thẩm Trúc Nhưng, lichngaytot.com).
  it("nam 1990 → Khảm (Đông Tứ)", () => {
    expect(computeCungMenh(1990, "Nam")).toBe("Khảm");
    expect(getGroup("Khảm")).toBe("Đông Tứ Mệnh");
  });

  it("nữ 1990 → Cấn (Tây Tứ)", () => {
    expect(computeCungMenh(1990, "Nữ")).toBe("Cấn");
    expect(getGroup("Cấn")).toBe("Tây Tứ Mệnh");
  });

  it("nam 1985 → Càn (Tây Tứ)", () => {
    expect(computeCungMenh(1985, "Nam")).toBe("Càn");
  });

  it("nữ 1985 → Ly (Đông Tứ)", () => {
    expect(computeCungMenh(1985, "Nữ")).toBe("Ly");
  });

  it("nam 1995 → Đoài", () => {
    // 1+9+9+5=24→6. 11-6=5 → Khôn (nam map 5→2)
    expect(computeCungMenh(1995, "Nam")).toBe("Khôn");
  });

  it("year < 1900 throws", () => {
    expect(() => computeCungMenh(1899, "Nam")).toThrow();
  });

  it("reduceDigits caps at single digit", () => {
    expect(_internal.reduceDigits(2024)).toBe(8);
    expect(_internal.reduceDigits(1990)).toBe(1);
    expect(_internal.reduceDigits(9)).toBe(9);
  });
});

describe("Bát Trạch · direction analysis", () => {
  it("returns 4 good + 4 bad directions for any cung", () => {
    const r = analyzeFengShui({ year: 1990, gender: "Nam" });
    expect(r.goodDirections.length).toBe(4);
    expect(r.badDirections.length).toBe(4);
    expect(r.allDirections.length).toBe(8);
  });

  it("Khảm best is Đông Nam (Sinh Khí)", () => {
    const r = analyzeFengShui({ year: 1990, gender: "Nam" });
    expect(r.cungMenh).toBe("Khảm");
    expect(r.bestDirection.direction).toBe("Đông Nam");
    expect(r.bestDirection.fortune).toBe("Sinh Khí");
  });

  it("Khảm worst is Tây Nam (Tuyệt Mệnh)", () => {
    const r = analyzeFengShui({ year: 1990, gender: "Nam" });
    expect(r.worstDirection.direction).toBe("Tây Nam");
    expect(r.worstDirection.fortune).toBe("Tuyệt Mệnh");
  });

  it("Càn (Tây Tứ) best is Tây (Sinh Khí)", () => {
    const r = analyzeFengShui({ year: 1985, gender: "Nam" });
    expect(r.cungMenh).toBe("Càn");
    expect(r.bestDirection.direction).toBe("Tây");
    expect(r.bestDirection.fortune).toBe("Sinh Khí");
  });

  it("compatibleBagua are same-group minus self", () => {
    const r = analyzeFengShui({ year: 1990, gender: "Nam" }); // Khảm
    expect(new Set(r.compatibleBagua)).toEqual(new Set(["Ly", "Chấn", "Tốn"]));
  });

  it("element + colors match cung mệnh", () => {
    const r = analyzeFengShui({ year: 1985, gender: "Nam" }); // Càn = Kim
    expect(r.element).toBe("Kim");
    expect(r.colors.primary).toContain("Trắng");
    expect(r.colors.avoid).toContain("Đỏ");
  });

  it("every cung has exactly 1 of each fortune", () => {
    const allCung = ["Khảm", "Khôn", "Chấn", "Tốn", "Càn", "Đoài", "Cấn", "Ly"] as const;
    for (const cung of allCung) {
      const row = _internal.FORTUNE_TABLE[cung];
      const fortunes = Object.values(row);
      const unique = new Set(fortunes);
      expect(unique.size).toBe(8); // all 8 fortunes appear exactly once
    }
  });
});
