// RetroVoting.jsx — the active retro board with tabs + dot voting.

const { useState: useStateRV, useMemo: useMemoRV } = React;

// ─────────────────────────────────────────────────────────────
// Author badge — initials avatar
// ─────────────────────────────────────────────────────────────
function RVAuthor({ author, size = 22 }) {
  return (
    <div style={{
      width: size, height: size,
      borderRadius: "50%",
      background: author.color,
      color: author.fg,
      display: "flex",
      alignItems: "center",
      justifyContent: "center",
      fontFamily: "var(--font-family-sans)",
      fontWeight: 700,
      fontSize: size <= 22 ? 9 : 10,
      letterSpacing: "0.04em",
      flexShrink: 0,
    }}>{author.initials}</div>
  );
}

// ─────────────────────────────────────────────────────────────
// Vote-count meter — dots showing base + player
// ─────────────────────────────────────────────────────────────
function RVVoteMeter({ baseVotes, playerVoted, colDef }) {
  // Show baseVotes filled dots + 1 if player voted, max display 6
  const totalShown = Math.min(baseVotes + (playerVoted ? 1 : 0), 6);
  const dots = Array.from({ length: 6 });
  return (
    <div style={{
      display: "flex",
      gap: 3,
      alignItems: "center",
    }}>
      {dots.map((_, i) => {
        const filledBase = i < baseVotes;
        const filledPlayer = !filledBase && i === baseVotes && playerVoted;
        return (
          <span key={i} aria-hidden style={{
            width: 8, height: 8, borderRadius: "50%",
            background: filledBase
              ? colDef.fg
              : filledPlayer
                ? "var(--color-brand-primary)"
                : "transparent",
            border: filledBase || filledPlayer ? "none" : "1.5px solid var(--color-border-subtle)",
            boxShadow: filledPlayer ? "0 0 0 2px rgba(196,31,62,0.18)" : "none",
            transition: "background var(--motion-base) var(--ease-standard), box-shadow var(--motion-base) var(--ease-standard)",
          }} />
        );
      })}
      <span style={{
        marginLeft: 4,
        fontSize: 11,
        fontWeight: 700,
        fontVariantNumeric: "tabular-nums",
        color: "var(--color-text-primary)",
        letterSpacing: "-0.005em",
      }}>
        {baseVotes + (playerVoted ? 1 : 0)}
      </span>
    </div>
  );
}

