#!/bin/bash

# Momentry Production Monitoring Dashboard
# Simple CLI dashboard for monitoring production deployment

API_KEY="muser_29dd336ea8d44b9badbc650d503b0348_1774620247_b098ff47"
API_URL="http://localhost:3002"

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

# Header
echo -e "${BLUE}╔══════════════════════════════════════════════════════════════╗${NC}"
echo -e "${BLUE}║              MOMENTRY PRODUCTION MONITORING                  ║${NC}"
echo -e "${BLUE}╠══════════════════════════════════════════════════════════════╣${NC}"

# Health Check
echo -e "${YELLOW}📊 System Health:${NC}"
health_response=$(curl -s -H "X-API-Key: $API_KEY" "$API_URL/health")
if [ $? -eq 0 ]; then
	status=$(echo "$health_response" | jq -r '.status')
	version=$(echo "$health_response" | jq -r '.version')
	uptime_ms=$(echo "$health_response" | jq -r '.uptime_ms')
	uptime_sec=$((uptime_ms / 1000))
	uptime_min=$((uptime_sec / 60))
	uptime_hr=$((uptime_min / 60))

	echo -e "  Status: ${GREEN}$status${NC}"
	echo -e "  Version: $version"
	echo -e "  Uptime: ${uptime_hr}h ${uptime_min%60}m ${uptime_sec%60}s"
else
	echo -e "  Status: ${RED}API Unreachable${NC}"
fi

# Videos Count
echo -e "\n${YELLOW}🎬 Video Assets:${NC}"
videos_response=$(curl -s -H "X-API-Key: $API_KEY" "$API_URL/api/v1/videos")
if [ $? -eq 0 ]; then
	video_count=$(echo "$videos_response" | jq -r '.videos | length')
	echo -e "  Total Videos: ${GREEN}$video_count${NC}"

	# Show recent videos
	echo -e "  Recent Videos:"
	echo "$videos_response" | jq -r '.videos[-3:] | .[] | "    - \(.file_name) (\(.duration | floor)s)"'
else
	echo -e "  ${RED}Failed to fetch videos${NC}"
fi

# System Status
echo -e "\n${YELLOW}⚙️  System Resources:${NC}"
system_status=$(cd /Users/accusys/momentry_core_0.1 && export QDRANT_URL=http://localhost:6333 && export QDRANT_API_KEY=Test3200Test3200Test3200 && export QDRANT_COLLECTION=chunks_v3 && cargo run --bin momentry -- system 2>/dev/null | tail -10)
if [ $? -eq 0 ]; then
	echo "$system_status"
else
	echo -e "  ${RED}Failed to get system status${NC}"
fi

# Service Status
echo -e "\n${YELLOW}🔧 Service Status:${NC}"
services=("postgresql@18" "redis" "mariadb" "mongodb" "qdrant" "caddy" "gitea" "sftpgo" "php-fpm" "n8n")
for service in "${services[@]}"; do
	if brew services list | grep -q "$service.*started"; then
		echo -e "  $service: ${GREEN}✓ Running${NC}"
	else
		echo -e "  $service: ${RED}✗ Stopped${NC}"
	fi
done

# Momentry Processes
echo -e "\n${YELLOW}🚀 Momentry Processes:${NC}"
momentry_procs=$(ps aux | grep momentry | grep -v grep | grep -v "monitor_dashboard")
if [ -n "$momentry_procs" ]; then
	echo "$momentry_procs" | while read line; do
		proc_name=$(echo "$line" | awk '{print $11, $12}')
		echo -e "  ${GREEN}✓${NC} $proc_name"
	done
else
	echo -e "  ${RED}No Momentry processes found${NC}"
fi

# Footer
echo -e "${BLUE}╠══════════════════════════════════════════════════════════════╣${NC}"
echo -e "${BLUE}║  API Endpoint: $API_URL                                    ║${NC}"
echo -e "${BLUE}║  API Key: ${API_KEY:0:20}...                              ║${NC}"
echo -e "${BLUE}╚══════════════════════════════════════════════════════════════╝${NC}"
echo -e "\n${YELLOW}📈 Monitoring Commands:${NC}"
echo -e "  Run './monitor_dashboard.sh' to refresh"
echo -e "  View logs: tail -f /Users/accusys/momentry/log/momentry_api.log"
echo -e "  System status: cargo run --bin momentry -- system"
echo -e "  API test: curl -H \"X-API-Key: \$API_KEY\" $API_URL/health"
