#!/bin/bash

# Quick script to check Strapi CMS status and troubleshoot 503 errors

echo "=========================================="
echo "Strapi CMS Status Check"
echo "=========================================="
echo ""

# Check PM2 status
echo "1. Checking PM2 status..."
if command -v pm2 &> /dev/null; then
    pm2 list
    echo ""
    pm2 logs nrel-cms --lines 20 --nostream
else
    echo "PM2 not found"
fi

echo ""
echo "2. Checking if port 1337 is in use..."
netstat -tlnp | grep 1337 || ss -tlnp | grep 1337 || echo "Port 1337 not in use"

echo ""
echo "3. Checking Meilisearch connection..."
if curl -s http://127.0.0.1:7700/health > /dev/null; then
    echo "✓ Meilisearch is running"
else
    echo "✗ Meilisearch is not responding"
fi

echo ""
echo "4. Checking Strapi process..."
ps aux | grep -i strapi | grep -v grep || echo "No Strapi process found"

echo ""
echo "=========================================="
echo "Quick Fix Commands:"
echo "=========================================="
echo ""
echo "If Strapi is not running:"
echo "  cd /path/to/nrel-cms"
echo "  pm2 restart nrel-cms"
echo ""
echo "If Meilisearch connection is failing, temporarily disable it:"
echo "  Comment out MEILISEARCH_HOST and MEILISEARCH_MASTER_KEY in .env"
echo "  Then restart: pm2 restart nrel-cms"
echo ""

