import { describe, it, expect } from "vitest";
import { fateSummary, suggestMarriageYears, partnerCompat } from "./extra-tools";

describe("fateSummary — base", () => {
  it("returns nap am for known year", () => {
    const r = fateSummary(1984); // Giáp Tý → Hải Trung Kim
    expect(r.napAm).toBe("Hải Trung Kim");
    expect(r.element).toBe("Kim");
    expect(r.stem).toBe("Giáp");
    expect(r.branch).toBe("Tý");
    expect(r.description.length).toBeGreaterThan(20);
  });

  it("does not include topic/zodiac without opts", () => {
    const r = fateSummary(1990);
    expect(r.topic).toBeUndefined();
    expect(r.zodiac).toBeUndefined();
  });
});

describe("fateSummary — topic", () => {
  it("returns career tips for the year's element", () => {
    const r = fateSummary(1990, { topic: "career" });
    expect(r.topic).toBeDefined();
    expect(r.topic?.key).toBe("career");
    expect(r.topic?.label).toBe("Sự nghiệp");
    expect(r.topic?.tips.length).toBeGreaterThanOrEqual(3);
  });

  it("supports all 5 topics", () => {
    for (const t of ["career", "love", "family", "health", "finance"] as const) {
      const r = fateSummary(1990, { topic: t });
      expect(r.topic?.tips.length).toBeGreaterThan(0);
    }
  });

  it("tips differ per element for same topic", () => {
    // 1990 = Lộ Bàng Thổ vs 1992 = Kiếm Phong Kim — career advice differs
    const t1 = fateSummary(1990, { topic: "career" }).topic?.tips;
    const t2 = fateSummary(1992, { topic: "career" }).topic?.tips;
    expect(t1).not.toEqual(t2);
  });
});

describe("fateSummary — zodiac", () => {
  it("includes tam hợp + tứ hành xung when gender provided", () => {
    const r = fateSummary(1990, { gender: "Nam" }); // Canh Ngọ → Ngọ
    expect(r.zodiac).toBeDefined();
    expect(r.zodiac?.branch).toBe("Ngọ");
    expect(r.zodiac?.tamHop).toEqual(["Dần", "Tuất"]);
    expect(r.zodiac?.tuHanhXung).toEqual(["Tý"]);
    expect(r.zodiac?.gender).toBe("Nam");
  });

  it("works with Nữ", () => {
    const r = fateSummary(1985, { gender: "Nữ" }); // Ất Sửu → Sửu
    expect(r.zodiac?.tamHop).toEqual(["Tỵ", "Dậu"]);
    expect(r.zodiac?.tuHanhXung).toEqual(["Mùi"]);
  });
});

describe("fateSummary — combo", () => {
  it("returns both topic and zodiac when both opts set", () => {
    const r = fateSummary(1990, { gender: "Nam", topic: "love" });
    expect(r.topic).toBeDefined();
    expect(r.zodiac).toBeDefined();
  });
});

describe("suggestMarriageYears — full 3 yếu tố", () => {
  it("returns 15 candidate years from start age", () => {
    const r = suggestMarriageYears(1995, "nam");
    expect(r.length).toBe(15);
  });

  it("includes details for all 3 factors", () => {
    const r = suggestMarriageYears(1995, "nam");
    r.forEach((y) => {
      expect(y.details.kimLau).toBeDefined();
      expect(y.details.hoangOc.type).toBeTruthy();
      expect(y.details.tamTai).toBeDefined();
    });
  });

  it("recommended only if all 3 factors are ok", () => {
    const r = suggestMarriageYears(1995, "nam");
    r.forEach((y) => {
      const allOk = y.details.kimLau.ok && y.details.hoangOc.ok && y.details.tamTai.ok;
      expect(y.recommended).toBe(allOk);
    });
  });
});

describe("partnerCompat", () => {
  it("Tam hợp pair (Tý-Thân) scores high", () => {
    // 1984 = Giáp Tý, 1992 = Nhâm Thân → tam hợp
    const r = partnerCompat(1984, 1992);
    expect(r.zodiacRelation).toBe("tam-hop");
    expect(r.score).toBeGreaterThan(60);
  });

  it("Tứ hành xung pair (Tý-Ngọ) scores low", () => {
    // 1984 = Tý, 1990 = Canh Ngọ → tứ hành xung
    const r = partnerCompat(1984, 1990);
    expect(r.zodiacRelation).toBe("tu-hanh-xung");
    expect(r.score).toBeLessThan(50);
  });

  it("flags tam thất (cách 3 năm)", () => {
    const r = partnerCompat(1990, 1993);
    expect(r.tamThat).toBe(true);
    expect(r.ageGap).toBe(3);
  });

  it("flags tam thất (cách 7 năm)", () => {
    const r = partnerCompat(1990, 1997);
    expect(r.tamThat).toBe(true);
    expect(r.ageGap).toBe(7);
  });

  it("returns rating string", () => {
    const r = partnerCompat(1990, 1992);
    expect(["rất hợp", "khá hợp", "tạm chấp nhận", "không thực sự hợp"]).toContain(r.rating);
  });
});
