name: Install System Dependencies
description: |
  Install and cache platform-specific dependencies required for document conversion.
  Includes: Tesseract OCR, fonts, and build tools.
  Features robust caching with architecture/version awareness, timeout handling, and retry logic.

inputs:
  enable-retry:
    description: Enable retry logic with exponential backoff
    required: false
    default: "true"

runs:
  using: composite
  steps:
    - name: Detect Tesseract version (macOS)
      if: runner.os == 'macOS'
      id: detect-tesseract-macos
      shell: bash
      run: scripts/ci/install-system-deps/detect-tesseract-macos.sh

    - name: Cache Tesseract & tessdata (macOS)
      if: runner.os == 'macOS'
      id: cache-tesseract-macos
      uses: actions/cache@v5
      with:
        path: |
          /usr/local/opt/tesseract/
          /usr/local/Cellar/tesseract/
          /opt/homebrew/opt/tesseract/
          /opt/homebrew/Cellar/tesseract/
        key: tesseract-macos-${{ runner.arch }}-v5-${{ steps.detect-tesseract-macos.outputs.version }}
        restore-keys: |
          tesseract-macos-${{ runner.arch }}-v5-
          tesseract-macos-${{ runner.arch }}-

    - name: Install dependencies (macOS)
      if: runner.os == 'macOS'
      shell: bash
      run: scripts/ci/install-system-deps/install-macos.sh

    - name: Detect Tesseract version (Linux)
      if: runner.os == 'Linux'
      id: detect-tesseract-linux
      shell: bash
      run: scripts/ci/install-system-deps/detect-tesseract-linux.sh

    - name: Cache Tesseract data (Linux)
      if: runner.os == 'Linux'
      id: cache-tesseract-linux
      uses: actions/cache@v5
      with:
        path: |
          /usr/share/tesseract-ocr/5/tessdata/
          /usr/share/tesseract-ocr/tessdata/
        key: tesseract-linux-${{ runner.arch }}-v5-${{ steps.detect-tesseract-linux.outputs.version }}
        restore-keys: |
          tesseract-linux-${{ runner.arch }}-v5-
          tesseract-linux-${{ runner.arch }}-

    - name: Install dependencies (Linux)
      if: runner.os == 'Linux'
      shell: bash
      run: scripts/ci/install-system-deps/install-linux.sh

    - name: Cache Tesseract (Windows)
      if: runner.os == 'Windows'
      id: cache-tesseract-windows
      uses: actions/cache@v5
      with:
        path: |
          C:\Program Files\Tesseract-OCR
          C:\ProgramData\chocolatey\lib\tesseract
        key: tesseract-windows-${{ runner.arch }}-v5-data
        restore-keys: |
          tesseract-windows-${{ runner.arch }}-

    - name: Cache LLVM (Windows)
      if: runner.os == 'Windows'
      id: cache-llvm-windows
      uses: actions/cache@v5
      with:
        path: |
          C:\Program Files\LLVM
          C:\ProgramData\chocolatey\lib\llvm
        key: llvm-windows-${{ runner.arch }}-v1

    - name: Cache CMake (Windows)
      if: runner.os == 'Windows'
      id: cache-cmake-windows
      uses: actions/cache@v5
      with:
        path: |
          C:\Program Files\CMake
          C:\ProgramData\chocolatey\lib\cmake
        key: cmake-windows-${{ runner.arch }}-v1

    - name: Install dependencies (Windows)
      if: runner.os == 'Windows'
      shell: pwsh
      env:
        TESSERACT_CACHE_HIT: ${{ steps.cache-tesseract-windows.outputs.cache-hit }}
        LLVM_CACHE_HIT: ${{ steps.cache-llvm-windows.outputs.cache-hit }}
        CMAKE_CACHE_HIT: ${{ steps.cache-cmake-windows.outputs.cache-hit }}
      run: pwsh -File scripts/ci/install-system-deps/install-windows.ps1
