name: Deployment to VPS

on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest

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

    - name: Setup Node.js
      uses: actions/setup-node@v2
      with:
        node-version: 18

    - name: Build client
      run: |
        cd client
        echo "VITE_API_URL=https://touch-programming.hazemkrimi.tech/api" > .env.production
        npm install
        npm run build

    - name: Setup Go
      uses: actions/setup-go@v5
      with:
        go-version: '1.23.x'

    - name: Build server
      run: |
        cd server
        echo -e "LLM_MODEL=llama3.2:1b\nMAX_LINES=15" >> .env
        CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o executable
      
    - name: Setup SSH
      uses: webfactory/ssh-agent@v0.9.0
      with:
        ssh-private-key: ${{ secrets.DEPLOY_SSH_KEY }}

    - name: Deploy to VPS
      env: 
        SSH_KEY: ${{ secrets.DEPLOY_SSH_KEY }}
      run: |
        ssh-keyscan -H hazemkrimi.tech >> ~/.ssh/known_hosts
        rsync -avz --delete client/build/* deploy@hazemkrimi.tech:/var/www/touch-programming.hazemkrimi.tech/public
        rsync -avz --delete server/executable deploy@hazemkrimi.tech:/var/www/touch-programming.hazemkrimi.tech/api
        rsync -avz --delete server/.env deploy@hazemkrimi.tech:/var/www/touch-programming.hazemkrimi.tech/api
