#!/bin/bash
# POI Cost Dashboard Integration Deployment Checklist
# Run this to verify all components are properly integrated

set -e

echo "🔍 POI Cost Dashboard Integration Verification"
echo "=============================================="
echo ""

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

# Check 1: Backend files exist
echo "1️⃣  Checking backend files..."
FILES=(
  "apps/api/src/modules/ai/services/cost-tracking.service.ts"
  "apps/api/src/modules/ai/integrations/poi-cost-dashboard-integration.ts"
  "apps/api/src/modules/ai/controllers/poi-cost-dashboard.controller.ts"
  "apps/api/src/modules/ai/controllers/poi.controller.ts"
)

for file in "${FILES[@]}"; do
  if [ -f "$file" ]; then
    echo -e "${GREEN}✓${NC} $file"
  else
    echo -e "${RED}✗${NC} $file (MISSING)"
    exit 1
  fi
done

echo ""

# Check 2: Frontend files exist
echo "2️⃣  Checking frontend files..."
FILES=(
  "apps/web/src/modules/ai/services/poiCostDashboardService.ts"
  "apps/web/src/modules/admin/composables/usePOICostIntegration.ts"
  "apps/web/src/modules/admin/components/CostDashboard.vue"
)

for file in "${FILES[@]}"; do
  if [ -f "$file" ]; then
    echo -e "${GREEN}✓${NC} $file"
  else
    echo -e "${RED}✗${NC} $file (MISSING)"
    exit 1
  fi
done

echo ""

# Check 3: Routes are registered
echo "3️⃣  Checking route registration..."
if grep -q "costs/pois/dashboard" apps/api/src/modules/ai/ai.routes.ts; then
  echo -e "${GREEN}✓${NC} Routes registered in ai.routes.ts"
else
  echo -e "${RED}✗${NC} Routes NOT registered in ai.routes.ts"
  exit 1
fi

echo ""

# Check 4: POI controller has cost tracking
echo "4️⃣  Checking POI controller integration..."
if grep -q "recordPOICost" apps/api/src/modules/ai/controllers/poi.controller.ts; then
  echo -e "${GREEN}✓${NC} Cost tracking integrated in poi.controller.ts"
else
  echo -e "${RED}✗${NC} Cost tracking NOT integrated in poi.controller.ts"
  exit 1
fi

echo ""

# Check 5: CostDashboard has POI providers
echo "5️⃣  Checking CostDashboard component..."
if grep -q "overpass" apps/web/src/modules/admin/components/CostDashboard.vue && \
   grep -q "wikipedia" apps/web/src/modules/admin/components/CostDashboard.vue; then
  echo -e "${GREEN}✓${NC} POI providers added to CostDashboard"
else
  echo -e "${RED}✗${NC} POI providers NOT added to CostDashboard"
  exit 1
fi

echo ""

# Check 6: Documentation exists
echo "6️⃣  Checking documentation..."
if [ -f "docs/features/ai/poi-cost-dashboard-integration.md" ]; then
  echo -e "${GREEN}✓${NC} Integration documentation exists"
else
  echo -e "${YELLOW}⚠${NC}  Integration documentation missing (non-critical)"
fi

echo ""

# Check 7: Environment variables
echo "7️⃣  Checking environment variables..."
if [ -z "$POI_API_BUDGET" ]; then
  echo -e "${YELLOW}⚠${NC}  POI_API_BUDGET not set (will default to $10)"
else
  echo -e "${GREEN}✓${NC} POI_API_BUDGET = $POI_API_BUDGET"
fi

if [ -z "$MAPBOX_TOKEN" ]; then
  echo -e "${RED}✗${NC} MAPBOX_TOKEN not configured"
else
  echo -e "${GREEN}✓${NC} MAPBOX_TOKEN configured"
fi

if [ -z "$GOOGLE_PLACES_TOKEN" ]; then
  echo -e "${RED}✗${NC} GOOGLE_PLACES_TOKEN not configured"
else
  echo -e "${GREEN}✓${NC} GOOGLE_PLACES_TOKEN configured"
fi

echo ""

# Check 8: Type checking
echo "8️⃣  Running type check..."
if pnpm type-check > /dev/null 2>&1; then
  echo -e "${GREEN}✓${NC} Type check passed"
else
  echo -e "${RED}✗${NC} Type check failed"
  echo "  Run: pnpm type-check"
  exit 1
fi

echo ""

# Check 9: Tests
echo "9️⃣  Checking test file..."
if [ -f "apps/api/src/modules/ai/__tests__/poi-cost-dashboard-integration.test.ts" ]; then
  echo -e "${GREEN}✓${NC} Integration tests exist"
  echo ""
  echo "  To run tests:"
  echo "    pnpm test --run apps/api/src/modules/ai/__tests__/poi-cost-dashboard-integration.test.ts"
else
  echo -e "${YELLOW}⚠${NC}  Test file missing (non-critical)"
fi

echo ""
echo "=============================================="
echo -e "${GREEN}✅ All checks passed!${NC}"
echo ""
echo "🚀 Next steps:"
echo "  1. Deploy code: pnpm build && pnpm deploy"
echo "  2. Start API: pnpm dev:api"
echo "  3. Start Web: pnpm dev:web"
echo "  4. Open admin dashboard: http://localhost:3000/admin"
echo "  5. Check Cost Optimization tab for POI cost data"
echo ""
echo "📊 Monitoring:"
echo "  • POI costs appear in dashboard under 'Cost by Provider'"
echo "  • Budget status shows remaining daily budget"
echo "  • Alerts appear when approaching limits"
echo "  • Top users show highest-cost POI requesters"
echo ""
echo "🔧 Debug commands:"
echo "  # Check POI costs endpoint"
echo "  curl http://localhost:3001/api/ai/costs/pois/dashboard"
echo ""
echo "  # Check budget status"
echo "  curl http://localhost:3001/api/ai/costs/pois/budget"
echo ""
echo "  # View cost tracking logs"
echo "  grep 'POI cost' logs/api.log"
