name: "Packaging: Build distributions"

"on":
  release:
    types: [published]

jobs:
  update:
    runs-on: ubuntu-latest

    steps:
    - name: Initialize build summary
      run: |
        echo "# 📦 Distribution Package Build Summary" >> $GITHUB_STEP_SUMMARY
        echo "" >> $GITHUB_STEP_SUMMARY
        echo "**Version:** \`${{ github.event.release.tag_name }}\`" >> $GITHUB_STEP_SUMMARY
        echo "**URL:** [${{ github.event.release.name }}](${{ github.event.release.html_url }})" >> $GITHUB_STEP_SUMMARY
        echo "**Triggered at:** $(date -u '+%Y-%m-%d %H:%M:%S UTC')" >> $GITHUB_STEP_SUMMARY
        echo "" >> $GITHUB_STEP_SUMMARY
        echo "## 🚀 Build Status" >> $GITHUB_STEP_SUMMARY
        echo "| Package | Status | Workflow |" >> $GITHUB_STEP_SUMMARY
        echo "| --- | --- | --- |" >> $GITHUB_STEP_SUMMARY
        echo "" >> $GITHUB_STEP_SUMMARY

        echo "HAS_FAILURES=false" >> $GITHUB_ENV

    - name: Build Docker Image
      env:
        GH_TOKEN: ${{ secrets.ORG_AUTOMATION_TOKEN }}
      run: |
        echo "🐳 Triggering Docker image build..." | tee -a build.log
        if gh workflow run docker-image.yml -R timescale/timescaledb-docker -f version=${{ github.event.release.tag_name }} -f tag_latest=true -f registry=prod; then
          echo "| 🐳 Docker Image | ✅ Triggered | [timescaledb-docker](https://github.com/timescale/timescaledb-docker/actions/workflows/docker-image.yml) |" >> $GITHUB_STEP_SUMMARY
        else
          echo "| 🐳 Docker Image | ❌ Failed | [timescaledb-docker](https://github.com/timescale/timescaledb-docker/actions/workflows/docker-image.yml) |" >> $GITHUB_STEP_SUMMARY
          echo "HAS_FAILURES=true" >> $GITHUB_ENV
        fi

    - name: Build APT Packages
      env:
        GH_TOKEN: ${{ secrets.ORG_AUTOMATION_TOKEN }}
      run: |
        echo "🐧 Triggering APT package build..." | tee -a build.log
        if gh workflow run timescaledb-apt.yml -R timescale/release-build-scripts -f version=${{ github.event.release.tag_name }} -f upload-artifacts=true; then
          echo "| 🐧 APT Packages | ✅ Triggered | [summary](https://github.com/timescale/release-build-scripts/actions/workflows/timescaledb-apt.yml) |" >> $GITHUB_STEP_SUMMARY
        else
          echo "| 🐧 APT Packages | ❌ Failed | [summary](https://github.com/timescale/release-build-scripts/actions/workflows/timescaledb-apt.yml) |" >> $GITHUB_STEP_SUMMARY
          echo "HAS_FAILURES=true" >> $GITHUB_ENV
        fi

    - name: Build RPM Packages
      env:
        GH_TOKEN: ${{ secrets.ORG_AUTOMATION_TOKEN }}
      run: |
        echo "⚙ Triggering RPM package build..." | tee -a build.log
        if gh workflow run timescaledb-rpm.yml -R timescale/release-build-scripts -f version=${{ github.event.release.tag_name }} -f upload-artifacts=true; then
          echo "| ⚙ RPM Packages | ✅ Triggered | [summary](https://github.com/timescale/release-build-scripts/actions/workflows/timescaledb-rpm.yml) |" >> $GITHUB_STEP_SUMMARY
        else
          echo "| ⚙ RPM Packages | ❌ Failed | [summary](https://github.com/timescale/release-build-scripts/actions/workflows/timescaledb-rpm.yml) |" >> $GITHUB_STEP_SUMMARY
          echo "HAS_FAILURES=true" >> $GITHUB_ENV
        fi

    - name: Build Homebrew Packages
      env:
        GH_TOKEN: ${{ secrets.ORG_AUTOMATION_TOKEN }}
      run: |
        echo "🍺 Triggering Homebrew package build..." | tee -a build.log
        if gh workflow run timescaledb-homebrew.yml -R timescale/release-build-scripts -f version=${{ github.event.release.tag_name }} -f upload-artifacts=true; then
          echo "| 🍺 Homebrew Packages | ✅ Triggered | [summary](https://github.com/timescale/release-build-scripts/actions/workflows/timescaledb-homebrew.yml) |" >> $GITHUB_STEP_SUMMARY
        else
          echo "| 🍺 Homebrew Packages | ❌ Failed | [summary](https://github.com/timescale/release-build-scripts/actions/workflows/timescaledb-homebrew.yml) |" >> $GITHUB_STEP_SUMMARY
          echo "HAS_FAILURES=true" >> $GITHUB_ENV
        fi

    - name: Build Windows Packages
      env:
        GH_TOKEN: ${{ secrets.ORG_AUTOMATION_TOKEN }}
      run: |
        echo "🪟 Triggering Windows package build..." | tee -a build.log
        if gh workflow run timescaledb-windows.yml -R timescale/release-build-scripts -f version=${{ github.event.release.tag_name }} -f upload-artifacts=true; then
          echo "| 🪟 Windows Packages | ✅ Triggered | [summary](https://github.com/timescale/release-build-scripts/actions/workflows/timescaledb-windows.yml) |" >> $GITHUB_STEP_SUMMARY
        else
          echo "| 🪟 Windows Packages | ❌ Failed | [summary](https://github.com/timescale/release-build-scripts/actions/workflows/timescaledb-windows.yml) |" >> $GITHUB_STEP_SUMMARY
          echo "HAS_FAILURES=true" >> $GITHUB_ENV
        fi

    - name: Generate final summary
      if: always()
      run: |
        echo "---" >> $GITHUB_STEP_SUMMARY
        echo "## 📋 Next Steps" >> $GITHUB_STEP_SUMMARY
        echo "" >> $GITHUB_STEP_SUMMARY
        echo "1. Monitor the triggered workflows in their respective repositories" >> $GITHUB_STEP_SUMMARY
        echo "2. Check build artifacts once workflows complete" >> $GITHUB_STEP_SUMMARY
        echo "3. Verify package availability in distribution channels" >> $GITHUB_STEP_SUMMARY

    - name: Validate workflow dispatch status
      if: ${{ env.HAS_FAILURES == 'true' }}
      run: |
        echo "---" >> $GITHUB_STEP_SUMMARY
        echo "❌ one or more workflows could not get dispatched" >> $GITHUB_STEP_SUMMARY
        exit 1
