name: Build and Push Docker Image

on:
  push:
    branches: [master, main]
    # 仅当构建相关文件变更时触发，避免无意义排队
    paths:
      - "backend/**"
      - "frontend/**"
      - ".github/workflows/docker-build.yml"
      - "docker-compose*.yml"
  workflow_dispatch:
    inputs:
      build_backend:
        description: "手动触发时是否构建后端"
        required: false
        default: true
        type: boolean
      build_frontend:
        description: "手动触发时是否构建前端"
        required: false
        default: true
        type: boolean

# 同一分支只保留最新一次，减少等待队列
concurrency:
  group: docker-build-${{ github.ref }}
  cancel-in-progress: true

jobs:
  changes:
    runs-on: ubuntu-latest
    outputs:
      backend: ${{ steps.filter.outputs.backend }}
      frontend: ${{ steps.filter.outputs.frontend }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Detect changed paths
        id: filter
        uses: dorny/paths-filter@v3
        with:
          filters: |
            backend:
              - "backend/**"
              - ".github/workflows/docker-build.yml"
              - "docker-compose*.yml"
            frontend:
              - "frontend/**"
              - ".github/workflows/docker-build.yml"
              - "docker-compose*.yml"

  build-backend:
    runs-on: ubuntu-latest
    needs: changes
    if: |
      github.event_name == 'workflow_dispatch' && inputs.build_backend == true ||
      github.event_name != 'workflow_dispatch' && needs.changes.outputs.backend == 'true'
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

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

      - name: Log in to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Build and push backend
        uses: docker/build-push-action@v6
        with:
          context: ./backend
          file: ./backend/Dockerfile
          push: true
          tags: |
            537yaha/ai-cs-backend:latest
          platforms: linux/amd64,linux/arm64
          # 使用 GitHub Actions 缓存构建层，显著减少重复构建时间
          cache-from: type=gha
          cache-to: type=gha,mode=max

  build-frontend:
    runs-on: ubuntu-latest
    needs: changes
    if: |
      github.event_name == 'workflow_dispatch' && inputs.build_frontend == true ||
      github.event_name != 'workflow_dispatch' && needs.changes.outputs.frontend == 'true'
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

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

      - name: Log in to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Build and push frontend
        uses: docker/build-push-action@v6
        with:
          context: ./frontend
          file: ./frontend/Dockerfile
          push: true
          tags: |
            537yaha/ai-cs-frontend:latest
          platforms: linux/amd64,linux/arm64
          # 前端统一走同域 /api（由反向代理转发到后端），无需在构建时注入 localhost/端口
          cache-from: type=gha
          cache-to: type=gha,mode=max