# Test version of .htaccess without problematic directives
RewriteEngine On

# Handle subdirectory routing (stable/dev environments)
RewriteRule ^(stable|dev)/plugin/([^/]+)/([^/]+)$ index.php?env=$1&plugin=$2&file=$3 [QSA,L]
RewriteRule ^(stable|dev)/plugin/([^/]+)$ index.php?env=$1&plugin=$2 [QSA,L]
RewriteRule ^(stable|dev)/plugins/catalog$ index.php?env=$1&plugins=catalog [QSA,L]
RewriteRule ^(stable|dev)/plugins/upload$ index.php?env=$1&plugins=upload [QSA,L]
RewriteRule ^(stable|dev)/plugins$ index.php?env=$1&plugins=list [QSA,L]
RewriteRule ^(stable|dev)/health$ index.php?env=$1&health=1 [QSA,L]
RewriteRule ^(stable|dev)/?$ index.php?env=$1 [QSA,L]

# Catch-all for subdirectory routes not matched above
RewriteRule ^(stable|dev)/(.*)$ index.php?env=$1&path=$2 [QSA,L]

# Handle plugin downloads and info requests (production/root)
RewriteRule ^plugin/([^/]+)/([^/]+)$ index.php?plugin=$1&file=$2 [QSA,L]
RewriteRule ^plugin/([^/]+)$ index.php?plugin=$1 [QSA,L]

# Handle plugins catalog (production/root) - these must come BEFORE the catch-all
RewriteRule ^plugins/catalog/?$ index.php?plugins=catalog [QSA,L]
RewriteRule ^plugins/upload/?$ index.php?plugins=upload [QSA,L]
RewriteRule ^plugins/?$ index.php?plugins=list [QSA,L]

# Handle health check (production/root)
RewriteRule ^health/?$ index.php?health=1 [QSA,L]

# Catch-all for any other requests
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

# Allow directory access for plugins/ (required for rewrite rules to work)
# Options -Indexes

# Set proper MIME types
<IfModule mod_mime.c>
    AddType application/json .json
    AddType application/zip .zip
</IfModule>