<template>
  <div class="board">
    <!-- 4 video call tiles. Only CFO is active at stage 0; others join at stage >= 1. -->
    <div class="tiles">
      <div v-for="(p, i) in people" :key="p.id"
           class="tile"
           :class="{ active: i === 0 || stage >= 1, deepfake: stage >= 3 }"
           :style="i > 0 ? { animationDelay: `${(i - 1) * 80}ms` } : {}">
        <div class="live"><span class="rec"></span>LIVE</div>
        <svg class="silhouette" viewBox="0 0 60 60" aria-hidden="true">
          <circle cx="30" cy="22" r="10" />
          <path d="M 6 60 Q 6 38, 30 38 Q 54 38, 54 60 Z" />
        </svg>
        <div class="name">{{ p.name }}</div>
      </div>
    </div>

    <!-- Speech + reply -->
    <div class="chat">
      <div class="speech" :class="{ visible: stage >= 1 }">
        <span class="who">CFO</span>
        <span class="words">&ldquo;Approve the $25M transfer.&rdquo;</span>
      </div>
      <div class="reply" :class="{ visible: stage >= 2 }">
        &rarr; OK.
      </div>
    </div>

    <!-- Ledger -->
    <div class="ledger" :class="{ visible: stage >= 2 }">
      <div class="lines">
        <div
          v-for="(amount, i) in shownTransfers"
          :key="`${transferIdx}-${i}`"
          class="line"
        >
          $ {{ amount.toLocaleString('en-US') }}
        </div>
      </div>
      <div class="total-row">
        <span class="total-label">TOTAL SENT</span>
        <span class="total-value">$ {{ total.toLocaleString('en-US') }}</span>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, watch, onBeforeUnmount } from 'vue'
import { useIsSlideActive } from '@slidev/client'

const props = withDefaults(defineProps<{
  stage?: number
  amounts?: number[]
  intervalMs?: number
}>(), {
  stage: 0,
  // 15 transfers summing to exactly $25,000,000.
  amounts: () => [
    800_000, 1_200_000, 1_600_000, 2_400_000, 1_800_000,
    3_200_000, 2_700_000, 2_100_000, 1_500_000, 1_300_000,
    900_000, 1_100_000, 1_400_000, 1_700_000, 1_300_000,
  ],
  intervalMs: 430,
})

const people = [
  { id: 0, name: 'CFO' },
  { id: 1, name: 'DIR. FINANCE' },
  { id: 2, name: 'DIR. OPS' },
  { id: 3, name: 'DIR. TREASURY' },
]

const transferIdx = ref(0)
const total = ref(0)

const VISIBLE_LINES = 5
const shownTransfers = computed(() =>
  props.amounts.slice(0, transferIdx.value).slice(-VISIBLE_LINES)
)

let stepTimer: ReturnType<typeof setInterval> | null = null

function stepTransfer() {
  if (transferIdx.value >= props.amounts.length) {
    if (stepTimer) { clearInterval(stepTimer); stepTimer = null }
    return
  }
  total.value += props.amounts[transferIdx.value]
  transferIdx.value += 1
}

function startLedger() {
  if (stepTimer) clearInterval(stepTimer)
  transferIdx.value = 0
  total.value = 0
  stepTransfer()
  stepTimer = setInterval(stepTransfer, props.intervalMs)
}

function stopLedger() {
  if (stepTimer) { clearInterval(stepTimer); stepTimer = null }
  transferIdx.value = 0
  total.value = 0
}

// Start/stop ledger based on stage prop driven by $clicks.
watch(() => props.stage ?? 0, (newStage, oldStage = 0) => {
  if (newStage >= 2 && oldStage < 2) startLedger()
  else if (newStage < 2 && oldStage >= 2) stopLedger()
})

// Reset when the slide becomes inactive; re-start if we re-enter at stage >= 2.
const isActive = useIsSlideActive()
watch(isActive, (active) => {
  if (!active) {
    stopLedger()
  } else if ((props.stage ?? 0) >= 2) {
    startLedger()
  }
}, { immediate: true })

onBeforeUnmount(() => {
  if (stepTimer) clearInterval(stepTimer)
})
</script>

<style scoped>
.board {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1.2rem;
  width: 100%;
}

