name: Build Executor Image

on:
  push:
    branches: [main]
    paths:
      - "src/cogtainer/docker/**"
      - "src/cogos/**"
      - "src/cogents/**"
      - "pyproject.toml"
      - ".github/workflows/docker-build-executor.yml"
      - ".github/actions/ecr-build/**"
  workflow_dispatch:

permissions:
  id-token: write
  contents: write

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Generate CI app token
        id: app-token
        uses: actions/create-github-app-token@v1
        with:
          app-id: ${{ secrets.APP_ID }}
          private-key: ${{ secrets.APP_PRIVATE_KEY }}

      - uses: actions/checkout@v4

      - name: Build and push executor image
        id: build
        uses: ./.github/actions/ecr-build
        with:
          image_name: cogent
          dockerfile: src/cogtainer/docker/Dockerfile
          context: .
          aws_role: ${{ vars.AWS_ROLE }}
          tag_prefix: executor

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.12"

      - name: Install uv
        uses: astral-sh/setup-uv@v4

      - name: Package Lambda zip
        run: |
          DEPS_DIR=$(mktemp -d)
          uv pip install --target "$DEPS_DIR" --quiet --python 3.12 --python-platform linux \
            pydantic pydantic-settings pydantic-core annotated-types \
            Pillow google-genai anthropic asana

          cd src
          zip -qr /tmp/lambda.zip . -x '*/__pycache__/*' '*.pyc' '*.egg-info/*'
          cd "$DEPS_DIR"
          zip -qr /tmp/lambda.zip . -x '*/__pycache__/*' '*.pyc'

          echo "SIZE=$(du -h /tmp/lambda.zip | cut -f1)" >> "$GITHUB_ENV"

      - name: Upload Lambda zip to S3
        uses: ./.github/actions/s3-upload
        with:
          aws_role: ${{ vars.AWS_ROLE }}
          source_path: /tmp/lambda.zip
          s3_key: lambda/${{ github.sha }}/lambda.zip

      - name: Summary
        run: |
          echo "### Executor Built" >> "$GITHUB_STEP_SUMMARY"
          echo "" >> "$GITHUB_STEP_SUMMARY"
          echo "**Docker Tags:**" >> "$GITHUB_STEP_SUMMARY"
          echo '```' >> "$GITHUB_STEP_SUMMARY"
          echo "${{ steps.build.outputs.image_uri }}" >> "$GITHUB_STEP_SUMMARY"
          echo '```' >> "$GITHUB_STEP_SUMMARY"
          echo "" >> "$GITHUB_STEP_SUMMARY"
          echo "**Lambda Zip:** \`s3://cogent-polis-ci-artifacts/lambda/${{ github.sha }}/lambda.zip\` (${{ env.SIZE }})" >> "$GITHUB_STEP_SUMMARY"
          echo "" >> "$GITHUB_STEP_SUMMARY"
          echo "**Deploy:** \`cogos <name> cogtainer update ecs --tag executor-${{ steps.build.outputs.short_sha }}\`" >> "$GITHUB_STEP_SUMMARY"

      - name: Update versions defaults (executor)
        uses: ./.github/actions/update-versions
        with:
          component: executor
          version: ${{ steps.build.outputs.short_sha }}
          token: ${{ steps.app-token.outputs.token }}

      - name: Update versions defaults (lambda)
        uses: ./.github/actions/update-versions
        with:
          component: lambda
          version: ${{ github.sha }}
          token: ${{ steps.app-token.outputs.token }}

      - name: Update versions defaults (cogos)
        uses: ./.github/actions/update-versions
        with:
          component: cogos
          version: ${{ github.sha }}
          token: ${{ steps.app-token.outputs.token }}
