# Docs for the Azure Web Apps Deploy action: https://github.com/Azure/webapps-deploy
# More GitHub Actions for Azure: https://github.com/Azure/actions
# More info on Python, GitHub Actions, and Azure App Service: https://aka.ms/python-webapps-actions

name: Deploy AI Service to Azure App Service - codeflash-aiservice-dev.azurewebsites.net

on:
  push:
    branches:
      - main
    paths:
      - "django/aiservice/**"
      - ".github/workflows/deploy_aiservice_to_azure.yml"
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v6

        #      - name: Set up Python version
        #        uses: actions/setup-python@v1
        #        with:
        #          python-version: '3.11'
        #
        #      - name: Create and start virtual environment
        #        run: |
        #          python -m venv venv
        #          source venv/bin/activate

        #      - name: Install dependencies
        #        run: |
        #          cd django/aiservice
        #          python -m pip install --upgrade pip
        #          pip install poetry
        #          poetry install

        #      - name: Publish to GitHub Packages
        #        run: |
        #          cd django/aiservice
        #          poetry config pypi-token.pypi ${{ secrets.GITHUB_TOKEN }}
        #          poetry publish --build
        #        env:
        #          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      # Optional: Add step to run tests here (PyTest, Django test suites, etc.)

      #      - name: Run tests
      #        run: |
      #          cd django/aiservice
      #          poetry run pytest
      - name: Install uv
        uses: astral-sh/setup-uv@v8.1.0

      - name: Generate requirements.txt
        run: |
          cd django/aiservice
          uv pip compile pyproject.toml -o requirements.txt
        # https://learn.microsoft.com/en-us/azure/app-service/configure-language-python

      - name: Zip artifact for deployment
        run: cd django/aiservice && zip -r ../../aiservice.zip . -x '*.git*' '.venv/*' '__pycache__/*' '*.pyc'

      - name: Upload artifact for deployment jobs
        uses: actions/upload-artifact@v7
        with:
          name: aiservice-artifact
          path: |
            aiservice.zip
            !venv/

  deploy:
    runs-on: ubuntu-latest
    needs: build
    environment:
      name: "dev"
      url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}
    permissions:
      id-token: write #This is required for requesting the JWT

    steps:
      - name: Download artifact from build job
        uses: actions/download-artifact@v8
        with:
          name: aiservice-artifact

      - name: Unzip artifact for deployment
        run: unzip aiservice.zip

      #      - name: Install aiservice package from GitHub Packages
      #        run: |
      #          python -m pip install --index-url https://pypi.org/simple --extra-index-url https://npm.pkg.github.com/codeflash-ai aiservice
      #        env:
      #          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Login to Azure
        uses: azure/login@v3
        with:
          client-id: ${{ secrets.AZURE_CLIENT_ID }}
          tenant-id: ${{ secrets.AZURE_TENANT_ID }}
          subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
      #        with:
      #          client-id: ${{ secrets.AZUREAPPSERVICE_CLIENTID_897D7C1175A641AF841798FA6EB00A05 }}
      #          tenant-id: ${{ secrets.AZUREAPPSERVICE_TENANTID_4AC4B6C41D1C4C5EA848BB4C1E09AE16 }}
      #          subscription-id: ${{ secrets.AZUREAPPSERVICE_SUBSCRIPTIONID_7F61AF52B6434A77B01AEC73C1E034FC }}

      - name: "Deploy to Azure App Service - codeflash-aiservice-dev.azurewebsites.net"
        uses: azure/webapps-deploy@v3
        id: deploy-to-webapp
        with:
          app-name: "codeflash-aiservice"
          slot-name: "dev"
          package: .
          startup-command: "./start_gunicorn.sh"