/* ---- Video tiles ---- */
.tiles {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.7rem;
}
.tile {
  position: relative;
  width: 8.5rem;
  height: 5.5rem;
  background: #0c0c10;
  border: 1px solid rgba(255, 255, 255, 0.13);
  border-radius: 4px;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition:
    background 0.55s ease,
    border-color 0.55s ease,
    box-shadow 0.55s ease;
}
.tile.active {
  opacity: 1;
  animation: tile-fade 0.42s ease forwards;
}
.tile.deepfake {
  border-color: rgba(255, 90, 90, 0.6);
  background: rgba(40, 10, 12, 1);
  animation: deepfake-flash 0.85s ease forwards;
}
.tile.deepfake .silhouette {
  fill: #ff5a5a;
  filter: drop-shadow(0 0 4px rgba(255, 90, 90, 0.45));
}
.tile .silhouette {
  transition: fill 0.55s ease, filter 0.55s ease;
}
.tile::after {
  /* faint scanlines so it reads as 'video' */
  content: '';
  position: absolute;
  inset: 0;
  background:
    repeating-linear-gradient(
      0deg,
      rgba(255, 255, 255, 0.022) 0 1px,
      transparent 1px 3px
    );
  pointer-events: none;
}
.tile .silhouette {
  width: 55%;
  height: 80%;
  fill: rgba(255, 255, 255, 0.22);
}
.tile .name {
  position: absolute;
  left: 0.5rem;
  bottom: 0.4rem;
  font-family: 'Inter', system-ui, sans-serif;
  font-size: 0.45rem;
  font-weight: 500;
  letter-spacing: 0.28em;
  color: rgba(255, 255, 255, 0.68);
  text-transform: uppercase;
}
.tile .live {
  position: absolute;
  top: 0.4rem;
  right: 0.5rem;
  font-family: 'Inter', system-ui, sans-serif;
  font-size: 0.42rem;
  font-weight: 500;
  letter-spacing: 0.28em;
  color: rgba(255, 100, 100, 0.85);
  text-transform: uppercase;
  display: flex;
  align-items: center;
  gap: 0.3rem;
}
.tile .live .rec {
  display: inline-block;
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: #ff5050;
  box-shadow: 0 0 6px rgba(255, 80, 80, 0.7);
  animation: blink 1.6s ease-in-out infinite;
}

/* ---- Chat ---- */
.chat {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.35rem;
  min-height: 3rem;
}
.speech, .reply {
  opacity: 0;
  transition: opacity 0.5s ease;
}
.speech.visible, .reply.visible { opacity: 1; }
/* Speech lands after the three new tiles finish fading in (~0.5s into the click). */
.speech.visible { transition-delay: 0.55s; }
.speech {
  font-family: 'DM Serif Display', serif;
  font-style: italic;
  font-size: 1.15rem;
  color: #ffffff;
}
.speech .who {
  font-family: 'Inter', system-ui, sans-serif;
  font-style: normal;
  font-size: 0.55rem;
  font-weight: 500;
  letter-spacing: 0.35em;
  text-transform: uppercase;
  color: rgba(255, 255, 255, 0.55);
  margin-right: 0.7rem;
  vertical-align: 0.15em;
}
.reply {
  font-family: 'DM Serif Display', serif;
  font-style: italic;
  font-size: 0.9rem;
  color: rgba(255, 255, 255, 0.62);
}

/* ---- Ledger ---- */
.ledger {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.6rem;
  min-width: 16rem;
  opacity: 0;
  transition: opacity 0.4s ease;
}
.ledger.visible { opacity: 1; }

.lines {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  font-family: 'JetBrains Mono', ui-monospace, monospace;
  font-size: 0.85rem;
  color: #ff5a5a;
  text-shadow: 0 0 12px rgba(255, 90, 90, 0.22);
  height: 5.2rem;
  overflow: hidden;
  gap: 0.1rem;
  width: 19rem;
  mask-image: linear-gradient(to bottom, transparent 0%, #000 35%, #000 100%);
  -webkit-mask-image: linear-gradient(to bottom, transparent 0%, #000 35%, #000 100%);
}
.line {
  font-variant-numeric: tabular-nums;
  animation: line-in 0.28s ease both;
}

.total-row {
  display: flex;
  align-items: baseline;
  gap: 0.9rem;
  padding-top: 0.5rem;
  border-top: 1px solid rgba(255, 90, 90, 0.35);
  width: 19rem;
  justify-content: space-between;
}
.total-label {
  font-family: 'Inter', system-ui, sans-serif;
  font-size: 0.55rem;
  font-weight: 500;
  letter-spacing: 0.4em;
  text-transform: uppercase;
  color: rgba(255, 255, 255, 0.55);
}
.total-value {
  font-family: 'JetBrains Mono', ui-monospace, monospace;
  font-size: 1.6rem;
  color: #ff5a5a;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
  text-shadow: 0 0 18px rgba(255, 90, 90, 0.45);
  white-space: nowrap;
}

@keyframes tile-fade {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}
@keyframes deepfake-flash {
  0%   { box-shadow: 0 0 0 rgba(255, 90, 90, 0); }
  30%  { box-shadow: 0 0 28px rgba(255, 90, 90, 0.7); }
  100% { box-shadow: 0 0 14px rgba(255, 90, 90, 0.25); }
}
@keyframes blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.35; }
}
@keyframes line-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}
</style>
