name: Build and Deploy

on:
  push:
    branches: [master]

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Install Docker CLI
        run: |
          apt-get update
          apt-get install -y ca-certificates curl gnupg
          install -m 0755 -d /etc/apt/keyrings
          curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
          chmod a+r /etc/apt/keyrings/docker.gpg

          echo \
          "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian \
          bullseye stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null

          apt-get update
          apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin
        
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2
        
      - name: Login to Harbor Registry
        uses: docker/login-action@v2
        with:
          registry: harbor.galpodlipnik.com
          username: ${{ secrets.HARBOR_USERNAME }}
          password: ${{ secrets.HARBOR_PASSWORD }}
          
      - name: Build and push Backend image
        uses: docker/build-push-action@v4
        with:
          context: ./backend
          push: true
          tags: harbor.galpodlipnik.com/chat-app/backend:latest
          
      - name: Build and push Frontend image
        uses: docker/build-push-action@v4
        with:
          context: ./frontend
          push: true
          tags: harbor.galpodlipnik.com/chat-app/frontend:latest
          
      - name: Create docker-compose.yml
        run: |
          cat > docker-compose.yml << 'EOL'
            services:
              backend:
                image: harbor.galpodlipnik.com/chat-app/backend:latest
                restart: always
                environment:
                  - PORT=3000
                  - DATABASE_URL=postgresql://chat:veryverysecure@31.15.169.148:5432/chat
                  - JWT_SECRET=supersecretkey
                networks:
                  - chat-network

              frontend:
                image: harbor.galpodlipnik.com/chat-app/frontend:latest
                restart: always
                ports:
                  - "5173:80"
                networks:
                  - chat-network

            networks:
              chat-network:
                driver: bridge
          EOL
          
      - name: Deploy with SSH
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.DEPLOY_HOST }}
          username: ${{ secrets.DEPLOY_USERNAME }}
          key: ${{ secrets.DEPLOY_SSH_KEY }}
          script_stop: true
          script: |
            mkdir -p ~/services/chat-app
            cd ~/services/chat-app
          
      - name: Copy docker-compose.yml
        uses: appleboy/scp-action@master
        with:
          host: ${{ secrets.DEPLOY_HOST }}
          username: ${{ secrets.DEPLOY_USERNAME }}
          key: ${{ secrets.DEPLOY_SSH_KEY }}
          source: "docker-compose.yml"
          target: "~/services/chat-app"
          
      - name: Complete Deployment
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.DEPLOY_HOST }}
          username: ${{ secrets.DEPLOY_USERNAME }}
          key: ${{ secrets.DEPLOY_SSH_KEY }}
          script: |
            cd ~/services/chat-app
            
            # Login to Harbor registry
            echo "${{ secrets.HARBOR_PASSWORD }}" | docker login harbor.galpodlipnik.com -u "${{ secrets.HARBOR_USERNAME }}" --password-stdin
            
            # Pull latest images and deploy
            docker compose pull
            docker compose down
            docker compose up -d