import {
  markFull,
  markAdvanced,
  hasSeenVariant,
  hasSeen,
  hasKilled,
  hasFull,    
  hasAdvanced, 
} from "./storage.js";

export function checkFull(player, entry) {
  const prog = entry.progression ?? {};
  const mode = prog.full ?? "seen";

  // evita escribir cada tick si ya está
  if (hasFull?.(player, entry.id)) return;

  if (mode === "seen") {
    if (hasSeen(player, entry.id)) markFull(player, entry.id);
    return;
  }

  if (mode === "kill") {
    if (hasKilled(player, entry.id)) markFull(player, entry.id);
    return;
  }

  if (mode === "variants") {
    const vars = Array.isArray(entry.variants) ? entry.variants : [];
    if (vars.length === 0) {
      if (hasSeen(player, entry.id)) markFull(player, entry.id);
      return;
    }

    const allSeen = vars.every((v) => hasSeenVariant(player, entry.id, v.id));
    if (allSeen) markFull(player, entry.id);
  }
}

export function checkAdvanced(player, entry) {
  const prog = entry.progression ?? {};
  const mode = prog.advanced ?? "full";

  // evita escribir cada tick si ya está
  if (hasAdvanced?.(player, entry.id)) return;

  if (mode === "full") {
    // SOLO cuando full ya se consiguió
    if (hasFull?.(player, entry.id)) markAdvanced(player, entry.id);
    return;
  }

  if (mode === "kill") {
    if (hasKilled(player, entry.id)) markAdvanced(player, entry.id);
    return;
  }

  if (mode === "variants") {
    const vars = Array.isArray(entry.variants) ? entry.variants : [];
    if (vars.length === 0) return;

    const allSeen = vars.every((v) => hasSeenVariant(player, entry.id, v.id));
    if (allSeen) markAdvanced(player, entry.id);
  }
}