name: Deploy to Production

on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Repository
        uses: actions/checkout@v3

      - name: Deploy via SSH
        uses: appleboy/ssh-action@v1.0.0
        with:
          host: 68.178.166.26
          username: farmtoapmc
          key: ${{ secrets.SSH_PRIVATE_KEY }}
          port: 22
          script: |
            echo "🔄 Starting deployment when code pushed to main..."
            cd /home/farmtoapmc/public_html
            git reset --hard HEAD
            git clean -fd
            eval "$(ssh-agent -s)"
            git pull origin main
            /opt/cpanel/ea-php82/root/usr/bin/php artisan optimize:clear
            # /opt/cpanel/ea-php82/root/usr/bin/php artisan config:cache
            # /opt/cpanel/ea-php82/root/usr/bin/php artisan route:cache
            # /opt/cpanel/ea-php82/root/usr/bin/php artisan view:cache
            /opt/cpanel/ea-php82/root/usr/bin/php artisan storage:link
            echo "✅ Deployment complete."
            if [[ -n "$SSH_AGENT_PID" ]]; then
              echo "Killing ssh-agent with PID $SSH_AGENT_PID"
              kill $SSH_AGENT_PID
            fi
            exit 0
