name: Docker Manual Publish

on:
  workflow_dispatch:
    inputs:
      image_tag:
        description: 'Image tag version (e.g., v1.0.0, latest)'
        required: true
        default: 'v1.0.0'

concurrency:
  group: docker-manual-publish-${{ github.ref }}-${{ inputs.image_tag }}
  cancel-in-progress: true

env:
  GHCR_REGISTRY: ghcr.io
  DOCKERHUB_REGISTRY: docker.io
  GHCR_IMAGE_NAME: ${{ github.repository }}
  DOCKERHUB_IMAGE_NAME: ${{ secrets.DOCKERHUB_USERNAME }}/${{ github.event.repository.name }}

jobs:
  build-and-push:
    runs-on: ubuntu-24.04
    permissions:
      contents: read
      packages: write
      id-token: write

    steps:
      - name: Checkout repository
        uses: actions/checkout@v5
        with:
          fetch-depth: 0

      - name: Validate release inputs
        run: |
          if [[ "${{ inputs.image_tag }}" =~ [[:space:]] ]]; then
            echo "image_tag must not contain spaces"
            exit 1
          fi

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
        with:
          driver-opts: |
            image=moby/buildkit:master
            network=host

      - name: Set up QEMU for multi-platform build
        uses: docker/setup-qemu-action@v3

      # 登录到 GHCR
      - name: Log in to GitHub Container Registry
        uses: docker/login-action@v3
        with:
          registry: ${{ env.GHCR_REGISTRY }}
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      # 登录到 Docker Hub（需要预先设置 secrets.DOCKERHUB_USERNAME 和 secrets.DOCKERHUB_TOKEN）
      - name: Log in to Docker Hub
        uses: docker/login-action@v3
        with:
          registry: ${{ env.DOCKERHUB_REGISTRY }}
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Extract metadata for GHCR
        id: meta-ghcr
        uses: docker/metadata-action@v5
        with:
          images: ${{ env.GHCR_REGISTRY }}/${{ env.GHCR_IMAGE_NAME }}
          tags: |
            type=raw,value=${{ inputs.image_tag }}
            type=raw,value=latest
          flavor: |
            latest=false

      - name: Extract metadata for Docker Hub
        id: meta-dockerhub
        uses: docker/metadata-action@v5
        with:
          images: ${{ env.DOCKERHUB_REGISTRY }}/${{ env.DOCKERHUB_IMAGE_NAME }}
          tags: |
            type=raw,value=${{ inputs.image_tag }}
            type=raw,value=latest
          flavor: |
            latest=false

      - name: Pre-publish docker smoke
        run: |
          docker build -t stock-analysis:manual -f docker/Dockerfile .
          docker run --rm stock-analysis:manual python -c "
          from src.config import get_config; print('ok-config')
          from src.storage import DatabaseManager; print('ok-storage')
          from src.notification import NotificationService; print('ok-notification')
          from data_provider import DataFetcherManager; print('ok-data-provider')
          from src.analyzer import GeminiAnalyzer; print('ok-analyzer')
          print('manual-smoke-ok')
          "

      - name: Build and push multi-arch images
        uses: docker/build-push-action@v5
        with:
          context: .
          file: docker/Dockerfile
          platforms: linux/amd64,linux/arm64
          push: true
          tags: |
            ${{ steps.meta-ghcr.outputs.tags }}
            ${{ steps.meta-dockerhub.outputs.tags }}
          labels: ${{ steps.meta-ghcr.outputs.labels }}
          cache-from: type=gha
          cache-to: type=gha,mode=max

      - name: Generate summary
        run: |
          echo "### Docker images built and pushed successfully" >> "$GITHUB_STEP_SUMMARY"
          echo "" >> "$GITHUB_STEP_SUMMARY"
          echo "- GHCR: \`${{ env.GHCR_REGISTRY }}/${{ env.GHCR_IMAGE_NAME }}\`" >> "$GITHUB_STEP_SUMMARY"
          echo "- Docker Hub: \`${{ env.DOCKERHUB_REGISTRY }}/${{ env.DOCKERHUB_IMAGE_NAME }}\`" >> "$GITHUB_STEP_SUMMARY"
          echo "- Tags: ${{ inputs.image_tag }}, latest" >> "$GITHUB_STEP_SUMMARY"
          echo "- Platforms: linux/amd64, linux/arm64" >> "$GITHUB_STEP_SUMMARY"
          echo "" >> "$GITHUB_STEP_SUMMARY"
          echo "### Pull commands" >> "$GITHUB_STEP_SUMMARY"
          echo '```bash' >> "$GITHUB_STEP_SUMMARY"
          echo "docker pull ${{ env.GHCR_REGISTRY }}/${{ env.GHCR_IMAGE_NAME }}:${{ inputs.image_tag }}" >> "$GITHUB_STEP_SUMMARY"
          echo "docker pull ${{ env.DOCKERHUB_REGISTRY }}/${{ env.DOCKERHUB_IMAGE_NAME }}:${{ inputs.image_tag }}" >> "$GITHUB_STEP_SUMMARY"
          echo '```' >> "$GITHUB_STEP_SUMMARY"
