name: Update README with Download Links

on:
  workflow_run:
    workflows: ["Build apt-ostree Package"]
    types: [completed]
    branches: [main, master]

jobs:
  update-readme:
    name: Update README with Download Links
    runs-on: ubuntu-latest
    container:
      image: debian:latest
    if: always()
    steps:
      - name: Setup build environment
        shell: bash
        run: |
          # APT Performance Optimizations (2-3x faster)
          echo 'Acquire::Languages "none";' > /etc/apt/apt.conf.d/99translations
          echo 'Acquire::GzipIndexes "true";' >> /etc/apt/apt.conf.d/99translations
          echo 'Acquire::CompressionTypes::Order:: "gz";' >> /etc/apt/apt.conf.d/99translations
          echo 'Dpkg::Use-Pty "0";' >> /etc/apt/apt.conf.d/99translations

          # Update package lists
          apt update -y

          # Install essential build tools (optimized order)
          apt install -y --no-install-recommends \
            git curl pkg-config build-essential gnupg wget \
            rustc cargo libapt-pkg-dev libapt-pkg7.0 \
            libostree-dev

      - name: Checkout repository manually
        run: |
          # Clone the repository manually instead of using actions/checkout
          git clone https://git.raines.xyz/robojerk/apt-ostree.git /tmp/apt-ostree
          cp -r /tmp/apt-ostree/* .
          cp -r /tmp/apt-ostree/.* . 2>/dev/null || true

      - name: Update README with download links
        run: |
          # Get current date and workflow run ID
          BUILD_DATE=$(date '+%Y-%m-%d %H:%M:%S UTC')
          WORKFLOW_RUN_ID=$(date +%s)

          echo "Updating README with workflow run ID: $WORKFLOW_RUN_ID"

          # Create the download section content
          echo "

          ## 📦 Download Latest Build

          **Last Built**: $BUILD_DATE
          **Version**: 0.1.0-1
          **Target**: Debian Stable
          **Build ID**: $WORKFLOW_RUN_ID

          ### Download Links

          - **apt-ostree_0.1.0-1_amd64.deb** - Build $WORKFLOW_RUN_ID
          - **apt-ostree-dbgsym_0.1.0-1_amd64.ddeb** - Build $WORKFLOW_RUN_ID

          ### Quick Installation

          \`\`\`bash
          # Download and install the package
          # Build ID: $WORKFLOW_RUN_ID
          # Download the .deb files and run:
          sudo dpkg -i apt-ostree_0.1.0-1_amd64.deb
          sudo apt-get install -f  # Install missing dependencies
          \`\`\`

          ### Verification

          \`\`\`bash
          # Check if apt-ostree is installed
          apt-ostree --version
          # Should output: apt-ostree 0.1.0
          \`\`\`

          ### Usage Example

          \`\`\`bash
          # Check status
          apt-ostree status

          # List packages
          apt-ostree list

          # Search packages
          apt-ostree search <package-name>

          # Build OCI image
          apt-ostree compose build-chunked-oci <treefile.yaml>
          \`\`\`

          ---
