/* Deterministic cassette traits for the browser-loaded prototype. */

const IJ_CASSETTE_ACCENTS = ["#D93616", "#FF4D2E", "#F5C518", "#2EB67D", "#5C9EAD"];
const IJ_CASSETTE_STICKERS = ["star", "heart", "bolt", "none"];
const IJ_CASSETTE_HUBS = ["classic", "chrome", "cream"];
const IJ_CASSETTE_TAPE_COLORS = ["#5a3a26", "#463020", "#6b3f24", "#3a2418"];
const IJ_DEFAULT_SHELL_FINISH = {
  name: "Classic Hi-Fi Graphite",
  body: "#15151a",
  bodyHighlight: "#3a3a44",
  bodyShadow: "#08080c",
};
const IJ_STANDARD_SHELL_FINISHES = {
  roast: {
    name: "Standard Roast Orange",
    body: "#D4501A",
    bodyHighlight: "#FF7A3E",
    bodyShadow: "#8C2A0A",
  },
  heartfelt: {
    name: "Standard Heartfelt Coral",
    body: "#C8553D",
    bodyHighlight: "#E87A60",
    bodyShadow: "#7A2A1C",
  },
  hype: {
    name: "Standard Hype Gold",
    body: "#F5C518",
    bodyHighlight: "#FFE873",
    bodyShadow: "#A88408",
    labelTone: "neon",
  },
  romantic: {
    name: "Standard Romantic Rose",
    body: "#D85A8A",
    bodyHighlight: "#F590B5",
    bodyShadow: "#8A2E58",
  },
  birthday_holiday: {
    name: "Standard Birthday Pool",
    body: "#5C9EAD",
    bodyHighlight: "#88BCC9",
    bodyShadow: "#2D5762",
    labelTone: "cool",
  },
  business: {
    name: "Standard Business Graphite",
    body: "#1a1a20",
    bodyHighlight: "#3a3a44",
    bodyShadow: "#08080c",
    labelTone: "cool",
  },
  other: IJ_DEFAULT_SHELL_FINISH,
};
const IJ_PREMIUM_SHELL_FINISHES = {
  roast: {
    name: "Premium Punk Silver",
    body: "#9aa0a8",
    bodyHighlight: "#d4d8e0",
    bodyShadow: "#5a6068",
    labelTone: "cool",
  },
  heartfelt: {
    name: "Premium Handmade Cream",
    body: "#FAEAC4",
    bodyHighlight: "#FFF4D8",
    bodyShadow: "#D8C088",
  },
  hype: {
    name: "Premium Black Edition",
    body: "#0a0a0e",
    bodyHighlight: "#2a2a30",
    bodyShadow: "#000000",
    labelTone: "cool",
  },
  romantic: {
    name: "Premium Dream Rose",
    body: "#FFD9F5",
    bodyHighlight: "#C5B8FF",
    bodyShadow: "#8F78D8",
    labelTone: "neon",
  },
  birthday_holiday: {
    name: "Premium Classic Keepsake",
    body: "#1a1a20",
    bodyHighlight: "#3a3a44",
    bodyShadow: "#08080c",
  },
  business: {
    name: "Premium Type II Chrome",
    body: "#a8b098",
    bodyHighlight: "#d4d8c8",
    bodyShadow: "#7a8268",
    labelTone: "cool",
  },
  other: {
    name: "Premium Type II Chrome",
    body: "#a8b098",
    bodyHighlight: "#d4d8c8",
    bodyShadow: "#7a8268",
    labelTone: "cool",
  },
};
const IJ_PREMIUM_VARIANTS = {
  roast: "punk",
  heartfelt: "handmade",
  hype: "memorex",
  romantic: "vapor",
  birthday_holiday: "classic",
  business: "translucent",
  other: "translucent",
};

function ijHashSeed(seed) {
  let hash = 2166136261;
  for (const char of String(seed || "insidejams")) {
    hash ^= char.charCodeAt(0);
    hash = Math.imul(hash, 16777619);
  }
  return hash >>> 0;
}

function ijPick(values, hash, offset) {
  return values[(hash + offset) % values.length];
}

function ijFormatDuration(seconds) {
  const safe = Math.max(0, Math.floor(Number(seconds) || 0));
  return `${Math.floor(safe / 60)}:${String(safe % 60).padStart(2, "0")}`;
}

function formatPrototypeCassetteMeta({ songType, genre, tier, durationSeconds }) {
  return `${String(songType || "jam").toUpperCase()} · ${String(genre || "custom").toUpperCase()} · ${ijNormalizeTier(tier).toUpperCase()} · ${ijFormatDuration(durationSeconds)}`;
}

