name: Release Workflow

on:
  workflow_dispatch:
    inputs:
      version:
        description: "The version to release (e.g., 0.12.0)"
        required: true

jobs:
  # Job 1: Bump Versions and Create Tags
  versioning:
    runs-on: ubuntu-latest

    outputs:
      version: ${{ steps.set_version.outputs.version }}

    steps:
      # 1. Checkout the repository
      - name: Checkout code
        uses: actions/checkout@v3

      # 2. Setup node & npm
      - name: Setup Node.js
        uses: actions/setup-node@v3
        with:
          node-version: '22'

      # 3. Bump versions using npm
      - name: Bump version in web and docs
        id: set_version
        run: |
          VERSION=${{ github.event.inputs.version }}
          cd web && npm version $VERSION --no-git-tag-version && cd ..
          cd docs && npm version $VERSION --no-git-tag-version && cd ..
          echo "version=v$VERSION" >> $GITHUB_OUTPUT

      # 4. Tag and push the new versions
      - name: Commit and Tag
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git add web/package.json docs/package.json
          git commit -m "Release ${{ github.event.inputs.version }}"
          git tag "v${{ github.event.inputs.version }}"
          git push origin main --tags

  # Job 2: Build Docker Images
  docker-build:
    runs-on: ubuntu-latest
    needs: versioning

    steps:
      # 1. Checkout the repository
      - name: Checkout code
        uses: actions/checkout@v3
        with:
          ref: ${{ github.ref }}

      - name: Setup Go
        uses: actions/setup-go@v4
        with:
          go-version: '1.22'

      # 2. Log in to Docker Hub
      - name: Login to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      # 3. Setup docker multi platform builds
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      # 4. Build and push Docker images
      - name: Build Docker Images
        env:
          VERSION: ${{ needs.versioning.outputs.version }}
        run: |
          # Build db image
          # cd db
          # env GOOS=linux GOARCH=arm64 go build -o pocketbase_arm64
          # env GOOS=linux GOARCH=amd64 go build -o pocketbase_amd64
          # cd ..
          # docker buildx build db/ --no-cache -t flomp/wanderer-db:$VERSION -t flomp/wanderer-db:latest --platform=linux/amd64,linux/arm64 --push

          # Build web image
          export PUBLIC_VALHALLA_URL=https://valhalla1.openstreetmap.de
          cd web
          npm ci && npm run build
          cd ..
          docker buildx build web/ --no-cache -t flomp/wanderer-web:$VERSION -t flomp/wanderer-web:latest --platform=linux/amd64,linux/arm64 --push

          # Build docs image
          cd docs
          npm ci && npm run build
          cd ..
          docker buildx build docs/ --no-cache -t flomp/wanderer-docs:$VERSION -t flomp/wanderer-docs:latest --platform=linux/amd64,linux/arm64 --push

  # Job 3: Publish the Release
  release:
    runs-on: ubuntu-latest
    needs: [versioning, docker-build]

    steps:
      # 1. Checkout the repository
      - name: Checkout code
        uses: actions/checkout@v3
        with:
          ref: ${{ github.ref }}
      # 2. Extract release notes from CHANGELOG.md
      - name: Extract release notes
        id: changelog
        run: |
          VERSION="${{ needs.versioning.outputs.version }}"
          CHANGELOG=$(awk -v ver="$VERSION" '
            BEGIN { in_section = 0 }
            /^# / {
              if (in_section) exit
              if ($2 == ver) in_section = 1
            }
            in_section { print }
          ' CHANGELOG.md)
          echo 'changelog<<EOF' >> $GITHUB_OUTPUT
          printf "%s\n" "$CHANGELOG" >> $GITHUB_OUTPUT
          echo 'EOF' >> $GITHUB_OUTPUT

      # 3. Create GitHub Release
      - name: Create GitHub Release
        uses: actions/create-release@v1
        with:
          tag_name: ${{ needs.versioning.outputs.version }}
          release_name: "${{ needs.versioning.outputs.version }}"
          body: ${{ steps.changelog.outputs.changelog }}
          draft: false
          prerelease: false
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
