name: Deploy to DigitalOcean Droplet

on:
  push:
    branches:
      - main
  workflow_dispatch:

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

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

      - name: Setup Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.11'

      - name: Install dependencies
        run: npm ci

      - name: Generate dashboard
        run: python3 generate_index.py

      - name: Deploy to Droplet
        uses: appleboy/ssh-action@v1.0.0
        with:
          host: ${{ secrets.DROPLET_HOST }}
          username: ${{ secrets.DROPLET_USER }}
          key: ${{ secrets.DROPLET_SSH_KEY }}
          script: |
            # Navigate to deployment directory
            cd /var/www/agents.jeffemmett.com || exit 1

            # Pull latest changes
            git fetch origin main
            git reset --hard origin/main

            # Install dependencies and generate dashboard
            npm ci
            python3 generate_index.py

            # Set proper permissions
            chown -R www-data:www-data /var/www/agents.jeffemmett.com

            # Reload nginx
            systemctl reload nginx

            echo "✅ Deployment completed successfully!"

      - name: Notify deployment status
        if: always()
        run: |
          if [ ${{ job.status }} == 'success' ]; then
            echo "🚀 Successfully deployed to https://agents.jeffemmett.com"
          else
            echo "❌ Deployment failed"
            exit 1
          fi
