#!/usr/bin/env bash
# Always start dev with a fresh .next cache — prevents missing vendor-chunks (zustand.js).
set -euo pipefail
cd "$(dirname "$0")/.."

pkill -f "next dev" 2>/dev/null || true
if lsof -ti:3000 >/dev/null 2>&1; then
  lsof -ti:3000 | xargs kill -9 2>/dev/null || true
  sleep 1
fi

rm -rf .next
echo "Starting Next.js dev server (clean cache)…"
exec npx next dev