# Docs for the Azure Web Apps Deploy action: https://github.com/Azure/webapps-deploy
# More GitHub Actions for Azure: https://github.com/Azure/actions

name: Build and deploy Node.js app to Azure Web App - app-crsouza-blog

on:
  push:
    branches:
      - main
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    permissions:
      contents: read #This is required for actions/checkout

    steps:
      - uses: actions/checkout@v4

      - name: Set up Node.js version
        uses: actions/setup-node@v3
        with:
          node-version: '20.x'

      - name: npm install, build, and test
        run: |
          npm install
          npm run build --if-present
          npm run test --if-present

      - name: Upload artifact for deployment job
        uses: actions/upload-artifact@v4
        with:
          name: node-app
          path: .

  deploy:
    runs-on: ubuntu-latest
    needs: build
    permissions:
      id-token: write #This is required for requesting the JWT
      contents: read #This is required for actions/checkout

    steps:
      - name: Checkout repository for newsletter metadata
        uses: actions/checkout@v4
        with:
          fetch-depth: 2

      - name: Download artifact from build job
        uses: actions/download-artifact@v4
        with:
          name: node-app

      - name: Capture changed files in push
        if: github.event_name == 'push' && contains(github.event.head_commit.message, 'NEWS UPDATE')
        run: |
          jq -r '.commits[] | (.added[]?, .modified[]?, .removed[]?)' "$GITHUB_EVENT_PATH" \
            | sed '/^$/d' \
            | sort -u > changed-files.txt

          echo "Changed files captured for newsletter:" 
          cat changed-files.txt

          {
            echo "NEWSLETTER_CHANGED_FILES<<EOF"
            cat changed-files.txt
            echo "EOF"
          } >> "$GITHUB_ENV"
      
      - name: Login to Azure
        uses: azure/login@v2
        with:
          client-id: ${{ secrets.AZUREAPPSERVICE_CLIENTID_0B59212DBB17453583D8BCB6261C6C0E }}
          tenant-id: ${{ secrets.AZUREAPPSERVICE_TENANTID_E4E71C43D20C4D56A49D9D80294BAAEE }}
          subscription-id: ${{ secrets.AZUREAPPSERVICE_SUBSCRIPTIONID_9FE3309989184838B0103246745A089D }}

      - name: 'Deploy to Azure Web App'
        id: deploy-to-webapp
        uses: azure/webapps-deploy@v3
        with:
          app-name: 'app-crsouza-blog'
          slot-name: 'Production'
          package: .

      - name: Load App Settings For Newsletter
        if: github.event_name == 'push' && contains(github.event.head_commit.message, 'NEWS UPDATE')
        run: |
          az webapp config appsettings list \
            --name app-crsouza-blog \
            --resource-group rg-crsouza-blog \
            --query "[?name=='AZURE_STORAGE_CONNECTION_STRING' || name=='COMMENTS_TABLE_NAME' || name=='SUBSCRIBERS_TABLE_NAME' || name=='AZURE_EMAIL_CONNECTION_STRING' || name=='MAIL_FROM' || name=='NODE_ENV' || name=='BLOG_URL'].[name,value]" \
            --output tsv > appsettings.tsv

          while IFS=$'\t' read -r name value; do
            echo "$name=$value" >> "$GITHUB_ENV"
          done < appsettings.tsv

      - name: Resolve Public URL For Newsletter
        if: github.event_name == 'push' && contains(github.event.head_commit.message, 'NEWS UPDATE')
        run: |
          DEFAULT_HOSTNAME=$(az webapp show \
            --name app-crsouza-blog \
            --resource-group rg-crsouza-blog \
            --query defaultHostName \
            --output tsv)

          if [ -n "$DEFAULT_HOSTNAME" ]; then
            echo "WEBSITE_HOSTNAME=$DEFAULT_HOSTNAME" >> "$GITHUB_ENV"
          fi

          if [ -z "${BLOG_URL:-}" ] || echo "${BLOG_URL:-}" | grep -Eiq 'seu-dominio\.com|example\.com|localhost'; then
            echo "BLOG_URL=https://$DEFAULT_HOSTNAME" >> "$GITHUB_ENV"
          fi

      - name: Send Newsletter For NEWS UPDATES
        if: github.event_name == 'push' && contains(github.event.head_commit.message, 'NEWS UPDATE')
        env:
          NEWSLETTER_COMMIT_MESSAGE: ${{ github.event.head_commit.message }}
          NEWSLETTER_COMMIT_HASH: ${{ github.sha }}
        run: node scripts/send-newsletter-on-update.js
          