name: Deploy to Cloudflare Pages

on:
  push:
    branches:
      - main
  workflow_dispatch:

jobs:
  deploy:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      deployments: write
    
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '21'

      - name: Install dependencies
        run: |
          cd oauth
          npm install

      - name: Build OAuth app
        run: |
          cd oauth
          npm run build
          
      - name: Copy OAuth build to static
        run: |
          mkdir -p my-blog/static/assets
          cp -r oauth/dist/assets/* my-blog/static/assets/
          cp oauth/dist/index.html my-blog/static/oauth/index.html || true
          
      - name: Cache ailog binary
        uses: actions/cache@v4
        with:
          path: ./bin
          key: ailog-bin-${{ runner.os }}
          restore-keys: |
            ailog-bin-${{ runner.os }}
            
      - name: Setup ailog binary
        run: |
          # Get expected version from Cargo.toml
          EXPECTED_VERSION=$(grep '^version' Cargo.toml | cut -d'"' -f2)
          echo "Expected version from Cargo.toml: $EXPECTED_VERSION"
          
          # Check current binary version if exists
          if [ -f "./bin/ailog" ]; then
            CURRENT_VERSION=$(./bin/ailog --version 2>/dev/null || echo "unknown")
            echo "Current binary version: $CURRENT_VERSION"
          else
            CURRENT_VERSION="none"
            echo "No binary found"
          fi
          
          # Check OS
          OS="${{ runner.os }}"
          echo "Runner OS: $OS"
          
          # Use pre-packaged binary if version matches or extract from tar.gz
          if [ "$CURRENT_VERSION" = "$EXPECTED_VERSION" ]; then
            echo "Binary is up to date"
            chmod +x ./bin/ailog
          elif [ "$OS" = "Linux" ] && [ -f "./bin/ailog-linux-x86_64.tar.gz" ]; then
            echo "Extracting ailog from pre-packaged tar.gz..."
            cd bin
            tar -xzf ailog-linux-x86_64.tar.gz
            chmod +x ailog
            cd ..
            
            # Verify extracted version
            EXTRACTED_VERSION=$(./bin/ailog --version 2>/dev/null || echo "unknown")
            echo "Extracted binary version: $EXTRACTED_VERSION"
            
            if [ "$EXTRACTED_VERSION" != "$EXPECTED_VERSION" ]; then
              echo "Warning: Binary version mismatch. Expected $EXPECTED_VERSION but got $EXTRACTED_VERSION"
            fi
          else
            echo "Error: No suitable binary found for OS: $OS"
            exit 1
          fi

      - name: Build site with ailog
        run: |
          cd my-blog
          ../bin/ailog build

      - name: List public directory
        run: |
          ls -la my-blog/public/
          
      - name: Deploy to Cloudflare Pages
        uses: cloudflare/pages-action@v1
        with:
          apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
          accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
          projectName: ${{ secrets.CLOUDFLARE_PROJECT_NAME }}
          directory: my-blog/public
          gitHubToken: ${{ secrets.GITHUB_TOKEN }}
          wranglerVersion: '3'
