#!/bin/bash
# =============================================================================
# Mastodon Fix/Repair Script
# Diagnoses and fixes common issues
# =============================================================================
# Run as root

echo "=========================================="
echo "Mastodon Fix/Repair Tool"
echo "=========================================="

# Check root
if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root"
   exit 1
fi

FIXED=0
ERRORS=0

# 1. Check and fix service status
echo ""
echo "[1/7] Checking services..."

services=("postgresql" "valkey" "nginx" "mastodon-web" "mastodon-sidekiq" "mastodon-streaming")
for svc in "${services[@]}"; do
    if systemctl is-active --quiet $svc 2>/dev/null; then
        echo "  ✓ $svc is running"
    elif systemctl list-unit-files | grep -q "^${svc}.service"; then
        echo "  ✗ $svc is not running, attempting to start..."
        systemctl start $svc 2>/dev/null
        sleep 2
        if systemctl is-active --quiet $svc; then
            echo "    ✓ $svc started successfully"
            FIXED=$((FIXED + 1))
        else
            echo "    ✗ Failed to start $svc"
            echo "    Check logs: journalctl -u $svc -n 50"
            ERRORS=$((ERRORS + 1))
        fi
    fi
done

# 2. Check file permissions
echo ""
echo "[2/7] Checking file permissions..."

# Check .env.production
if [ -f /home/mastodon/live/.env.production ]; then
    OWNER=$(stat -c '%U' /home/mastodon/live/.env.production)
    PERMS=$(stat -c '%a' /home/mastodon/live/.env.production)

    if [ "$OWNER" != "mastodon" ]; then
        echo "  ✗ Fixing .env.production ownership..."
        chown mastodon:mastodon /home/mastodon/live/.env.production
        FIXED=$((FIXED + 1))
    fi

    if [ "$PERMS" != "600" ]; then
        echo "  ✗ Fixing .env.production permissions..."
        chmod 600 /home/mastodon/live/.env.production
        FIXED=$((FIXED + 1))
    fi

    echo "  ✓ .env.production permissions OK"
fi

# Check live directory ownership
if [ -d /home/mastodon/live ]; then
    LIVE_OWNER=$(stat -c '%U' /home/mastodon/live)
    if [ "$LIVE_OWNER" != "mastodon" ]; then
        echo "  ✗ Fixing /home/mastodon/live ownership..."
        chown -R mastodon:mastodon /home/mastodon/live
        FIXED=$((FIXED + 1))
    else
        echo "  ✓ /home/mastodon/live ownership OK"
    fi
fi

# 3. Check database connection
echo ""
echo "[3/7] Checking database..."

if sudo -u postgres psql -c "SELECT 1" mastodon_production > /dev/null 2>&1; then
    echo "  ✓ Database connection successful"
else
    echo "  ✗ Cannot connect to database"

    # Try to fix common issues
    if ! systemctl is-active --quiet postgresql; then
        echo "    Attempting to start PostgreSQL..."
        systemctl start postgresql
        sleep 2
    fi

    # Check if database exists
    if ! sudo -u postgres psql -lqt | cut -d \| -f 1 | grep -qw mastodon_production; then
        echo "    Database does not exist!"
        ERRORS=$((ERRORS + 1))
    fi
fi

# 4. Check Redis/Valkey connection
echo ""
echo "[4/7] Checking cache server..."

if valkey-cli ping > /dev/null 2>&1; then
    echo "  ✓ Valkey connection successful"
elif redis-cli ping > /dev/null 2>&1; then
    echo "  ✓ Redis connection successful"
else
    echo "  ✗ Cannot connect to cache server"

    if systemctl is-active --quiet valkey; then
        echo "    Valkey is running but not responding"
    elif systemctl is-active --quiet redis; then
        echo "    Redis is running but not responding"
    else
        echo "    Attempting to start Valkey..."
        systemctl start valkey 2>/dev/null || systemctl start redis 2>/dev/null
        sleep 2
        FIXED=$((FIXED + 1))
    fi
fi

# 5. Check nginx configuration
echo ""
echo "[5/7] Checking nginx configuration..."

if nginx -t 2>/dev/null; then
    echo "  ✓ Nginx configuration is valid"
else
    echo "  ✗ Nginx configuration has errors"
    nginx -t
    ERRORS=$((ERRORS + 1))
fi

# 6. Check SELinux contexts (Rocky/RHEL)
echo ""
echo "[6/7] Checking SELinux..."

if command -v getenforce &> /dev/null; then
    SELINUX_MODE=$(getenforce)
    echo "  SELinux mode: $SELINUX_MODE"

    if [ "$SELINUX_MODE" = "Enforcing" ]; then
        # Fix common SELinux issues
        if [ -d /home/mastodon/live/public ]; then
            echo "  Ensuring correct SELinux contexts..."
            chcon -R -t httpd_sys_content_t /home/mastodon/live/public 2>/dev/null || true
        fi
    fi
else
    echo "  SELinux not present"
fi

# 7. Check API endpoints
echo ""
echo "[7/7] Checking API endpoints..."

sleep 1

# Test instance API
if curl -sf http://127.0.0.1:3000/api/v1/instance > /dev/null 2>&1; then
    echo "  ✓ Instance API responding"
else
    echo "  ✗ Instance API not responding"

    # Check if it's a startup timing issue
    echo "    Waiting for services to fully start..."
    sleep 5

    if curl -sf http://127.0.0.1:3000/api/v1/instance > /dev/null 2>&1; then
        echo "    ✓ Instance API now responding"
    else
        echo "    ✗ Instance API still not responding"
        echo "    Check logs: journalctl -u mastodon-web -n 50"
        ERRORS=$((ERRORS + 1))
    fi
fi

# Test streaming API
if curl -sf http://127.0.0.1:4000/api/v1/streaming/health > /dev/null 2>&1; then
    echo "  ✓ Streaming API healthy"
else
    echo "  ✗ Streaming API not responding"
    echo "    Attempting to restart streaming service..."
    systemctl restart mastodon-streaming
    sleep 3
    if curl -sf http://127.0.0.1:4000/api/v1/streaming/health > /dev/null 2>&1; then
        echo "    ✓ Streaming API now healthy"
        FIXED=$((FIXED + 1))
    else
        echo "    ✗ Streaming API still not responding"
        ERRORS=$((ERRORS + 1))
    fi
fi

# Summary
echo ""
echo "=========================================="
if [ $ERRORS -eq 0 ]; then
    if [ $FIXED -eq 0 ]; then
        echo "✅ All checks passed! No issues found."
    else
        echo "✅ Fixed $FIXED issue(s). All checks now pass."
        echo ""
        echo "You may want to restart services:"
        echo "  systemctl restart mastodon-web mastodon-sidekiq mastodon-streaming"
    fi
else
    echo "⚠️  Found $ERRORS error(s) that need manual attention."
    echo ""
    echo "Common fixes:"
    echo "  - Check logs: journalctl -u mastodon-web -f"
    echo "  - Restart all: systemctl restart mastodon-{web,sidekiq,streaming}"
    echo "  - Check .env: cat /home/mastodon/live/.env.production"
    echo "  - Run migrations: sudo -u mastodon bash -lc 'cd ~/live && RAILS_ENV=production bin/rails db:migrate'"
fi
echo "=========================================="

exit $ERRORS
