on:
  workflow_call:
    inputs:
      cache-key:
        required: true
        type: string
      cache-path:
        required: true
        type: string
      valid-cache:
        required: true
        type: boolean
      os:
        required: true
        type: string
      arch:
        required: false
        type: string
      build-deps-only:
        required: false
        type: boolean
      force-build:
        required: false
        type: boolean

jobs:
  build_deps:
    name: Build Deps
    if: ${{ !cancelled() && (inputs.build-deps-only || inputs.force-build || inputs.valid-cache != true) }}
    runs-on: ${{ inputs.os }}
    env:
      date:
    steps:

      # Setup the environment
      - name: Checkout
        uses: actions/checkout@v6
        with:
          lfs: 'false'

      - name: load cached deps
        uses: actions/cache@v5
        with:
          path: ${{ inputs.cache-path }}
          key: ${{ inputs.cache-key }}

      - uses: lukka/get-cmake@latest
        with:
          cmakeVersion: "~4.3.0"  # use most recent 4.3.x version
          useLocalCache: true         # <--= Use the local cache (default is 'false').
          useCloudCache: true

      - name: setup dev on Windows
        if: runner.os == 'Windows'
        uses: microsoft/setup-msbuild@v3

      - name: Get the date on Ubuntu and macOS
        if: runner.os != 'Windows'
        run: echo "date=$(date +'%Y%m%d')" >> $GITHUB_ENV
        shell: bash

      - name: Get the date on Windows
        if: runner.os == 'Windows'
        run: echo "date=$(Get-Date -Format 'yyyyMMdd')" | Out-File -Append -FilePath $env:GITHUB_ENV -Encoding utf8
        shell: pwsh


      # Build Dependencies
      - name: Build on Windows
        if: runner.os == 'Windows'
        working-directory: ${{ github.workspace }}
        run: |
            if (-not "${{ vars.SELF_HOSTED }}") {
              choco install strawberryperl
            }
            .\build_release_vs.bat deps
            .\build_release_vs.bat pack
        shell: pwsh

      - name: Build on Mac ${{ inputs.arch }}
        if: runner.os == 'macOS'
        working-directory: ${{ github.workspace }}
        run: |
            if [ -z "${{ vars.SELF_HOSTED }}" ]; then
              brew install automake texinfo libtool
            fi
            ./build_release_macos.sh -dx ${{ !vars.SELF_HOSTED && '-1' || '' }} -a ${{ inputs.arch }} -t 10.15
            (cd "${{ github.workspace }}/deps/build/${{ inputs.arch }}" && \
            find . -mindepth 1 -maxdepth 1 ! -name 'OrcaSlicer_dep' -exec rm -rf {} +)


      - name: Apt-Install Dependencies
        if: runner.os == 'Linux' && !vars.SELF_HOSTED
        uses: ./.github/actions/apt-install-deps

      - name: Build on Ubuntu
        if: runner.os == 'Linux'
        working-directory: ${{ github.workspace }}
        run: |
            mkdir -p ${{ github.workspace }}/deps/build/destdir
            ./build_linux.sh -drlL
            cd deps/build
            tar -czvf OrcaSlicer_dep_ubuntu_$(date +"%Y%m%d").tar.gz destdir


      # Upload Artifacts
      # - name: Upload Mac ${{ inputs.arch }} artifacts
      #   if: runner.os == 'macOS'
      #   uses: actions/upload-artifact@v6
      #   with:
      #     name: OrcaSlicer_dep_mac_${{ env.date }}
      #     path: ${{ github.workspace }}/deps/build/OrcaSlicer_dep*.tar.gz

      # - name: Upload Windows artifacts
      #   if: runner.os == 'Windows'
      #   uses: actions/upload-artifact@v6
      #   with:
      #     name: OrcaSlicer_dep_win64_${{ env.date }}
      #     path: ${{ github.workspace }}/deps/build/OrcaSlicer_dep*.zip

      # - name: Upload Ubuntu artifacts
      #   if: runner.os == 'Linux' && !env.ACT
      #   env:
      #     ubuntu-ver: '2404'
      #   uses: actions/upload-artifact@v6
      #   with:
      #     name: OrcaSlicer_dep_ubuntu_${{ env.ubuntu-ver }}_${{ env.date }}
      #     path: ${{ github.workspace }}/deps/build/OrcaSlicer_dep_ubuntu_*.tar.gz

  build_orca:
    name: Build OrcaSlicer
    needs: [build_deps]
    if: ${{ !cancelled() && !inputs.build-deps-only && (inputs.force-build || (inputs.valid-cache == true && needs.build_deps.result == 'skipped') || (inputs.valid-cache != true && success())) }}
    uses: ./.github/workflows/build_orca.yml
    with:
      cache-key: ${{ inputs.cache-key }}
      cache-path: ${{ inputs.cache-path }}
      os: ${{ inputs.os }}
      arch: ${{ inputs.arch }}
    secrets: inherit