function ijNormalizeSongType(songType) {
  const key = String(songType || "other")
    .trim()
    .toLowerCase()
    .replace(/[^a-z0-9]+/g, "_")
    .replace(/^_|_$/g, "");
  if (key === "birthday" || key === "birthday_holiday" || key === "holiday") return "birthday_holiday";
  if (key === "commercial") return "business";
  return key || "other";
}

function ijNormalizeTier(tier) {
  return String(tier || "quick").trim().toLowerCase() === "polished" ? "polished" : "quick";
}

function resolvePrototypeCassetteStyle(input) {
  const songKey = ijNormalizeSongType(input.songType);
  const tierStyle = ijNormalizeTier(input.tier) === "polished" ? "premium" : "standard";
  const finishMap = tierStyle === "premium" ? IJ_PREMIUM_SHELL_FINISHES : IJ_STANDARD_SHELL_FINISHES;
  const finish = finishMap[songKey] || finishMap.other || IJ_DEFAULT_SHELL_FINISH;

  return {
    tierStyle,
    previewVariant: tierStyle === "premium" ? (IJ_PREMIUM_VARIANTS[songKey] || "translucent") : "classic",
    shellFinish: {
      name: finish.name,
      body: finish.body,
      bodyHighlight: finish.bodyHighlight,
      bodyShadow: finish.bodyShadow,
    },
    labelTone: finish.labelTone,
  };
}

function createPrototypeCassetteSeed(input) {
  const parts = [
    input.recipient || "your person",
    input.occasion || "custom gift",
    input.songType || "jam",
    input.genre || "custom",
    ijNormalizeTier(input.tier),
    input.title || "",
  ];
  return parts.map((part) => String(part).trim().toLowerCase()).join("|");
}

function createPrototypeCassetteTraits(input) {
  const seed = input.seed || createPrototypeCassetteSeed(input);
  const hash = ijHashSeed(seed);
  const edition = String((hash % 997) + 1).padStart(3, "0");
  const style = resolvePrototypeCassetteStyle(input);
  const labelTone = style.labelTone || (hash % 5 === 0 ? "aged" : "warm");
  const recipient = input.recipient || "your person";
  const occasion = input.occasion || "custom gift";
  const tier = ijNormalizeTier(input.tier);

  return {
    editionId: `IJ-C60-${edition}`,
    shellFinish: style.shellFinish,
    shellAccent: ijPick(IJ_CASSETTE_ACCENTS, hash, 3),
    labelTone,
    labelStripe: ijPick(IJ_CASSETTE_ACCENTS, hash, 7),
    labelStyle: "recipient-handwritten",
    stickerMark: ijPick(IJ_CASSETTE_STICKERS, hash, 11),
    hubStyle: ijPick(IJ_CASSETTE_HUBS, hash, 13),
    reelTapeColor: ijPick(IJ_CASSETTE_TAPE_COLORS, hash, 17),
    tierStyle: style.tierStyle,
    previewVariant: style.previewVariant,
    side: "A",
    label: {
      title: `For ${recipient}, ${occasion}`,
      meta: formatPrototypeCassetteMeta({
        songType: input.songType,
        genre: input.genre,
        tier,
        durationSeconds: input.durationSeconds,
      }),
    },
  };
}

function cassetteLabelToneForPreview(labelTone) {
  return labelTone === "aged" ? "warm" : labelTone;
}

function getPrototypeCassettePreviewProps(traits, options = {}) {
  const compact = Boolean(options.compact);
  const includeEdition = options.includeEdition !== false;
  const tierLabel = traits.tierStyle === "premium" ? "POLISHED" : "QUICK";
  const styleLabel = traits.tierStyle === "premium" ? traits.previewVariant : "standard";
  const meta = compact
    ? `${tierLabel} · ${traits.editionId}`
    : `${traits.label.meta}${includeEdition ? ` · ${traits.editionId}` : ""}`;

  return {
    title: options.title || traits.label.title,
    recipient: options.recipient || traits.label.title.replace(/^For\s+/i, "").split(",")[0],
    meta,
    variant: options.variant || traits.previewVariant || "classic",
    accent: traits.labelStripe || traits.shellAccent,
    body: traits.shellFinish.body,
    bodyHighlight: traits.shellFinish.bodyHighlight,
    bodyShadow: traits.shellFinish.bodyShadow,
    labelTone: cassetteLabelToneForPreview(traits.labelTone),
    tierStyle: traits.tierStyle,
    styleSignal: `${tierLabel} · ${String(styleLabel).toUpperCase()}`,
  };
}

Object.assign(window, {
  createPrototypeCassetteSeed,
  createPrototypeCassetteTraits,
  getPrototypeCassettePreviewProps,
  formatPrototypeCassetteMeta,
});
