name: Deploy to Development and Production

# 1. EN trigger, ki se sproži ob potisku na OBE veji
on:
  push:
    branches:
      - develop
      - master

# 2. EN jobs blok
jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
      # Ta korak se vedno izvede
      - name: Checkout repository
        uses: actions/checkout@v4

      # Ta korak se vedno izvede - priprava SSH ključa
      - name: Setup SSH
        run: |
          mkdir -p ~/.ssh
          echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa
          chmod 600 ~/.ssh/id_rsa                    
      
      # 3. KORAK ZA DEVELOP: Izvede se samo, če je veja 'develop'
      - name: Deploy to Development Server
        if: gitea.ref_name == 'develop'
        run: |
          echo "🚀 Deploying to Development..."
          rsync -avz --delete \
            --exclude=".git/" \
            --exclude=".gitea/" \
            --exclude=".gitignore" \
            -e "ssh -p ${{ secrets.SSH_PORT }} -i ~/.ssh/id_rsa -o StrictHostKeyChecking=no" \
            ./ \
            ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }}:${{ secrets.TARGET_DIR }}                    

      # 4. KORAK ZA MASTER: Izvede se samo, če je veja 'master'
      - name: Install lftp (master only)
        if: gitea.ref_name == 'master'
        run: |
          apt-get update
          apt-get install -y lftp

      - name: Deploy to Production Server (FTP)
        if: gitea.ref_name == 'master'
        run: |
          echo "🚀 Deploying to Production..."
          lftp -c "
            set ftp:ssl-allow no
            set ftp:passive-mode yes
            open -u ${FTP_USER},${FTP_PASS} ${FTP_HOST}
            mirror -R --delete --verbose \
              --exclude-glob .git/ \
              --exclude-glob .gitea/ \
              --exclude-glob .gitignore \
              ./ ${TARGET_MASTER_DIR}
          "
        env:
          FTP_HOST: ${{ secrets.FTP_HOST }}
          FTP_USER: ${{ secrets.FTP_USER }}
          FTP_PASS: ${{ secrets.FTP_PASS }}
          TARGET_MASTER_DIR: ${{ secrets.TARGET_MASTER_DIR }}
