name: Deploy Marketplace Site

# Builds the marketplace site + catalog from private sources and deploys
# to the PUBLIC repo's gh-pages branch via repository_dispatch.
# All source content (index.html, catalog, build scripts) stays private.

on:
  push:
    branches: [main]
    paths:
      - "catalog/**"
      - "marketplace-site/**"
  workflow_dispatch:
    inputs:
      reason:
        description: "Reason for deployment"
        required: false
        default: "Manual marketplace deploy"

permissions:
  contents: read

jobs:
  build-and-deploy:
    name: Build and deploy marketplace
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.12"

      - name: Install deps
        run: pip install pyyaml

      - name: Build catalog JSON from YAML sources
        run: python marketplace-site/build_catalog.py

      - name: Build site artifacts
        run: |
          mkdir -p _site/api/v1

          # Copy static site
          cp -r marketplace-site/static/* _site/ 2>/dev/null || true

          # Copy catalog
          cp marketplace-site/output/catalog.json _site/api/v1/catalog.json

          # Build manifest
          python -c "
          import json, hashlib, datetime
          with open('_site/api/v1/catalog.json') as f:
              data = f.read()
          checksum = hashlib.sha256(data.encode()).hexdigest()
          catalog = json.loads(data)
          manifest = {
              'version': catalog.get('version', '0.0.0'),
              'updated_at': datetime.datetime.utcnow().isoformat() + 'Z',
              'checksum': checksum,
              'server_count': len(catalog.get('servers', [])),
              'endpoints': {
                  'catalog': '/api/v1/catalog.json',
                  'manifest': '/api/v1/manifest.json'
              }
          }
          with open('_site/api/v1/manifest.json', 'w') as f:
              json.dump(manifest, f, indent=2)
          print(f'Manifest: v{manifest[\"version\"]}, {manifest[\"server_count\"]} servers')
          "

          # CNAME
          echo "grid.mcpstudio.qabots.diy" > _site/CNAME

      - name: Package artifacts
        run: |
          cd _site && tar -czf ../marketplace-artifacts.tar.gz . && cd ..
          echo "Packaged $(du -h marketplace-artifacts.tar.gz | cut -f1) of site artifacts"

      - name: Upload build artifact
        uses: actions/upload-artifact@v4
        with:
          name: marketplace-site
          path: marketplace-artifacts.tar.gz
          retention-days: 7

      - name: Trigger deployment on public repo
        if: github.ref == 'refs/heads/main'
        run: |
          # Trigger the public repo's deploy workflow via repository_dispatch.
          # The actual artifact transfer uses the upload URL from this run.
          # DEPLOY_PAT must have repo scope on the public repo.
          curl -sS -X POST \
            -H "Authorization: token ${{ secrets.DEPLOY_PAT }}" \
            -H "Accept: application/vnd.github.v3+json" \
            "https://api.github.com/repos/${{ secrets.PUBLIC_REPO }}/dispatches" \
            -d "{\"event_type\": \"deploy-marketplace\", \"client_payload\": {\"run_id\": \"${{ github.run_id }}\", \"sha\": \"${{ github.sha }}\"}}"
          echo "Dispatch sent to public repo."
