name: EasyTier Docker

on:
  workflow_dispatch:
    inputs:
      run_id:
        description: 'The run id of EasyTier-Core Action in EasyTier repo'
        type: number
        default: 10228239965
        required: true
      image_tag:
        description: 'Tag for this image build'
        type: string
        default: 'v2.6.0'
        required: true
      mark_latest:
        description: 'Mark this image as latest'
        type: boolean
        default: false
        required: true
      mark_unstable:
        description: 'Mark this image as unstable'
        type: boolean
        default: false
        required: true

jobs:
  docker:
    if: contains('["KKRainbow"]', github.actor)
    runs-on: ubuntu-latest
    steps:
      -
        name: Checkout
        uses: actions/checkout@v5
      -
        name: Validate inputs
        run: |
          if [[ "${{ inputs.mark_latest }}" == "true" && "${{ inputs.mark_unstable }}" == "true" ]]; then
            echo "Error: mark_latest and mark_unstable cannot both be true"
            exit 1
          fi
      -
        name: Set up QEMU
        uses: docker/setup-qemu-action@v3
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      -
        name: Login to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      - name: login github container registry
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - name: Download artifact
        id: download-artifact
        uses: dawidd6/action-download-artifact@v11
        with:
          github_token: ${{secrets.GITHUB_TOKEN}}
          run_id: ${{ inputs.run_id }}
          repo: EasyTier/EasyTier
          path: docker_context
      - name: List files
        run: |
          ls -l -R .
      - name: Prepare Docker tags
        id: tags
        run: |
          # Base tags with version
          DOCKERHUB_TAGS="easytier/easytier:${{ inputs.image_tag }}"
          GHCR_TAGS="ghcr.io/easytier/easytier:${{ inputs.image_tag }}"

          # Add latest tags if requested
          if [[ "${{ inputs.mark_latest }}" == "true" ]]; then
            DOCKERHUB_TAGS="${DOCKERHUB_TAGS},easytier/easytier:latest"
            GHCR_TAGS="${GHCR_TAGS},ghcr.io/easytier/easytier:latest"
          fi

          # Add unstable tags if requested
          if [[ "${{ inputs.mark_unstable }}" == "true" ]]; then
            DOCKERHUB_TAGS="${DOCKERHUB_TAGS},easytier/easytier:unstable"
            GHCR_TAGS="${GHCR_TAGS},ghcr.io/easytier/easytier:unstable"
          fi

          # Combine all tags
          ALL_TAGS="${DOCKERHUB_TAGS},${GHCR_TAGS}"

          echo "tags=${ALL_TAGS}" >> $GITHUB_OUTPUT
          echo "Generated tags: ${ALL_TAGS}"
      -
        name: Build and push
        uses: docker/build-push-action@v6
        with:
          context: ./docker_context
          platforms: linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64,linux/riscv64
          push: true
          file: .github/workflows/Dockerfile
          tags: ${{ steps.tags.outputs.tags }}
