name: Deploy Documentation to GitHub Pages

on:
  push:
    branches:
      - main 
    paths:
      - 'docs/**'  # Trigger when files in docs/ change
  workflow_dispatch:  # Allows manual triggering

jobs:
  deploy-docs:
    runs-on: ubuntu-latest
    
    steps:
      - name: Checkout source repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0  # Fetch all history for proper git operations
      
      - name: Deploy to documentation repository
        env:
          TARGET_REPO: jesus-333/dtw_loss_functions_documentation
          TARGET_BRANCH: main  # Branch in the docs repo (often 'main' or 'gh-pages')
          GITHUB_TOKEN: ${{ secrets.DOCS_DEPLOY_TOKEN }}
        run: |
          # Verify token is available
          if [ -z "$GITHUB_TOKEN" ]; then
            echo "Error: DOCS_DEPLOY_TOKEN secret is not set!"
            exit 1
          fi
          
          # Configure git
          git config --global user.name "GitHub Actions Bot"
          git config --global user.email "actions@github.com"
          
          # Clone the target documentation repository
          git clone https://${GITHUB_TOKEN}@github.com/${TARGET_REPO}.git docs-deploy
          cd docs-deploy
          
          # Switch to target branch (create if doesn't exist)
          git checkout ${TARGET_BRANCH} || git checkout -b ${TARGET_BRANCH}
          
          # Remove old content (preserve .git, .github, README.md, LICENSE, etc.)
          find . -mindepth 1 -maxdepth 1 \
            ! -name '.git' \
            ! -name '.github' \
            ! -name 'README.md' \
            ! -name 'LICENSE' \
            ! -name '.gitignore' \
            -exec rm -rf {} +
          
          # Copy new documentation content
          cp -r ../docs/* .
          
          # Add, commit and push changes
          git add .
          
          # Only commit if there are changes
          if git diff --staged --quiet; then
            echo "No changes to commit"
          else
            git commit -m "Update documentation from source repo (commit: ${{ github.sha }})"
            git push https://${GITHUB_TOKEN}@github.com/${TARGET_REPO}.git ${TARGET_BRANCH}
            echo "Documentation successfully deployed!"
          fi
