name: Build Host Agent

on:
  push:
    tags:
      - 'v*.*.*'  # Trigger on version tags (v1.0.0, v1.2.3, etc.)

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Set up Go
        uses: actions/setup-go@v4
        with:
          go-version: '1.21'

      - name: Get version from tag
        id: version
        run: echo "VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT

      - name: Build Linux AMD64
        run: |
          cd companion-agent
          mkdir -p bin
          GOOS=linux GOARCH=amd64 go build -ldflags "-s -w -X main.version=${{ steps.version.outputs.VERSION }}" -o bin/corrosion-host-agent-linux-amd64 ./cmd/agent
          chmod +x bin/corrosion-host-agent-linux-amd64

      - name: Build Windows AMD64
        run: |
          cd companion-agent
          GOOS=windows GOARCH=amd64 go build -ldflags "-s -w -X main.version=${{ steps.version.outputs.VERSION }}" -o bin/corrosion-host-agent-windows-amd64.exe ./cmd/agent

      - name: Generate checksums
        run: |
          cd companion-agent/bin
          sha256sum corrosion-host-agent-linux-amd64 > checksums.txt
          sha256sum corrosion-host-agent-windows-amd64.exe >> checksums.txt
          cat checksums.txt

      - name: Create Release
        env:
          RELEASE_TOKEN: ${{ secrets.RELEASE_TOKEN }}
        run: |
          VERSION=${{ steps.version.outputs.VERSION }}
          REPO="vantzs/corrosion-admin-panel"
          API_URL="https://git.corrosionmgmt.com/api/v1"

          # Create release (parse ID without jq)
          RESPONSE=$(curl -s -X POST \
            -H "Authorization: token ${RELEASE_TOKEN}" \
            -H "Content-Type: application/json" \
            -d "{\"tag_name\": \"${VERSION}\", \"name\": \"Corrosion Host Agent ${VERSION}\", \"body\": \"Corrosion Host Agent release ${VERSION}\", \"draft\": false, \"prerelease\": false}" \
            "${API_URL}/repos/${REPO}/releases")
          RELEASE_ID=$(echo "$RESPONSE" | grep -o '"id":[0-9]*' | head -1 | grep -o '[0-9]*')

          if [ -z "$RELEASE_ID" ]; then
            echo "ERROR: Failed to create release. Response:"
            echo "$RESPONSE"
            exit 1
          fi
          echo "Release created with ID: ${RELEASE_ID}"

          # Upload Linux binary
          curl -s -X POST \
            -H "Authorization: token ${RELEASE_TOKEN}" \
            -H "Content-Type: application/octet-stream" \
            --data-binary @companion-agent/bin/corrosion-host-agent-linux-amd64 \
            "${API_URL}/repos/${REPO}/releases/${RELEASE_ID}/assets?name=corrosion-host-agent-linux-amd64"

          # Upload Windows binary
          curl -s -X POST \
            -H "Authorization: token ${RELEASE_TOKEN}" \
            -H "Content-Type: application/octet-stream" \
            --data-binary @companion-agent/bin/corrosion-host-agent-windows-amd64.exe \
            "${API_URL}/repos/${REPO}/releases/${RELEASE_ID}/assets?name=corrosion-host-agent-windows-amd64.exe"

          # Upload checksums
          curl -s -X POST \
            -H "Authorization: token ${RELEASE_TOKEN}" \
            -H "Content-Type: application/octet-stream" \
            --data-binary @companion-agent/bin/checksums.txt \
            "${API_URL}/repos/${REPO}/releases/${RELEASE_ID}/assets?name=checksums.txt"

      - name: Upload to CDN (latest)
        run: |
          CDN_URL="https://cdn.corrosionmgmt.com"

          # Upload Linux binary to /host-agent/latest/
          curl -s -X POST \
            -F "file=@companion-agent/bin/corrosion-host-agent-linux-amd64" \
            "${CDN_URL}/host-agent/latest/corrosion-host-agent-linux-amd64"

          # Upload Windows binary to /host-agent/latest/
          curl -s -X POST \
            -F "file=@companion-agent/bin/corrosion-host-agent-windows-amd64.exe" \
            "${CDN_URL}/host-agent/latest/corrosion-host-agent-windows-amd64.exe"

          # Upload checksums
          curl -s -X POST \
            -F "file=@companion-agent/bin/checksums.txt" \
            "${CDN_URL}/host-agent/latest/checksums.txt"

          # Also upload versioned copies
          VERSION=${{ steps.version.outputs.VERSION }}
          curl -s -X POST \
            -F "file=@companion-agent/bin/corrosion-host-agent-linux-amd64" \
            "${CDN_URL}/host-agent/${VERSION}/corrosion-host-agent-linux-amd64"
          curl -s -X POST \
            -F "file=@companion-agent/bin/corrosion-host-agent-windows-amd64.exe" \
            "${CDN_URL}/host-agent/${VERSION}/corrosion-host-agent-windows-amd64.exe"
          curl -s -X POST \
            -F "file=@companion-agent/bin/checksums.txt" \
            "${CDN_URL}/host-agent/${VERSION}/checksums.txt"

          echo "CDN upload complete: ${CDN_URL}/host-agent/latest/"

      - name: Build Summary
        run: |
          echo "## Corrosion Host Agent Build Complete" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "**Version:** ${{ steps.version.outputs.VERSION }}" >> $GITHUB_STEP_SUMMARY
          echo "**Commit:** ${GITHUB_SHA:0:7}" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "### Built Artifacts:" >> $GITHUB_STEP_SUMMARY
          echo "- Linux AMD64 ($(stat -c%s companion-agent/bin/corrosion-host-agent-linux-amd64) bytes)" >> $GITHUB_STEP_SUMMARY
          echo "- Windows AMD64 ($(stat -c%s companion-agent/bin/corrosion-host-agent-windows-amd64.exe) bytes)" >> $GITHUB_STEP_SUMMARY
          echo "- SHA256 checksums" >> $GITHUB_STEP_SUMMARY
