import { describe, it, expect } from "vitest";
import { computeBatTu, computeDungThan } from "./bat-tu";

describe("computeDungThan", () => {
  it("returns valid structure", () => {
    const bt = computeBatTu(1990, 8, 15, 10);
    const dt = computeDungThan(bt);
    expect(dt.dayMasterElement).toBeDefined();
    expect(["vượng", "nhược", "trung hoà"]).toContain(dt.selfStrength);
    expect(dt.selfScore).toBeGreaterThanOrEqual(0);
    expect(dt.selfScore).toBeLessThanOrEqual(8);
    expect(dt.totalElements).toBe(8);
  });

  it("needs elements have at least 1 entry", () => {
    const bt = computeBatTu(1990, 8, 15, 10);
    const dt = computeDungThan(bt);
    expect(dt.needs.length).toBeGreaterThan(0);
  });

  it("provides directions, colors, numbers, careers for needs", () => {
    const bt = computeBatTu(1990, 8, 15, 10);
    const dt = computeDungThan(bt);
    expect(dt.directions.length).toBeGreaterThan(0);
    expect(dt.colors.length).toBeGreaterThan(0);
    expect(dt.numbers.length).toBeGreaterThan(0);
    expect(dt.careers.length).toBeGreaterThan(0);
  });

  it("vượng case avoids same element", () => {
    // Find a chart that's vượng
    let found = false;
    for (let m = 1; m <= 12 && !found; m++) {
      const bt = computeBatTu(1990, m, 15, 10);
      const dt = computeDungThan(bt);
      if (dt.selfStrength === "vượng") {
        expect(dt.avoids).toContain(dt.dayMasterElement);
        found = true;
      }
    }
  });

  it("summary mentions selfStrength and dayMaster element", () => {
    const bt = computeBatTu(1990, 8, 15, 10);
    const dt = computeDungThan(bt);
    expect(dt.summary).toContain(dt.dayMasterElement);
    expect(dt.summary).toContain(dt.selfStrength);
  });

  it("different birth charts produce different needs", () => {
    const a = computeDungThan(computeBatTu(1990, 1, 15, 10));
    const b = computeDungThan(computeBatTu(1990, 7, 15, 10));
    // Just sanity check — both produce valid output with possibly different needs
    expect(a.needs).toBeDefined();
    expect(b.needs).toBeDefined();
  });
});
