name: Deploy SecondTech

on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: self-hosted

    steps:
      - name: Pull latest code
        run: |
          cd /var/www/secondtech
          git fetch origin main
          git reset --hard origin/main

      - name: Install dependencies
        run: |
          cd /var/www/secondtech
          pnpm install

      - name: Build frontend
        run: |
          cd /var/www/secondtech
          pnpm run build

      - name: Restart backend
        run: |
          cd /var/www/secondtech
          pm2 restart secondtech-api || pm2 start "pnpm run start" --name secondtech-api
          pm2 save

      - name: Reload nginx
        run: |
          nginx -t
          systemctl reload nginx
