import { describe, it, expect } from "vitest";
import { colorAdvice, elementFromYear } from "./colors";

describe("elementFromYear", () => {
  it("returns Kim for 1984 (Giáp Tý cycle)", () => {
    // 1984 = Giáp Tý new cycle = Hải Trung Kim
    expect(elementFromYear(1984)).toBe("Kim");
  });

  it("handles years before 1924", () => {
    // 1923 should still resolve to a valid element via mod
    expect(["Kim", "Mộc", "Thuỷ", "Hoả", "Thổ"]).toContain(elementFromYear(1923));
  });

  it("is consistent across full 60-year cycle", () => {
    expect(elementFromYear(1990)).toBe(elementFromYear(2050));
  });
});

describe("colorAdvice — base", () => {
  it("returns universal advice without opts", () => {
    const r = colorAdvice(1990);
    expect(r.element).toBeDefined();
    expect(r.good.length).toBeGreaterThan(0);
    expect(r.bad.length).toBeGreaterThan(0);
    expect(r.application).toBeUndefined();
    expect(r.personalized).toBeUndefined();
  });

  it("includes element description", () => {
    const r = colorAdvice(1990);
    expect(r.description.length).toBeGreaterThan(20);
  });
});

describe("colorAdvice — application", () => {
  it("adds clothing tips", () => {
    const r = colorAdvice(1990, { application: "clothing" });
    expect(r.application).toBeDefined();
    expect(r.application?.key).toBe("clothing");
    expect(r.application?.label).toBe("Trang phục");
    expect(r.application?.tips.length).toBeGreaterThanOrEqual(3);
  });

  it("application tips differ by element", () => {
    // 1990 = Lộ Bàng Thổ; 1991 = Kiếm Phong Kim (different element)
    const t1 = colorAdvice(1990, { application: "vehicle" }).application?.tips;
    const t2 = colorAdvice(1992, { application: "vehicle" }).application?.tips;
    expect(t1).not.toEqual(t2);
  });

  it("supports all 5 application types", () => {
    for (const app of ["clothing", "interior", "vehicle", "business", "wedding"] as const) {
      const r = colorAdvice(1990, { application: app });
      expect(r.application?.tips.length).toBeGreaterThan(0);
    }
  });
});

describe("colorAdvice — personalization", () => {
  it("adds cung mệnh + 4 good directions for Nam", () => {
    const r = colorAdvice(1990, { gender: "Nam" });
    expect(r.personalized).toBeDefined();
    expect(r.personalized?.cung).toBeDefined();
    expect(r.personalized?.group).toMatch(/Đông|Tây/);
    expect(r.personalized?.goodDirections.length).toBe(4);
  });

  it("Nam vs Nữ same year produces different cung", () => {
    const nam = colorAdvice(1990, { gender: "Nam" });
    const nu = colorAdvice(1990, { gender: "Nữ" });
    // For most years, Nam and Nữ are in different mệnh groups
    expect(nam.personalized?.cung).not.toBe(nu.personalized?.cung);
  });

  it("combines application + personalization", () => {
    const r = colorAdvice(1990, { gender: "Nam", application: "clothing" });
    expect(r.application).toBeDefined();
    expect(r.personalized).toBeDefined();
  });
});
