import { describe, it, expect } from "vitest";
import {
  computeCungMenh,
  batTrach,
  findGoodBuildYears,
  previewBuildYears,
  buildMaterialAdvice,
} from "./year-pick";

describe("computeCungMenh", () => {
  it("1990 Nam = Khảm (Đông tứ mệnh)", () => {
    expect(computeCungMenh(1990, "Nam")).toBe("Khảm");
  });

  it("1985 Nam = Càn (Tây tứ mệnh)", () => {
    expect(computeCungMenh(1985, "Nam")).toBe("Càn");
  });

  it("Nam vs Nữ same year produce different cung", () => {
    expect(computeCungMenh(1990, "Nam")).not.toBe(computeCungMenh(1990, "Nữ"));
  });
});

describe("batTrach", () => {
  it("returns 4 good and 4 bad directions", () => {
    const r = batTrach(1990, "Nam");
    expect(r.goodDirections.length).toBe(4);
    expect(r.badDirections.length).toBe(4);
    expect(r.group).toBe("Đông Tứ Mệnh");
  });

  it("good and bad directions don't overlap", () => {
    const r = batTrach(1990, "Nam");
    const overlap = r.goodDirections.filter((d) => r.badDirections.includes(d));
    expect(overlap).toEqual([]);
  });
});

describe("findGoodBuildYears", () => {
  it("returns count years for a person born 1985", () => {
    const r = findGoodBuildYears(1985, 2026, 5);
    expect(r.length).toBe(5);
    expect(r.every((y) => y.year >= 2026)).toBe(true);
  });

  it("each returned year has positive ageMu and hoangOcType", () => {
    const r = findGoodBuildYears(1990, 2026, 3);
    expect(r.every((y) => y.ageMu > 0)).toBe(true);
    expect(r.every((y) => y.hoangOcType.length > 0)).toBe(true);
  });
});

describe("previewBuildYears", () => {
  it("returns exactly N years (range)", () => {
    const r = previewBuildYears(1990, 2026, 10);
    expect(r.length).toBe(10);
  });

  it("includes both ok and bad years over a 10-year window", () => {
    const r = previewBuildYears(1990, 2026, 10);
    const okCount = r.filter((y) => y.ok).length;
    const badCount = r.filter((y) => !y.ok).length;
    expect(okCount + badCount).toBe(10);
    // Over 10 years, statistically there should be at least one of each
    expect(okCount).toBeGreaterThan(0);
    expect(badCount).toBeGreaterThan(0);
  });

  it("bad years have non-empty reasons", () => {
    const r = previewBuildYears(1990, 2026, 10);
    const bad = r.filter((y) => !y.ok);
    bad.forEach((y) => {
      expect(y.reasons.length).toBeGreaterThan(0);
    });
  });
});

describe("buildMaterialAdvice", () => {
  it("returns element + 4-section advice", () => {
    const r = buildMaterialAdvice(1990); // Lộ Bàng Thổ
    expect(r.element).toBe("Thổ");
    expect(r.good.length).toBeGreaterThanOrEqual(3);
    expect(r.avoid.length).toBeGreaterThanOrEqual(1);
    expect(r.rooms.length).toBeGreaterThanOrEqual(3);
  });

  it("differs by element", () => {
    const t = buildMaterialAdvice(1990); // Thổ
    const k = buildMaterialAdvice(1992); // Kiếm Phong Kim
    expect(t.element).not.toBe(k.element);
    expect(t.good).not.toEqual(k.good);
  });
});