// ─────────────────────────────────────────────────────────────
// Single sticky note
// ─────────────────────────────────────────────────────────────
function RVStickyNote({ item, colDef, playerVoted, locked, canVote, onToggle }) {
  const disabled = locked || (!playerVoted && !canVote);
  return (
    <button
      onClick={() => !locked && (playerVoted || canVote) && onToggle(item.id)}
      disabled={disabled}
      style={{
        position: "relative",
        background: playerVoted ? "var(--color-surface-promo)" : colDef.stickyBg,
        border: `1px solid ${playerVoted ? "var(--color-brand-primary)" : colDef.stickyBorder}`,
        borderRadius: "var(--radius-md)",
        padding: "10px 12px 10px",
        textAlign: "left",
        cursor: disabled ? "default" : "pointer",
        boxShadow: playerVoted ? "var(--shadow-2)" : "var(--shadow-1)",
        opacity: disabled && !playerVoted ? 0.55 : 1,
        transition: "transform var(--motion-base) var(--ease-standard), background var(--motion-base) var(--ease-standard), border-color var(--motion-base) var(--ease-standard), box-shadow var(--motion-base) var(--ease-standard)",
        transform: playerVoted ? "translateY(-1px)" : "translateY(0)",
        font: "inherit",
        color: "inherit",
        width: "100%",
        boxSizing: "border-box",
        display: "flex",
        flexDirection: "column",
        gap: 8,
      }}
    >
      {playerVoted && (
        <span aria-hidden style={{
          position: "absolute",
          top: -8, right: -8,
          width: 24, height: 24,
          borderRadius: "50%",
          background: "var(--color-brand-primary)",
          color: "var(--color-text-on-brand)",
          display: "flex",
          alignItems: "center",
          justifyContent: "center",
          fontFamily: "var(--font-family-sans)",
          fontWeight: 700,
          fontSize: 9,
          letterSpacing: "0.08em",
          boxShadow: "var(--shadow-2)",
        }}>
          <svg width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="3.5" strokeLinecap="round" strokeLinejoin="round">
            <polyline points="20 6 9 17 4 12" />
          </svg>
        </span>
      )}

      <div style={{
        display: "flex",
        alignItems: "center",
        gap: 8,
      }}>
        <RVAuthor author={item.author} />
        <div style={{ display: "flex", flexDirection: "column", gap: 0, minWidth: 0 }}>
          <span style={{
            fontSize: 11,
            fontWeight: 700,
            color: "var(--color-text-primary)",
            letterSpacing: "-0.003em",
            lineHeight: 1.2,
          }}>{item.author.name}</span>
          <span style={{
            fontSize: 9,
            letterSpacing: "0.1em",
            textTransform: "uppercase",
            color: "var(--color-text-muted)",
            fontWeight: 600,
          }}>{item.author.role}</span>
        </div>
      </div>

      <div style={{
        fontFamily: "var(--font-family-sans)",
        fontSize: 12.5,
        lineHeight: "17px",
        color: "var(--color-text-primary)",
        letterSpacing: "-0.003em",
      }}>{item.text}</div>

      <div style={{
        display: "flex",
        alignItems: "center",
        justifyContent: "space-between",
        gap: 8,
        paddingTop: 4,
        borderTop: "1px dashed var(--color-border-subtle)",
      }}>
        <RVVoteMeter baseVotes={item.baseVotes} playerVoted={playerVoted} colDef={colDef} />
        <span style={{
          fontSize: 10,
          fontWeight: 700,
          letterSpacing: "0.1em",
          textTransform: "uppercase",
          color: playerVoted ? "var(--color-brand-primary)" : "var(--color-text-muted)",
        }}>
          {playerVoted ? "filed" : !locked && canVote ? "tap to file" : ""}
        </span>
      </div>
    </button>
  );
}

// ─────────────────────────────────────────────────────────────
// Tab switcher
// ─────────────────────────────────────────────────────────────
function RVTabs({ active, onChange, items, votesByCol }) {
  return (
    <div style={{
      display: "grid",
      gridTemplateColumns: "1fr 1fr 1fr",
      gap: 4,
      padding: "8px 16px 6px",
      background: "var(--color-surface-default)",
      borderBottom: "1px solid var(--color-border-subtle)",
    }}>
      {window.RETRO_COLUMNS.map((col) => {
        const isActive = col.id === active;
        const itemCount = (items[col.id] || []).length;
        const playerVotes = votesByCol[col.id] || 0;
        return (
          <button
            key={col.id}
            onClick={() => onChange(col.id)}
            style={{
              position: "relative",
              padding: "8px 6px 8px",
              background: isActive ? col.bg : "var(--color-surface-default)",
              border: `1px solid ${isActive ? col.border : "var(--color-border-subtle)"}`,
              borderRadius: "var(--radius-sm)",
              cursor: "pointer",
              fontFamily: "var(--font-family-sans)",
              font: "inherit",
              color: "inherit",
              textAlign: "left",
              display: "flex",
              flexDirection: "column",
              gap: 1,
              transition: "background var(--motion-base) var(--ease-standard), border-color var(--motion-base) var(--ease-standard)",
            }}
          >
            <span style={{
              fontSize: 9,
              letterSpacing: "0.1em",
              textTransform: "uppercase",
              fontWeight: 700,
              color: col.fg,
              lineHeight: 1.1,
            }}>{col.label}</span>
            <div style={{ display: "flex", alignItems: "baseline", gap: 4 }}>
              <span style={{
                fontFamily: "var(--font-family-sans)",
                fontWeight: 700,
                fontSize: 14,
                color: "var(--color-text-primary)",
                fontVariantNumeric: "tabular-nums",
                letterSpacing: "-0.005em",
              }}>{itemCount}</span>
              {playerVotes > 0 && (
                <span style={{
                  fontSize: 9,
                  fontWeight: 700,
                  color: "var(--color-brand-primary)",
                  letterSpacing: "0.04em",
                }}>+{playerVotes}</span>
              )}
            </div>
            {isActive && (
              <div aria-hidden style={{
                position: "absolute",
                bottom: -7,
                left: 8, right: 8,
                height: 3,
                borderRadius: 3,
                background: col.fg,
              }} />
            )}
          </button>
        );
      })}
    </div>
  );
}

