MODULE_NAME := "optimizer"
BUCKET := "botspot-plugins"
GCS_PREFIX := "gs://" + BUCKET + "/" + MODULE_NAME

default:
    @just --list

# === Build ===

# The module's info file carries no version in git because drupal.org adds it
# when it packages a release. The build recipes write the version they are
# given into the zip's copy, so installs from the bucket still report one.

# Build production zip (default URLs). Example: just build-prod 1.2.1
[group('build')]
build-prod VERSION:
    #!/usr/bin/env bash
    set -euo pipefail
    # ponytail: clean only the staging dir, not dist/ — build-all would
    # otherwise delete the zip the previous recipe just produced.
    rm -rf dist/{{MODULE_NAME}}
    mkdir -p dist/{{MODULE_NAME}}

    cp -r \
        optimizer.info.yml \
        optimizer.module \
        optimizer.install \
        optimizer.routing.yml \
        optimizer.services.yml \
        optimizer.links.menu.yml \
        config \
        src \
        README.md \
        SECURITY.md \
        SECURITY-OVERVIEW.md \
        dist/{{MODULE_NAME}}/

    printf 'version: %s\n' "{{VERSION}}" >> dist/{{MODULE_NAME}}/optimizer.info.yml

    cd dist && zip -r "{{MODULE_NAME}}-{{VERSION}}.zip" {{MODULE_NAME}}
    rm -rf {{MODULE_NAME}}
    echo "Built dist/{{MODULE_NAME}}-{{VERSION}}.zip (production)"

# Build staging zip (staging API URLs baked in).
[group('build')]
build-staging VERSION:
    #!/usr/bin/env bash
    set -euo pipefail
    # ponytail: clean only the staging dir, not dist/ — build-all would
    # otherwise delete the zip the previous recipe just produced.
    rm -rf dist/{{MODULE_NAME}}
    mkdir -p dist/{{MODULE_NAME}}

    cp -r \
        optimizer.info.yml \
        optimizer.module \
        optimizer.install \
        optimizer.routing.yml \
        optimizer.services.yml \
        optimizer.links.menu.yml \
        config \
        src \
        README.md \
        SECURITY.md \
        SECURITY-OVERVIEW.md \
        dist/{{MODULE_NAME}}/

    printf 'version: %s\n' "{{VERSION}}" >> dist/{{MODULE_NAME}}/optimizer.info.yml

    # Overwrite config with staging URLs
    printf '%s\n' \
        "enabled: false" \
        "site_id: ''" \
        "connected_at: ''" \
        "locus_api_url: 'https://locus-staging-api.bot.spot'" \
        "connectors_url: 'https://locus-connectors-staging.bot.spot'" \
        "enable_jsonld: true" \
        "cache_ttl: 300" \
        > dist/{{MODULE_NAME}}/config/install/optimizer.settings.yml

    cd dist && zip -r "{{MODULE_NAME}}-{{VERSION}}-staging.zip" {{MODULE_NAME}}
    rm -rf {{MODULE_NAME}}
    echo "Built dist/{{MODULE_NAME}}-{{VERSION}}-staging.zip (staging)"

# Build both staging and production zips.
[group('build')]
build-all VERSION: (build-prod VERSION) (build-staging VERSION)
    @echo "Built both production and staging zips"

# === Release ===

# Upload production zip to versioned bucket path.
[group('release')]
upload-prod VERSION:
    gcloud storage cp "dist/{{MODULE_NAME}}-{{VERSION}}.zip" "{{GCS_PREFIX}}/v{{VERSION}}/"

# Upload staging zip to staging/ bucket path.
[group('release')]
upload-staging VERSION:
    gcloud storage cp "dist/{{MODULE_NAME}}-{{VERSION}}-staging.zip" "{{GCS_PREFIX}}/staging/{{MODULE_NAME}}-staging.zip"

# Promote production version to latest/.
[group('release')]
promote-latest VERSION:
    gcloud storage cp "dist/{{MODULE_NAME}}-{{VERSION}}.zip" "{{GCS_PREFIX}}/latest/{{MODULE_NAME}}.zip"

# Tag and push git tag.
[group('release')]
tag VERSION:
    #!/usr/bin/env bash
    set -euo pipefail
    if git rev-parse "v{{VERSION}}" >/dev/null 2>&1; then
        echo "Tag v{{VERSION}} already exists — skipping."
        exit 0
    fi
    git tag -a "v{{VERSION}}" -m "{{MODULE_NAME}} {{VERSION}}"
    git push origin "v{{VERSION}}"

# Push this version's code and tag to drupal.org.
[group('release')]
drupal-push VERSION:
    #!/usr/bin/env bash
    set -euo pipefail
    # drupal.org tags carry no v prefix, and its packaging system reads the
    # branch a tag sits on, so the minor branch has to exist there first.
    branch="$(echo '{{VERSION}}' | cut -d. -f1,2).x"

    git fetch drupal
    echo "Pushing main to drupal:${branch} and tag {{VERSION}}"
    git push drupal "main:refs/heads/${branch}"
    git push drupal "refs/tags/v{{VERSION}}:refs/tags/{{VERSION}}"

    echo
    echo "Now create the release node by hand, which drupal.org has no API for:"
    echo "  https://www.drupal.org/node/add/project-release/optimizer"
    echo "Pick tag {{VERSION}} on branch ${branch}. Packaging takes ~30 minutes."

# Full staging release: build + upload.
[group('release')]
release-staging VERSION: (build-staging VERSION) (upload-staging VERSION)
    @echo "Released staging to {{GCS_PREFIX}}/staging/"

# Full production release: build, upload, promote, tag.
[group('release')]
release-prod VERSION: (build-prod VERSION) (upload-prod VERSION) (promote-latest VERSION) (tag VERSION)
    @echo "Released v{{VERSION}} to {{GCS_PREFIX}}/v{{VERSION}}/ and latest/"

# === Info ===

# Show bucket contents.
[group('info')]
ls-bucket:
    gcloud storage ls -l "{{GCS_PREFIX}}/"
    @echo
    @echo "--- staging/ ---"
    gcloud storage ls -l "{{GCS_PREFIX}}/staging/" 2>/dev/null || echo "(empty)"
    @echo
    @echo "--- latest/ ---"
    gcloud storage ls -l "{{GCS_PREFIX}}/latest/" 2>/dev/null || echo "(empty)"

# Print the latest released version, from the newest git tag.
[group('info')]
version:
    @git describe --tags --abbrev=0 --match 'v*' | sed 's/^v//'

# === Dev ===

# Start ddev environment.
[group('dev')]
up:
    ddev start

# Stop ddev environment.
[group('dev')]
down:
    ddev stop

# Run PHPStan.
[group('dev')]
lint:
    phpstan analyse src --level=5 --memory-limit=1G
