name: Deploy Ingest Server

on:
  push:
    tags:
      - 'v[0-9]+.[0-9]+.[0-9]+'
  workflow_dispatch:

env:
  IMAGE_NAME: qctrace-ingest

jobs:
  deploy:
    runs-on: ubuntu-latest
    needs: []
    permissions:
      contents: read

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

      - name: Log in to Azure Container Registry
        uses: docker/login-action@v3
        with:
          registry: ${{ secrets.ACR_SERVER }}
          username: ${{ secrets.ACR_USERNAME }}
          password: ${{ secrets.ACR_PASSWORD }}

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

      - name: Build and push image
        uses: docker/build-push-action@v5
        with:
          context: .
          file: ./Dockerfile
          push: true
          platforms: linux/amd64
          tags: |
            ${{ secrets.ACR_SERVER }}/${{ env.IMAGE_NAME }}:latest
            ${{ secrets.ACR_SERVER }}/${{ env.IMAGE_NAME }}:${{ github.ref_name }}
          cache-from: type=registry,ref=${{ secrets.ACR_SERVER }}/${{ env.IMAGE_NAME }}:buildcache
          cache-to: type=registry,ref=${{ secrets.ACR_SERVER }}/${{ env.IMAGE_NAME }}:buildcache,mode=max

      - name: Azure Login
        uses: azure/login@v1
        with:
          creds: ${{ secrets.AZURE_CREDENTIALS }}

      - name: Configure Container Registry
        run: |
          az containerapp registry set \
            --name qctrace-pilot-ingest \
            --resource-group quickcall-rg \
            --server ${{ secrets.ACR_SERVER }} \
            --username ${{ secrets.ACR_USERNAME }} \
            --password ${{ secrets.ACR_PASSWORD }}

      - name: Deploy to Azure Container Apps
        run: |
          az containerapp update \
            --name qctrace-pilot-ingest \
            --resource-group quickcall-rg \
            --image ${{ secrets.ACR_SERVER }}/${{ env.IMAGE_NAME }}:${{ github.ref_name }} \
            --min-replicas 1 \
            --max-replicas 3 \
            --set-env-vars \
              QC_TRACE_DSN="${{ secrets.QC_TRACE_DSN }}" \
              QC_TRACE_HOST="0.0.0.0" \
              QC_TRACE_PORT="19777" \
              QC_TRACE_ADMIN_KEYS="${{ secrets.QC_TRACE_ADMIN_KEYS }}" \
              QC_TRACE_PUSH_KEYS="${{ secrets.QC_TRACE_PUSH_KEYS }}"

      - name: Configure Ingress
        run: |
          az containerapp ingress enable \
            --name qctrace-pilot-ingest \
            --resource-group quickcall-rg \
            --type external \
            --target-port 19777 \
            --transport http

      - name: Verify deployment
        run: |
          APP_URL=$(az containerapp show \
            --name qctrace-pilot-ingest \
            --resource-group quickcall-rg \
            --query properties.configuration.ingress.fqdn -o tsv)
          echo "Deployed at: https://$APP_URL"
          echo "Health check: https://$APP_URL/health"

          # Wait for container to be ready
          sleep 10
          curl -sf "https://$APP_URL/health" || echo "Health check pending - container may still be starting"

      - name: Azure Logout
        run: az logout
