name: Conda

on:
  workflow_dispatch:
    inputs:
      label:
        description: "Anaconda label"
        required: true
        type: string
      build_number:
        description: "Build number"
        required: true
        type: string

jobs:
  build:
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-latest
            python-version: "3.12"
            arch: x86_64
          - os: ubuntu-latest
            python-version: "3.13"
            arch: x86_64
          - os: macos-latest
            python-version: "3.12"
            arch: arm64
          - os: macos-latest
            python-version: "3.13"
            arch: arm64
          - os: macos-latest
            python-version: "3.12"
            arch: x86_64
          - os: macos-latest
            python-version: "3.13"
            arch: x86_64
          - os: windows-latest
            python-version: "3.12"
            arch: x86_64
          - os: windows-latest
            python-version: "3.13"
            arch: x86_64
    runs-on: ${{ matrix.os }}
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Version
        shell: bash -l {0}
        run: |
          VERSION=$(python -c "import re; print(next(re.finditer(r'CLINGO_VERSION \"([0-9.]+)\"', open('lib/c-api/include/clingo/core.h').read())).group(1))")
          echo "VERSION_NUMBER=$VERSION" >> $GITHUB_ENV

      - name: Build number
        shell: bash -l {0}
        run: |
          if [ "${{ inputs.build_number }}" == "auto" ]; then
            echo "BUILD_NUMBER=${{ github.run_number }}" >> $GITHUB_ENV
          else
            echo "BUILD_NUMBER=${{ inputs.build_number }}" >> $GITHUB_ENV
          fi

      - name: Windows re2c
        if: ${{ runner.os == 'windows' }}
        shell: powershell
        run: |
          choco install re2c

      - name: Conda
        if: ${{ runner.os != 'linux' }}
        uses: conda-incubator/setup-miniconda@v3
        with:
          python-version: ${{ matrix.python-version }}
          channels: conda-forge
          channel-priority: strict
          auto-update-conda: true

      - name: Build (linux)
        if: ${{ runner.os == 'linux' }}
        run: |
          mkdir artifacts
          chmod a+rwx artifacts
          docker run --rm \
            -v ${{ github.workspace }}:/work \
            -v ${{ github.workspace }}/artifacts:/artifacts \
            quay.io/condaforge/linux-anvil-cos7-x86_64 /bin/bash -c "
              export VERSION_NUMBER=${{ env.VERSION_NUMBER }} && \
              export BUILD_NUMBER=${{ env.BUILD_NUMBER }} && \
              curl -L -o miniconda.sh https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-Linux-x86_64.sh && \
              bash miniconda.sh -b && \
              source ~/miniforge3/etc/profile.d/conda.sh && \
              conda activate base && \
              conda install -y tree conda-build && \
              conda build /work/.github/conda --output-folder artifacts --python ${{ matrix.python-version }} && \
              find artifacts -type f -name '*.conda' -exec cp {} /artifacts/ \;
            "
      - name: Build (macos_x86_64)
        shell: bash -l {0}
        if: ${{ runner.os == 'macos' && matrix.arch == 'x86_64' }}
        run: |
          conda create --platform osx-64 -n build conda-build
          conda activate build
          arch -x86_64 conda build .github/conda \
            --output-folder ./artifacts \
            --python "${{ matrix.python-version }}"
          find artifacts -type f ! -name "*.conda" -delete
          find artifacts -type d -empty -delete

      - name: Build (windows/macos_arm64)
        shell: bash -l {0}
        if: ${{ runner.os != 'linux' && (runner.os != 'macos' || matrix.arch != 'x86_64') }}
        run: |
          conda create -n build conda-build
          conda activate build
          conda build .github/conda \
            --output-folder ./artifacts \
            --python "${{ matrix.python-version }}"
          find artifacts -type f ! -name "*.conda" -delete
          find artifacts -type d -empty -delete

      - name: Upload
        uses: actions/upload-artifact@v4
        with:
          name: conda-packages-${{ matrix.os }}-${{ matrix.arch }}-py${{ matrix.python-version }}
          path: artifacts

  upload:
    needs:
      - build
    runs-on: ubuntu-latest
    steps:
      - name: Dowload
        uses: actions/download-artifact@v4
        with:
          path: artifacts
          merge-multiple: false

      - name: Conda
        uses: conda-incubator/setup-miniconda@v3
        with:
          channels: conda-forge
          channel-priority: strict
          activate-environment: conda-upload
          auto-update-conda: true

      - name: Install
        shell: bash -l {0}
        run: |
          conda install anaconda-client

      - name: Upload
        shell: bash -l {0}
        env:
          ANACONDA_API_TOKEN: ${{ secrets.ANACONDA_TOKEN }}
        run: |
          files=( $(find artifacts -type f -name "*.conda") )
          anaconda upload -l "${{ inputs.label }}" -u potassco --force "${files[@]}"
