name: Build and Deploy Svelte App

on:
  push:
    branches:
      - main  # Trigger the action on push to the main branch

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest

    steps:
      # Check out the repository
      - name: Checkout Repository
        uses: actions/checkout@v2

      # Set up Node.js
      - name: Set up Node.js
        uses: actions/setup-node@v2
        with:
          node-version: '23'  # You can adjust the Node.js version if needed

      # Install dependencies
      - name: Install Dependencies
        run: |
          npm install

      # Build the Svelte app
      - name: Build the Svelte app
        run: |
          npm run build

      # Deploy the build to your server (e.g., using SCP, Rsync, or FTP)
      - name: Deploy to Provider
        run: |
          sudo apt-get update  # Update package list
          sudo apt-get install -y sshpass  # Install sshpass for non-interactive SFTP
          echo "putting files to Strato server"
          sshpass -p "$DEPLOY_PASSWORD" sftp -o StrictHostKeyChecking=no "$DEPLOY_USER@$DEPLOY_SERVER:/" <<EOF
            put -r build/* 
            exit  
          EOF
        env:
          DEPLOY_PASSWORD: ${{ secrets.STRATO_PASSWORD }}  # Add your deployment password to secrets
          DEPLOY_USER: ${{ secrets.STRATO_USER }}  # Add your deploy username to secrets
          DEPLOY_SERVER: ${{ secrets.STRATO_SERVER }}  # Add your deploy server address to secrets

      # Clean up if necessary (optional step)
      - name: Clean up
        run: |
          rm -rf build/