// ─────────────────────────────────────────────────────────────
// Dots-remaining pill + sprint goal
// ─────────────────────────────────────────────────────────────
function RVHeader({ sgGoal, dotsLeft, dotsTotal, onBack }) {
  return (
    <div style={{
      padding: "6px 16px 8px",
      background: "var(--color-surface-default)",
      display: "flex",
      flexDirection: "column",
      gap: 6,
    }}>
      <button
        onClick={onBack}
        style={{
          alignSelf: "flex-start",
          background: "none",
          border: "none",
          padding: 0,
          color: "var(--color-text-link)",
          fontFamily: "var(--font-family-sans)",
          fontSize: 12,
          fontWeight: 500,
          cursor: "pointer",
          display: "inline-flex",
          alignItems: "center",
          gap: 4,
        }}
      >
        <svg width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round">
          <polyline points="15 18 9 12 15 6" />
        </svg>
        Back
      </button>
      <div style={{
        display: "flex",
        alignItems: "center",
        justifyContent: "space-between",
        gap: 12,
      }}>
        <div>
          <div style={{
            fontSize: 9,
            letterSpacing: "0.14em",
            textTransform: "uppercase",
            color: "var(--color-text-muted)",
            fontWeight: 700,
          }}>
            Process review
          </div>
          <div style={{
            fontSize: 12,
            color: "var(--color-text-secondary)",
            overflow: "hidden",
            textOverflow: "ellipsis",
            whiteSpace: "nowrap",
            maxWidth: 220,
          }}>{sgGoal}</div>
        </div>
        <div style={{
          display: "flex",
          alignItems: "center",
          gap: 6,
          background: "var(--color-surface-promo)",
          padding: "5px 11px 5px 8px",
          borderRadius: "var(--radius-pill)",
          border: "1px solid rgba(196,31,62,0.20)",
        }}>
          <span style={{ display: "flex", gap: 2 }}>
            {Array.from({ length: dotsTotal }).map((_, i) => (
              <span key={i} style={{
                width: 7, height: 7, borderRadius: "50%",
                background: i < dotsTotal - dotsLeft ? "var(--color-border-subtle)" : "var(--color-brand-primary)",
                transition: "background var(--motion-base) var(--ease-standard)",
              }} />
            ))}
          </span>
          <span style={{
            fontFamily: "var(--font-family-sans)",
            fontWeight: 700,
            fontSize: 13,
            color: "var(--color-brand-mark)",
            fontVariantNumeric: "tabular-nums",
            letterSpacing: "-0.005em",
          }}>{dotsLeft}</span>
          <span style={{
            fontSize: 9,
            letterSpacing: "0.12em",
            textTransform: "uppercase",
            fontWeight: 700,
            color: "var(--color-brand-mark)",
          }}>left</span>
        </div>
      </div>
    </div>
  );
}

