name: Release Charts

on:
  push:
    tags:
      - v*

jobs:
  release:
    # depending on default permission settings for your org (contents being read-only or read-write for workloads), you will have to add permissions
    # see: https://docs.github.com/en/actions/security-guides/automatic-token-authentication#modifying-the-permissions-for-the-github_token
    permissions:
      contents: write
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6
        with:
          fetch-depth: 0


      - name: Configure Git
        run: |
          git config user.name "$GITHUB_ACTOR"
          git config user.email "$GITHUB_ACTOR@users.noreply.github.com"

      - name: Install Helm
        uses: azure/setup-helm@1a275c3b69536ee54be43f2070a358922e12c8d4 # v4.3.1
        with:
          version: v3.16.0

      - name: Add Helm repositories and update dependencies
        run: |
          helm repo add qdrant https://qdrant.github.io/qdrant-helm
          helm repo add ollama https://otwld.github.io/ollama-helm
          helm repo update
          helm dependency build charts/nextcloud-mcp-server

      - name: Run chart-releaser
        uses: helm/chart-releaser-action@cae68fefc6b5f367a0275617c9f83181ba54714f # v1.7.0
        env:
          CR_TOKEN: "${{ secrets.GITHUB_TOKEN }}"

      - name: Update gh-pages with Chart README and Index
        run: |
          # Get the repository name
          REPO_NAME="${GITHUB_REPOSITORY##*/}"
          REPO_OWNER="${GITHUB_REPOSITORY%/*}"

          # Switch to gh-pages branch
          git fetch origin gh-pages
          git checkout gh-pages

          # Copy Chart README to root
          git checkout ${GITHUB_REF#refs/tags/} -- charts/nextcloud-mcp-server/README.md
          mv charts/nextcloud-mcp-server/README.md README.md || true
          rm -rf charts 2>/dev/null || true

          # Create index.html with installation instructions
          cat > index.html <<'EOF'
          <!DOCTYPE html>
          <html lang="en">
          <head>
              <meta charset="UTF-8">
              <meta name="viewport" content="width=device-width, initial-scale=1.0">
              <title>Nextcloud MCP Server Helm Chart</title>
              <style>
                  body {
                      font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
                      max-width: 800px;
                      margin: 50px auto;
                      padding: 20px;
                      line-height: 1.6;
                  }
                  code {
                      background: #f4f4f4;
                      padding: 2px 6px;
                      border-radius: 3px;
                      font-family: "Monaco", "Courier New", monospace;
                  }
                  pre {
                      background: #f4f4f4;
                      padding: 15px;
                      border-radius: 5px;
                      overflow-x: auto;
                  }
                  h1, h2 { color: #0082c9; }
                  a { color: #0082c9; text-decoration: none; }
                  a:hover { text-decoration: underline; }
              </style>
          </head>
          <body>
              <h1>Nextcloud MCP Server Helm Chart</h1>

              <p>A Helm chart for deploying the Nextcloud MCP (Model Context Protocol) Server on Kubernetes, enabling AI assistants to interact with your Nextcloud instance.</p>

              <h2>Installation</h2>

              <p>Add the Helm repository:</p>
              <pre><code>helm repo add nextcloud-mcp https://REPO_OWNER.github.io/REPO_NAME/
          helm repo update</code></pre>

              <p>Install the chart:</p>
              <pre><code>helm install nextcloud-mcp nextcloud-mcp/nextcloud-mcp-server \
            --set nextcloud.host=https://cloud.example.com \
            --set auth.basic.username=myuser \
            --set auth.basic.password=mypassword</code></pre>

              <h2>Documentation</h2>

              <ul>
                  <li><a href="README.md">Chart README</a> - Full documentation for the Helm chart</li>
                  <li><a href="https://github.com/REPO_OWNER/REPO_NAME">GitHub Repository</a> - Source code and issues</li>
                  <li><a href="index.yaml">Helm Repository Index</a> - Chart metadata</li>
              </ul>

              <h2>Quick Start</h2>

              <p>See the <a href="README.md">full documentation</a> for detailed configuration options, examples, and troubleshooting guides.</p>

              <hr>
              <p><small>Generated by <a href="https://github.com/helm/chart-releaser">chart-releaser</a></small></p>
          </body>
          </html>
          EOF

          # Replace placeholders
          sed -i "s/REPO_OWNER/$REPO_OWNER/g" index.html
          sed -i "s/REPO_NAME/$REPO_NAME/g" index.html

          # Commit changes
          git add README.md index.html
          git commit -m "Update README and index from chart release" || echo "No changes to commit"
          git push origin gh-pages
