name: Build and Push to Docker Hub

on:
  push:
    branches:
      - main

env:
  PROJECT_NAME: eggs

permissions:
  contents: write

jobs:
  build-test-push:
    runs-on: ubuntu-latest
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Copy env
        run: |
          cp .env.example .env

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Build Docker image
        uses: docker/build-push-action@v5
        with:
          context: .
          file: ./Dockerfile
          push: false
          load: true
          tags: ${{ env.PROJECT_NAME }}:test
          no-cache: true


      - name: Login to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}

      - name: Tag and push to Docker Hub
        run: |
          docker tag ${{ env.PROJECT_NAME }}:test ${{ secrets.DOCKER_USERNAME }}/${{ env.PROJECT_NAME }}:latest
          docker tag ${{ env.PROJECT_NAME }}:test ${{ secrets.DOCKER_USERNAME }}/${{ env.PROJECT_NAME }}:${{ github.run_number }}
          docker push ${{ secrets.DOCKER_USERNAME }}/${{ env.PROJECT_NAME }}:latest
          docker push ${{ secrets.DOCKER_USERNAME }}/${{ env.PROJECT_NAME }}:${{ github.run_number }}
          echo "SUCCESS TAGS: latest, ${{ github.run_number }}"

      - name: stack.yaml updated
        run: |
          sed -i 's|image: ${{ secrets.DOCKER_USERNAME }}/${{ env.PROJECT_NAME }}:.*|image: ${{ secrets.DOCKER_USERNAME }}/${{ env.PROJECT_NAME }}:${{ github.run_number }}|' stack.yaml
          sed -i 's/return HttpResponse("OK.*"/return HttpResponse("OK: #${{ github.sha }}"/' routes/common.py

      - name: Commit and push updated version
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git add .
          git commit -m "🔄 image to ${{ github.run_number }} [CI SKIP]" || echo "No changes"
          git pull origin main --rebase
          git push origin main

      - name: Execute remote SSH commands using SSH key
        uses: appleboy/ssh-action@v1.2.2
        with:
          host: ${{ secrets.HOST }}
          username: ${{ secrets.USERNAME }}
          # key: ${{ secrets.KEY }}
          password: ${{ secrets.PASSWORD }}
          port: ${{ secrets.PORT }}
          script: |
            sudo su
            PROJECTS=/opt/projects/
            DIR=/opt/projects/${{ env.PROJECT_NAME }}/
            
            if [ -d "$PROJECTS" ]; then
              echo "projects papkasi mavjud"
            else
              mkdir -p $PROJECTS
              echo "projects papkasi yaratildi"
            fi

            if [ -d "$DIR" ]; then
              echo "loyiha mavjud"
            else
              cd $PROJECTS
              echo ${{ github.repository }}
              sudo git clone git@github.com:${{ github.repository }}.git ${{ env.PROJECT_NAME }}
              echo "Clone qilindi";
            fi

            cd $DIR
            sudo git fetch origin main
            sudo git reset --hard origin/main
            sudo cp .env.example .env

            update_env() {
                local env_file=".env"
                sudo cp .env.example "$env_file"

                # argumentlar orqali key=value olish
                for kv in "$@"; do
                    local key="${kv%%=*}"
                    local value="${kv#*=}"
                    sudo sed -i "s|^$key=.*|$key=$value|" "$env_file"
                done
            }

            # Funksiyani chaqirish misoli
            update_env \
              "DJANGO_SECRET_KEY=${{ secrets.KEY }}" \
              "CACHE_ENABLED=True" \
              "ALLOWED_HOSTS=127.0.0.1,web,api.gold-eggs.uz" \
              "CSRF_TRUSTED_ORIGINS=http://127.0.0.1:8081,https://api.gold-eggs.uz" \
              "SMS_API_URL=${{ secrets.SMS_API_URL }}" \
              "SMS_LOGIN=${{ secrets.SMS_LOGIN }}" \
              "SMS_PASSWORD=${{ secrets.SMS_PASSWORD }}"

            sudo docker stack deploy -c stack.yaml ${{ env.PROJECT_NAME }} 

