name: Build Sphinx Documentation

on:
  pull_request:
    types: [opened, synchronize, reopened, edited]
    branches: [master]

jobs:
  build-docs:
    name: Build Documentation from PR Branch
    runs-on: linux

    steps:
      # 第一步：设置主工作树和虚拟环境
      - name: Setup main worktree and virtual environment
        run: |
          echo "=== Setting up main worktree ==="
          
          # 如果主目录不存在，克隆仓库
          if [ ! -d "/home/ly0kos/work/test_env" ]; then
            echo "Cloning repository to main worktree..."
            git clone http://localhost:3000/yuysh/Manlink_Doc.git /home/ly0kos/work/test_env
            cd /home/ly0kos/work/test_env
            
            # 创建共享虚拟环境
            python -m venv .venv-shared
            source .venv-shared/bin/activate
            pip install --upgrade pip
            pip install sphinx sphinx-rtd-theme
            
            # 记录初始依赖状态
            if [ -f "requirements.txt" ]; then
              pip install -r requirements.txt
              stat -c %Y requirements.txt > .venv-shared/last_update.txt
            fi
          else
            cd /home/ly0kos/work/test_env
            git fetch --all --prune
            git checkout master
            git pull origin master
          fi
          
          echo "✓ Main worktree ready"

      # 第二步：为PR分支创建工作树
      - name: Create worktree for PR branch
        run: |
          echo "=== Creating worktree for PR branch: ${{ github.head_ref }} ==="
          cd /home/ly0kos/work/test_env
          
          # 清理可能存在的旧工作树
          WORKTREE_DIR="/home/ly0kos/work/test_env/worktrees/${{ github.head_ref }}"
          if [ -d "$WORKTREE_DIR" ]; then
            echo "Removing existing worktree..."
            git worktree remove "$WORKTREE_DIR" --force 2>/dev/null || true
            rm -rf "$WORKTREE_DIR" 2>/dev/null || true
          fi
          
          # 创建新的工作树
          mkdir -p "/home/ly0kos/work/test_env/worktrees"
          git fetch origin ${{ github.head_ref }}:${{ github.head_ref }} --force
          git worktree add "/home/ly0kos/work/test_env/worktrees/${{ github.head_ref }}" ${{ github.head_ref }} --force
          
          echo "✓ Worktree created at: /home/ly0kos/work/test_env/worktrees/${{ github.head_ref }}"

      # 第三步：复用共享虚拟环境并检查依赖更新
      - name: Reuse shared virtual environment
        run: |
          echo "=== Reusing shared virtual environment ==="
          cd /home/ly0kos/work/test_env
          WORKTREE_DIR="/home/ly0kos/work/test_env/worktrees/${{ github.head_ref }}"
          
          # 使用主工作树的共享虚拟环境
          source .venv-shared/bin/activate
          
          # 检查PR分支的requirements.txt是否有更新
          if [ -f "$WORKTREE_DIR/requirements.txt" ]; then
            echo "Checking for dependency updates in PR branch..."
            REQ_MOD_TIME=$(stat -c %Y "$WORKTREE_DIR/requirements.txt" 2>/dev/null || echo 0)
            VENV_MOD_TIME=$(cat .venv-shared/last_update.txt 2>/dev/null || echo 0)
            
            if [ $REQ_MOD_TIME -gt $VENV_MOD_TIME ]; then
              echo "PR branch requirements updated, installing new dependencies..."
              pip install -r "$WORKTREE_DIR/requirements.txt"
              echo $REQ_MOD_TIME > .venv-shared/last_update.txt
              echo "Dependencies updated from PR branch"
            else
              echo "No changes in requirements, using existing packages"
            fi
          fi
          
          echo "Python: $(python --version)"
          echo "Sphinx: $(sphinx-build --version 2>/dev/null || echo 'Not installed')"

      # 第四步：在工作树中构建文档
      - name: Build documentation in worktree
        run: |
          echo "=== Building documentation in worktree ==="
          cd /home/ly0kos/work/test_env
          WORKTREE_DIR="/home/ly0kos/work/test_env/worktrees/${{ github.head_ref }}"
          source .venv-shared/bin/activate
          
          # 构建文档
          cd "$WORKTREE_DIR"
          sphinx-build -b html source "$WORKTREE_DIR/build" -v -j 4
          
          # 复制到预览位置
          mkdir -p /home/ly0kos/work/test_env/pr-builds/
          rm -rf /home/ly0kos/work/test_env/pr-builds/${{ github.head_ref }}
          cp -r "$WORKTREE_DIR/build" /home/ly0kos/work/test_env/pr-builds/${{ github.head_ref }}
          
          echo "✓ Build completed for PR branch: ${{ github.head_ref }}"

      # 第五步：验证构建结果
      - name: Verify build
        run: |
          echo "Build output for PR ${{ github.head_ref }}:"
          ls -la /home/ly0kos/work/test_env/pr-builds/${{ github.head_ref }}/ | head -10