import { describe, it, expect } from "vitest";
import { analyzePalm } from "./analyze";

const baseInput = {
  dominantHand: "right" as const,
  shape: "earth" as const,
  heart: { length: "medium" as const, quality: "clear" as const },
  head: { length: "medium" as const, quality: "clear" as const },
  life: { length: "medium" as const, quality: "clear" as const },
  fate: { length: "medium" as const, quality: "clear" as const },
};

describe("analyzePalm", () => {
  it("returns 4 lines with finite scores", () => {
    const r = analyzePalm(baseInput);
    expect(r.lines).toHaveLength(4);
    expect(r.lines.every((l) => l.score >= 0 && l.score <= 25)).toBe(true);
  });

  it("scores higher with stronger lines", () => {
    const weak = analyzePalm({
      ...baseInput,
      heart: { length: "short", quality: "broken" },
      head: { length: "short", quality: "broken" },
      life: { length: "absent", quality: "broken" },
      fate: { length: "absent", quality: "broken" },
    });
    const strong = analyzePalm({
      ...baseInput,
      heart: { length: "long", quality: "clear" },
      head: { length: "long", quality: "clear" },
      life: { length: "long", quality: "clear" },
      fate: { length: "long", quality: "clear" },
      thumbStrong: true,
      mountVenus: true,
      mountJupiter: true,
    });
    expect(strong.totalScore).toBeGreaterThan(weak.totalScore);
    expect(strong.totalScore).toBeGreaterThan(80);
    expect(weak.totalScore).toBeLessThan(50);
  });

  it("score bounded 0-100", () => {
    const allMaxed = analyzePalm({
      ...baseInput,
      heart: { length: "long", quality: "clear" },
      head: { length: "long", quality: "clear" },
      life: { length: "long", quality: "clear" },
      fate: { length: "long", quality: "clear" },
      thumbStrong: true,
      fingersLong: true,
      mountVenus: true,
      mountJupiter: true,
    });
    expect(allMaxed.totalScore).toBeLessThanOrEqual(100);
    expect(allMaxed.totalScore).toBeGreaterThan(0);
  });

  it("includes shape profile", () => {
    const r = analyzePalm({ ...baseInput, shape: "fire" });
    expect(r.shape.name).toContain("Lửa");
    expect(r.shape.desc).toBeTruthy();
  });

  it("rates score with label", () => {
    const r = analyzePalm({
      ...baseInput,
      heart: { length: "long", quality: "clear" },
      head: { length: "long", quality: "clear" },
      life: { length: "long", quality: "clear" },
      fate: { length: "long", quality: "clear" },
      thumbStrong: true,
      mountJupiter: true,
    });
    expect(r.rating).toMatch(/(Rất tốt|Tốt|Khá)/);
  });

  it("warns about absent fate line in career advice", () => {
    const r = analyzePalm({
      ...baseInput,
      fate: { length: "absent", quality: "unknown" },
    });
    expect(r.career).toContain("tự khởi nghiệp");
  });

  it("clarifies short life line is not premature death", () => {
    const r = analyzePalm({
      ...baseInput,
      life: { length: "short", quality: "clear" },
    });
    expect(r.lines.find((l) => l.label.includes("Mệnh"))!.finding).toContain("KHÔNG");
    expect(r.health).toContain("không phải đoản thọ");
  });

  it("personality reflects hand shape", () => {
    const earth = analyzePalm({ ...baseInput, shape: "earth" });
    expect(earth.personality.some((p) => p.includes("Thực tế"))).toBe(true);

    const water = analyzePalm({ ...baseInput, shape: "water" });
    expect(water.personality.some((p) => p.includes("Nhạy cảm"))).toBe(true);
  });

  it("highlights strong lines", () => {
    const r = analyzePalm({
      ...baseInput,
      heart: { length: "long", quality: "clear" },
      head: { length: "long", quality: "clear" },
      life: { length: "long", quality: "clear" },
      fate: { length: "long", quality: "clear" },
    });
    expect(r.highlights.length).toBeGreaterThan(0);
    expect(r.highlights.some((h) => h.includes("Đường"))).toBe(true);
  });
});