// ─────────────────────────────────────────────────────────────
// Main board
// ─────────────────────────────────────────────────────────────
function RetroVoting({ items, sgGoal, onLock, onBack }) {
  const [activeTab, setActiveTab] = useStateRV("not_well");
  const [votes, setVotes] = useStateRV({});  // { itemId: true }
  const [toast, setToast] = useStateRV(null);

  const totalVotes = window.RETRO_TOTAL_VOTES;
  const dotsSpent = Object.values(votes).filter(Boolean).length;
  const dotsLeft = totalVotes - dotsSpent;

  const votesByCol = useMemoRV(() => {
    const out = { went_well: 0, not_well: 0, actions: 0 };
    window.RETRO_COLUMNS.forEach((col) => {
      out[col.id] = (items[col.id] || []).filter((it) => votes[it.id]).length;
    });
    return out;
  }, [items, votes]);

  const flashToast = (text) => {
    setToast(text);
    setTimeout(() => setToast(null), 1600);
  };

  const onToggle = (itemId) => {
    const already = votes[itemId];
    if (!already && dotsLeft <= 0) {
      flashToast("All filed — tap a filed one to swap.");
      return;
    }
    setVotes((v) => {
      const next = { ...v };
      if (already) delete next[itemId]; else next[itemId] = true;
      return next;
    });
  };

  const activeCol = window.RETRO_COLUMN_BY_ID[activeTab];
  const visibleItems = useMemoRV(() => {
    const arr = [...(items[activeTab] || [])];
    arr.sort((a, b) => {
      const aV = a.baseVotes + (votes[a.id] ? 1 : 0);
      const bV = b.baseVotes + (votes[b.id] ? 1 : 0);
      return bV - aV;
    });
    return arr;
  }, [items, activeTab, votes]);

  const canLock = dotsSpent > 0;

  return (
    <div style={{
      display: "flex",
      flexDirection: "column",
      height: "100%",
      boxSizing: "border-box",
    }}>
      <RVHeader sgGoal={sgGoal} dotsLeft={dotsLeft} dotsTotal={totalVotes} onBack={onBack} />
      <RVTabs active={activeTab} onChange={setActiveTab} items={items} votesByCol={votesByCol} />

      <div style={{
        flex: 1,
        minHeight: 0,
        overflowY: "auto",
        padding: "12px 16px 12px",
        background: activeCol.bg,
        display: "flex",
        flexDirection: "column",
        gap: 10,
      }}>
        <div style={{
          fontSize: 11,
          lineHeight: "16px",
          color: "var(--color-text-secondary)",
          padding: "0 2px",
        }}>
          {activeCol.sub}. Tap the observation to file it.
        </div>
        {visibleItems.map((item) => (
          <RVStickyNote
            key={item.id}
            item={item}
            colDef={activeCol}
            playerVoted={!!votes[item.id]}
            canVote={dotsLeft > 0}
            onToggle={onToggle}
          />
        ))}
      </div>

      <div style={{
        borderTop: "1px solid var(--color-border-subtle)",
        background: "var(--color-surface-default)",
        padding: "10px 20px 14px",
        display: "flex",
        flexDirection: "column",
        gap: 6,
      }}>
        <div style={{
          minHeight: 16,
          fontSize: 11,
          textAlign: "center",
          color: toast ? "var(--color-danger)" : "var(--color-text-muted)",
          fontWeight: toast ? 600 : 400,
        }}>
          {toast || (dotsLeft <= 0 ? "All filed. Lock it in, or swap one out." : `${dotsLeft} of ${totalVotes} to file — pick what to continue, start or stop.`)}
        </div>
        <button
          onClick={() => onLock(votes)}
          disabled={!canLock}
          style={{
            height: 46,
            background: canLock ? "var(--color-brand-primary)" : "var(--color-surface-subtle)",
            color: canLock ? "var(--color-text-on-brand)" : "var(--color-text-muted)",
            border: "none",
            borderRadius: "var(--radius-sm)",
            fontFamily: "var(--font-family-sans)",
            fontWeight: 600,
            fontSize: 15,
            cursor: canLock ? "pointer" : "not-allowed",
            display: "inline-flex",
            alignItems: "center",
            justifyContent: "center",
            gap: 8,
          }}
        >
          File to knowledge store
          {canLock && (
            <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round">
              <polyline points="9 18 15 12 9 6" />
            </svg>
          )}
        </button>
      </div>
    </div>
  );
}

Object.assign(window, { RetroVoting });
