name: Build and Deploy Documentation

on:
  push:
    branches:
      - main
    paths:
      - 'Docs/**'
  workflow_dispatch:

jobs:
  build-and-deploy-docs:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup Bun
        uses: oven-sh/setup-bun@v1
        with:
          bun-version: latest

      - name: Install dependencies
        working-directory: ./Docs
        run: bun install

      - name: Build VitePress site
        working-directory: ./Docs
        run: bun run docs:build 

      - name: Prepare target directory on server
        uses: appleboy/ssh-action@v1.0.3
        with:
          host: ${{ secrets.SERVER_HOST }}
          username: ${{ secrets.SERVER_USERNAME }}
          key: ${{ secrets.SERVER_SSH_KEY }}
          passphrase: ${{ secrets.SSH_PASSPHRASE }}
          script_stop: true 
          script: |
            mkdir -p /opt/1panel/www/sites/foxel.cc/index
            rm -rf /opt/1panel/www/sites/foxel.cc/index/*

      - name: Upload documentation to server
        uses: appleboy/scp-action@v1
        with:
          host: ${{ secrets.SERVER_HOST }}
          username: ${{ secrets.SERVER_USERNAME }}
          key: ${{ secrets.SERVER_SSH_KEY }}
          passphrase: ${{ secrets.SSH_PASSPHRASE }}
          source: "Docs/.vitepress/dist/*" 
          target: "/opt/1panel/www/sites/foxel.cc/index"