name: AI Service CI/CD

on:
  push:
    branches: [main]
    paths:
      - 'ai/**'
      - 'ai/.github/workflows/ai.yml'
  pull_request:
    branches: [main]
    paths:
      - 'ai/**'
      - 'ai/.github/workflows/ai.yml'

defaults:
  run:
    working-directory: ai

jobs:
  test:
    name: Lint & Test
    runs-on: ubuntu-latest
    strategy:
      fail-fast: true
      matrix:
        python-version: ['3.11']
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Setup Ubuntu apt mirrors
        run: |
          sudo tee /etc/apt/sources.list > /dev/null <<'EOF'
          deb [trusted=yes] https://mirror2.chabokan.net/ubuntu jammy main restricted universe multiverse
          deb [trusted=yes] https://mirror2.chabokan.net/ubuntu jammy-updates main restricted universe multiverse
          deb [trusted=yes] https://mirror2.chabokan.net/ubuntu jammy-security main restricted universe multiverse
          EOF
          sudo apt-get update

      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}

      - name: Setup Python pip mirrors
        run: |
          pip config --user set global.index-url https://package-mirror.liara.ir/repository/pypi/simple
          pip config --user set global.extra-index-url https://mirror.cdn.ir/repository/pypi/simple
          pip config --user set global.trusted-host "package-mirror.liara.ir mirror.cdn.ir mirror2.chabokan.net"

      - name: Cache pip dependencies
        uses: actions/cache@v4
        with:
          path: ~/.cache/pip
          key: ${{ runner.os }}-pip-ai-${{ hashFiles('ai/requirements.txt') }}
          restore-keys: |
            ${{ runner.os }}-pip-ai-

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt
          pip install pytest flake8

      - name: Run lint
        run: flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics

      - name: Run tests
        run: pytest --tb=short -q

  docker:
    name: Build & Push Docker Image
    needs: test
    if: github.event_name == 'push' && github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

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

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

      - name: Build and push
        uses: docker/build-push-action@v6
        with:
          context: ./ai
          push: true
          tags: |
            ${{ secrets.DOCKER_REGISTRY }}/ai:latest
            ${{ secrets.DOCKER_REGISTRY }}/ai:${{ github.sha }}
          build-args: |
            APT_MIRROR=mirror2.chabokan.net
          cache-from: type=gha
          cache-to: type=gha,mode=max

  deploy:
    name: Deploy AI Service
    needs: docker
    if: github.event_name == 'push' && github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    steps:
      - name: Deploy via SSH
        uses: appleboy/ssh-action@v1
        with:
          host: ${{ secrets.SSH_HOST }}
          username: ${{ secrets.SSH_USER }}
          port: ${{ secrets.SSH_PORT }}
          key: ${{ secrets.SSH_PRIVATE_KEY }}
          script: |
            cd /opt/myproject/ai
            git pull origin main
            docker compose pull
            docker compose up -d --remove-orphans
