name: Release Chart

on:
  push:
    branches:
      - main
    paths:
      - "charts/marmot/Chart.yaml"

permissions:
  contents: read

jobs:
  release:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v5
        with:
          fetch-depth: 0

      - name: Configure Git
        run: |
          git config user.name marmot-helm-bot
          git config user.email marmot-helm-bot@marmotdata.com

      - name: Get chart version
        id: chart_version
        run: |
          cd charts/marmot
          echo "CHART_VERSION=$(grep -o 'version: [0-9.]*' Chart.yaml | awk '{print $2}')" >> "$GITHUB_OUTPUT"

      - name: Check if version changed
        id: version_check
        run: |
          PREV_VERSION=$(git show HEAD~1:charts/marmot/Chart.yaml | grep -o 'version: [0-9.]*' | awk '{print $2}' || echo "0.0.0")
          CURRENT_VERSION="${{ steps.chart_version.outputs.CHART_VERSION }}"

          if [ "$PREV_VERSION" != "$CURRENT_VERSION" ]; then
            echo "VERSION_CHANGED=true" >> "$GITHUB_OUTPUT"
            echo "Chart version changed from $PREV_VERSION to $CURRENT_VERSION"
          else
            echo "VERSION_CHANGED=false" >> "$GITHUB_OUTPUT"          
            echo "Chart version unchanged: $CURRENT_VERSION"
          fi

      - name: Check if tag exists in charts repo
        id: tag_exists
        if: steps.version_check.outputs.VERSION_CHANGED == 'true'
        run: |
          TAG_EXISTS=$(curl -s -o /dev/null -w "%{http_code}" \
            -H "Authorization: token ${{ secrets.CHARTS_REPO_TOKEN }}" \
            "https://api.github.com/repos/marmotdata/charts/git/refs/tags/marmot-${{ steps.chart_version.outputs.CHART_VERSION }}")

          if [ "$TAG_EXISTS" = "200" ]; then
            echo "TAG_EXISTS=true" >> "$GITHUB_OUTPUT"
          else
            echo "TAG_EXISTS=false" >> "$GITHUB_OUTPUT"
          fi

      - name: Lint chart
        if: steps.version_check.outputs.VERSION_CHANGED == 'true' && steps.tag_exists.outputs.TAG_EXISTS == 'false'
        run: make chart-lint

      - name: Test chart
        if: steps.version_check.outputs.VERSION_CHANGED == 'true' && steps.tag_exists.outputs.TAG_EXISTS == 'false'
        run: make chart-test

      - name: Create tag and release in charts repo
        if: steps.version_check.outputs.VERSION_CHANGED == 'true' && steps.tag_exists.outputs.TAG_EXISTS == 'false'
        run: |
          TAG_NAME="marmot-${{ steps.chart_version.outputs.CHART_VERSION }}"

          # Create tag
          curl -X POST \
            -H "Authorization: token ${{ secrets.CHARTS_REPO_TOKEN }}" \
            -H "Accept: application/vnd.github.v3+json" \
            https://api.github.com/repos/marmotdata/charts/git/refs \
            -d "{
              \"ref\": \"refs/tags/$TAG_NAME\",
              \"sha\": \"$(curl -s -H 'Authorization: token ${{ secrets.CHARTS_REPO_TOKEN }}' https://api.github.com/repos/marmotdata/charts/git/refs/heads/main | jq -r .object.sha)\"
            }"

          # Create release
          curl -X POST \
            -H "Authorization: token ${{ secrets.CHARTS_REPO_TOKEN }}" \
            -H "Accept: application/vnd.github.v3+json" \
            https://api.github.com/repos/marmotdata/charts/releases \
            -d "{
              \"tag_name\": \"$TAG_NAME\",
              \"name\": \"Marmot Chart v${{ steps.chart_version.outputs.CHART_VERSION }}\",
              \"body\": \"Marmot Helm Chart v${{ steps.chart_version.outputs.CHART_VERSION }}\\n\\nInstall with:\\n\\\`\\\`\\\`bash\\nhelm repo add marmotdata https://marmotdata.github.io/charts\\nhelm install marmot marmotdata/marmot --version ${{ steps.chart_version.outputs.CHART_VERSION }}\\n\\\`\\\`\\\`\"
            }"

      - name: Publish Helm chart
        if: steps.version_check.outputs.VERSION_CHANGED == 'true' && steps.tag_exists.outputs.TAG_EXISTS == 'false'
        uses: stefanprodan/helm-gh-pages@master
        with:
          token: ${{ secrets.CHARTS_REPO_TOKEN }}
          owner: marmotdata
          repository: charts
          branch: main
          target_dir: marmot
          index_dir: .
          charts_dir: charts/
          charts_url: https://marmotdata.github.io/charts
          commit_username: marmot-helm-bot
          commit_email: marmot-helm-bot@marmotdata.com
