#!/bin/bash
# PunimTag Demo Helper Script
# Makes running demo commands easier

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

echo -e "${BLUE}🎬 PunimTag Demo Helper${NC}"
echo "=================================="

# Check if virtual environment exists
if [ ! -d "venv" ]; then
    echo -e "${RED}❌ Virtual environment not found!${NC}"
    echo "Run: python3 -m venv venv && source venv/bin/activate && python3 setup.py"
    exit 1
fi

# Activate virtual environment
source venv/bin/activate

# Check if demo photos exist
photo_count=$(find demo_photos/ -name "*.jpg" -o -name "*.png" -o -name "*.jpeg" 2>/dev/null | wc -l)
if [ "$photo_count" -eq 0 ]; then
    echo -e "${YELLOW}⚠️  No demo photos found!${NC}"
    echo "Please add photos to demo_photos/ folders first."
    echo "See: demo_photos/DEMO_INSTRUCTIONS.md"
    echo ""
    echo "Demo folder structure:"
    ls -la demo_photos/
    exit 1
fi

echo -e "${GREEN}✅ Found $photo_count demo photos${NC}"

# Function to run demo commands with explanations
demo_command() {
    echo ""
    echo -e "${YELLOW}📋 $1${NC}"
    echo "Command: $2"
    echo "Press Enter to run..."
    read
    echo -e "${BLUE}Running: $2${NC}"
    eval $2
    echo ""
}

# Demo workflow
echo ""
echo "🎯 Enhanced Demo Workflow:"
echo "1. Clean demo database"
echo "2. Scan photos"
echo "3. Process for faces"
echo "4. Show statistics"
echo "5. Identify faces with visual display (ENHANCED!)"
echo "6. Auto-match faces across photos (NEW!)"
echo "7. Search for people"
echo "8. Show verbose modes"
echo ""

# Clean demo database
demo_command "Clean previous demo data" "rm -f demo.db"

# Scan photos
demo_command "Scan photos with verbose output" "python3 photo_tagger.py scan demo_photos --recursive --db demo.db -v"

# Process faces
demo_command "Process photos to find faces" "python3 photo_tagger.py process --db demo.db -v"

# Show stats
demo_command "Show database statistics" "python3 photo_tagger.py stats --db demo.db"

# Enhanced visual identification
echo -e "${YELLOW}📋 Enhanced face identification with visual display${NC}"
echo "Command: python3 photo_tagger.py identify --show-faces --batch 3 --db demo.db"
echo "Press Enter to run (you'll see individual face crops!)..."
read
echo -e "${BLUE}Running: python3 photo_tagger.py identify --show-faces --batch 3 --db demo.db${NC}"
python3 photo_tagger.py identify --show-faces --batch 3 --db demo.db

# Smart auto-matching
echo ""
echo -e "${YELLOW}📋 Smart auto-matching across photos${NC}"
echo "Command: python3 photo_tagger.py auto-match --show-faces --db demo.db"
echo "Press Enter to run (you'll see side-by-side face comparisons!)..."
read
echo -e "${BLUE}Running: python3 photo_tagger.py auto-match --show-faces --db demo.db${NC}"
python3 photo_tagger.py auto-match --show-faces --db demo.db

# Search for people
echo ""
echo -e "${YELLOW}📋 Search for identified people${NC}"
echo "Available people in database:"
python3 -c "
import sqlite3
conn = sqlite3.connect('demo.db')
cursor = conn.cursor()
cursor.execute('SELECT DISTINCT name FROM people')
people = cursor.fetchall()
for person in people:
    print(f'  - {person[0]}')
conn.close()
"

if [ $? -eq 0 ]; then
    echo ""
    echo "Enter a person's name to search for:"
    read person_name
    if [ ! -z "$person_name" ]; then
        echo -e "${BLUE}Running: python3 photo_tagger.py search \"$person_name\" --db demo.db${NC}"
        python3 photo_tagger.py search "$person_name" --db demo.db
    fi
fi

# Verbose modes demo
echo ""
echo -e "${YELLOW}📋 Verbose modes demonstration${NC}"
echo "Let's see different verbosity levels..."

# Reset a photo for verbose demo
python3 -c "
import sqlite3
conn = sqlite3.connect('demo.db')
conn.execute('UPDATE photos SET processed = 0 WHERE id = 1')
conn.commit()
conn.close()
"

demo_command "Quiet mode (default)" "python3 photo_tagger.py process --limit 1 --db demo.db"

# Reset photo
python3 -c "
import sqlite3
conn = sqlite3.connect('demo.db')
conn.execute('UPDATE photos SET processed = 0 WHERE id = 1')
conn.commit()
conn.close()
"

demo_command "Verbose mode (-v)" "python3 photo_tagger.py process --limit 1 --db demo.db -v"

# Reset photo
python3 -c "
import sqlite3
conn = sqlite3.connect('demo.db')
conn.execute('UPDATE photos SET processed = 0 WHERE id = 1')
conn.commit()
conn.close()
"

demo_command "Very verbose mode (-vv)" "python3 photo_tagger.py process --limit 1 --db demo.db -vv"

# Reset photo
python3 -c "
import sqlite3
conn = sqlite3.connect('demo.db')
conn.execute('UPDATE photos SET processed = 0 WHERE id = 1')
conn.commit()
conn.close()
"

demo_command "Maximum verbose (-vvv)" "python3 photo_tagger.py process --limit 1 --db demo.db -vvv"

# Final stats
demo_command "Final statistics" "python3 photo_tagger.py stats --db demo.db"

echo ""
echo -e "${GREEN}🎉 Demo complete!${NC}"
echo ""
echo "📋 Summary of what we demonstrated:"
echo "  ✅ Photo scanning and indexing"
echo "  ✅ Automatic face detection"
echo "  ✅ Interactive face identification"
echo "  ✅ Person-based photo search"
echo "  ✅ Verbose output modes (-v, -vv, -vvv)"
echo "  ✅ Database statistics and reporting"
echo ""
echo "🔧 Enhanced features demonstrated:"
echo "  ✅ Visual face identification with --show-faces"
echo "  ✅ Smart cross-photo auto-matching"
echo "  ✅ Confidence-based match suggestions"
echo "  ✅ Side-by-side face comparisons"
echo "  ✅ Non-blocking image display"
echo ""
echo "🔧 Additional features to explore:"
echo "  - Custom tagging: python3 photo_tagger.py tag"
echo "  - Different models: --model cnn (more accurate)"
echo "  - Tolerance adjustment: --tolerance 0.3 (stricter) or 0.7 (lenient)"
echo "  - Twins detection: --include-twins"
echo "  - Auto mode: --auto (high-confidence auto-identification)"