# GitHub Actions workflow to deploy website to GitHub Pages
name: Deploy Website to GitHub Pages

on:
  push:
    branches:
      - main
      - master
    paths:
      - 'website/**'
      - '.github/workflows/deploy.yml'
  
  # Allow manual trigger
  workflow_dispatch:

# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
  contents: read
  pages: write
  id-token: write

# Allow only one concurrent deployment
concurrency:
  group: "pages"
  cancel-in-progress: true

jobs:
  build:
    runs-on: ubuntu-latest
    
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'npm'
          cache-dependency-path: website/docusaurus/package-lock.json

      - name: Setup Pages
        uses: actions/configure-pages@v4

      - name: Copy images from paper to website
        run: |
          mkdir -p website/assets/images
          
          # Copy key images
          cp -f paper/sections/theoretical_background/imgs/fuked_manifold.png website/assets/images/manifold_distortion.png || true
          cp -f paper/sections/results_discussion/imgs/fineweb_wandb.png website/assets/images/training_curves.png || true
          cp -f paper/sections/results_discussion/imgs/mnist_prototypes.png website/assets/images/mnist_prototypes.png || true
          cp -f paper/sections/results_discussion/imgs/linear.png website/assets/images/linear_boundary.png || true
          cp -f paper/sections/results_discussion/imgs/yat.png website/assets/images/yat_boundary.png || true
          
          # Copy vector visualization images
          cp -f paper/vector_viz_iclr/combined_heatmaps_2d.png website/assets/images/combined_heatmaps_2d.png || true
          cp -f paper/vector_viz_iclr/combined_gradients.png website/assets/images/combined_gradients.png || true
          
          # Copy blog assets if they exist
          mkdir -p website/docusaurus/static/img/blog
          cp -rf website/assets/* website/docusaurus/static/img/blog/ || true
          
          echo "Copied images:"
          ls -la website/assets/images/ || true
          ls -la website/docusaurus/static/img/blog/ || true

      - name: Copy existing website to Docusaurus static/paper
        run: |
          mkdir -p website/docusaurus/static/paper
          
          # Copy the original static website files (excluding docusaurus folder)
          cp -f website/index.html website/docusaurus/static/paper/ || true
          cp -f website/nmn.html website/docusaurus/static/paper/ || true
          cp -rf website/css website/docusaurus/static/paper/ || true
          cp -rf website/js website/docusaurus/static/paper/ || true
          cp -rf website/assets website/docusaurus/static/paper/ || true
          cp -rf website/blog-pages website/docusaurus/static/paper/ || true
          cp -rf website/visualizations website/docusaurus/static/paper/ || true
          
          echo "Copied static website to paper folder:"
          ls -la website/docusaurus/static/paper/

      - name: Install Docusaurus dependencies
        working-directory: website/docusaurus
        run: npm ci

      - name: Build Docusaurus
        working-directory: website/docusaurus
        run: npm run build

      - name: Upload artifact
        uses: actions/upload-pages-artifact@v3
        with:
          path: 'website/docusaurus/build'

  deploy:
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    runs-on: ubuntu-latest
    needs: build
    
    steps:
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v4
