#!/bin/bash
set -e

echo "Installing Headless Drupal modules..."

# Check if aliases are available.
if command -v dcomposer &> /dev/null; then
    COMPOSER_CMD="dcomposer"
    DRUSH_CMD="ddrush"
else
    COMPOSER_CMD="docker exec drupal-fpm composer"
    DRUSH_CMD="docker exec drupal-fpm drush"
fi

# Core headless modules.
echo "→ Installing core headless modules..."
$COMPOSER_CMD require \
    drupal/next \
    drupal/jsonapi_extras \
    drupal/simple_oauth \
    drupal/pathauto \

# Additional JSON:API enhancements.
echo "→ Installing JSON:API enhancement modules..."
$COMPOSER_CMD require \
    drupal/subrequests \
    drupal/decoupled_router \
    drupal/consumers \
    drupal/jsonapi_menu_items \
    drupal/jsonapi_include \
    drupal/jsonapi_resources \
    drupal/jsonapi_menu_items

# Content and media.
echo "→ Installing content and media modules..."
$COMPOSER_CMD require \
    drupal/metatag \
    drupal/redirect \
    drupal/field_group

# Performance and caching.
echo "→ Installing performance modules..."
$COMPOSER_CMD require \
    drupal/redis

# Development tools.
echo "→ Installing development tools..."
$COMPOSER_CMD require --dev \
    drupal/coder \
    drupal/devel \
    drupal/restui \
    drupal/admin_toolbar

# "Advanced" modules.
echo "→ Installing advanced modules..."
$COMPOSER_CMD require \
    drupal/pathauto

echo "✓ All modules installed successfully!"
echo ""
echo "Next steps:"
echo "1. Enable modules: $DRUSH_CMD en next jsonapi_extras simple_oauth pathauto -y"
echo "2. Generate OAuth keys: docker exec drupal-fpm bash -c 'cd /opt/drupal && mkdir -p oauth/keys && openssl genrsa -out oauth/keys/private.key 2048 && openssl rsa -in oauth/keys/private.key -pubout -out oauth/keys/public.key && chmod 600 oauth/keys/*.key'"
echo "3. Configure modules via Drupal admin UI"
echo "4. Export configuration: $DRUSH_CMD cex -y"
