#!/bin/bash
# Kimi Workspace Resume Script
# Quick status check and resume prompt

set -e

cd "$(dirname "$0")/../.."

echo "==============================================="
echo "  Kimi Workspace Status"
echo "==============================================="
echo ""

# Git status
echo "🌿 Git Status:"
echo "   Branch: $(git branch --show-current)"
echo "   Commit: $(git log --oneline -1)"
if [ -n "$(git status --short)" ]; then
    echo "   Uncommitted changes:"
    git status --short | sed 's/^/     /'
else
    echo "   Working directory clean"
fi
echo ""

# Test status (quick check)
echo "🧪 Test Status:"
if [ -f ".tox/unit/log/1-commands[0].log" ]; then
    LAST_TEST=$(grep -o '[0-9]* passed' .tox/unit/log/1-commands[0].log 2>/dev/null | tail -1 || echo "unknown")
    echo "   Last unit test run: $LAST_TEST"
else
    echo "   No recent test runs found"
fi
echo ""

# Check Ollama
echo "🤖 Ollama Status:"
if curl -s http://localhost:11434/api/tags &>/dev/null; then
    MODELS=$(curl -s http://localhost:11434/api/tags 2>/dev/null | grep -o '"name":"[^"]*"' | head -3 | sed 's/"name":"//;s/"$//' | tr '\n' ', ' | sed 's/, $//')
    echo "   ✅ Running (models: $MODELS)"
else
    echo "   ⚠️  Not running (start with: ollama serve)"
fi
echo ""

# Dashboard status
echo "🌐 Dashboard Status:"
if curl -s http://localhost:8000/health &>/dev/null; then
    echo "   ✅ Running at http://localhost:8000"
else
    echo "   ⚠️  Not running (start with: make dev)"
fi
echo ""

# Show TODO items
echo "📝 Next Tasks (from TODO.md):"
if [ -f ".kimi/TODO.md" ]; then
    grep -E "^\s*- \[ \]" .kimi/TODO.md 2>/dev/null | head -5 | sed 's/^/   /' || echo "   No pending tasks"
else
    echo "   No TODO.md found"
fi
echo ""

# Resume prompt
echo "==============================================="
echo "  Resume Prompt (copy/paste to Kimi):"
echo "==============================================="
echo ""
echo "cd $(pwd) && cat .kimi/CHECKPOINT.md"
echo ""
echo "Continue from checkpoint. Check .kimi/TODO.md for next tasks."
echo "Run 'make test' after changes and update CHECKPOINT.md."
echo ""
