#!/bin/bash

# Script to fix database password for explorer backend
# This script will reset the PostgreSQL password to match the backend configuration

set -euo pipefail

cd "$(dirname "$0")/.."

# Default configuration
DB_HOST="${DB_HOST:-localhost}"
DB_PORT="${DB_PORT:-5432}"
DB_USER="${DB_USER:-explorer}"
DB_PASSWORD="${DB_PASSWORD:-changeme}"
DB_NAME="${DB_NAME:-explorer}"

echo "=========================================="
echo "Database Password Fix Script"
echo "=========================================="
echo ""
echo "Configuration:"
echo "  Host: $DB_HOST"
echo "  Port: $DB_PORT"
echo "  User: $DB_USER"
echo "  Database: $DB_NAME"
echo "  New Password: [REDACTED]"
echo ""

# Check if PostgreSQL is accessible
echo -n "Checking PostgreSQL accessibility... "
if pg_isready -h "$DB_HOST" -p "$DB_PORT" >/dev/null 2>&1; then
    echo "✅ PostgreSQL is accessible"
else
    echo "❌ PostgreSQL is not accessible at $DB_HOST:$DB_PORT"
    echo "Please ensure PostgreSQL is running and accessible."
    exit 1
fi

# Try to reset password using docker-compose if available
if [ -f "deployment/docker-compose.yml" ]; then
    echo ""
    echo "Attempting to reset password via docker-compose..."
    
    # Check if postgres container is running
    if docker-compose -f deployment/docker-compose.yml ps postgres 2>/dev/null | grep -q "Up"; then
        echo "PostgreSQL container is running via docker-compose"
        
        # Try to reset password using postgres superuser
        echo "Resetting password for user '$DB_USER'..."
        if docker-compose -f deployment/docker-compose.yml exec -T postgres psql -U postgres -c "ALTER USER $DB_USER WITH PASSWORD '$DB_PASSWORD';" 2>&1; then
            echo "✅ Password reset successful via docker-compose"
        else
            echo "⚠️  Could not reset password via docker-compose (may need superuser access)"
        fi
    fi
fi

# Test the connection with new password
echo ""
echo -n "Testing database connection with new password... "
if PGPASSWORD="$DB_PASSWORD" psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -c "SELECT 1;" >/dev/null 2>&1; then
    echo "✅ Connection successful!"
    echo ""
    echo "Database password has been fixed. The backend should now be able to connect."
    echo ""
    echo "Next steps:"
    echo "1. Restart the backend service:"
    echo "   kill \$(cat /tmp/explorer_backend.pid) && ./scripts/start-backend-service.sh"
    echo ""
    echo "2. Verify the connection:"
    echo "   curl http://localhost:8080/health"
    exit 0
else
    echo "❌ Connection still failing"
    echo ""
    echo "The password reset may not have worked, or the database requires different credentials."
    echo ""
    echo "Options:"
    echo "1. Check if the database is running in Docker:"
    echo "   docker ps | grep postgres"
    echo ""
    echo "2. Manually reset the password:"
    echo "   docker exec -it <postgres-container> psql -U postgres -c \"ALTER USER $DB_USER WITH PASSWORD '$DB_PASSWORD';\""
    echo ""
    echo "3. Or update the backend to use the correct password:"
    echo "   export DB_PASSWORD='<actual-password>'"
    echo "   ./scripts/start-backend-service.sh"
    exit 1
fi

