import { expect, test } from "bun:test";
import {
  HABIT_PALETTES,
  isHabitColor,
  previewHabitColors,
} from "./HabitColorPicker";
import { HABIT_COLORS } from "./calendar-model";

test("eight suggested palettes contain thirty-two unique, valid habit colors", () => {
  const colors = HABIT_PALETTES.flatMap((palette) => [...palette.colors]);
  expect(HABIT_PALETTES.length).toBe(8);
  expect(HABIT_PALETTES.every((palette) => palette.colors.length === 4)).toBe(true);
  expect(new Set(colors).size).toBe(32);
  expect(colors.every(isHabitColor)).toBe(true);
});

test("custom colors accept six hex digits and reject incomplete or malformed colors", () => {
  for (const color of ["#123456", "#abcdef", "#ABCDEF", "#ffffff", "#000000"])
    expect(isHabitColor(color)).toBe(true);
  for (const color of ["", "red", "123456", "#fff", "#gggggg", "#12345678"])
    expect(isHabitColor(color)).toBe(false);
});

test("a draft color updates only its habit and cancel restores the saved color", () => {
  const habits = [
    { id: "water", color: HABIT_COLORS.water },
    { id: "reading", color: "#123456" },
  ];
  const preview = previewHabitColors(habits, "water", "#9c647c");
  expect(preview.water).toBe("#9c647c");
  expect(preview.reading).toBe("#123456");
  expect(preview.movement).toBe(HABIT_COLORS.movement);
  expect(previewHabitColors(habits, "water", habits[0]!.color).water).toBe(
    HABIT_COLORS.water,
  );
  expect(habits[0]!.color).toBe(HABIT_COLORS.water);
});

test("invalid drafts fall back to saved colors and saved edits survive selecting another habit", () => {
  const habits = [
    { id: "water", color: "#9c647c" },
    { id: "reading", color: HABIT_COLORS.reading },
  ];
  expect(previewHabitColors(habits, "water", "#zz").water).toBe("#9c647c");
  expect(previewHabitColors(habits, "reading", "#654321").water).toBe(
    "#9c647c",
  );
});
