# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: tests

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

jobs:
  prepare-cache:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Cache KRISTINsnid.csv.zip
        id: cache-kristin
        uses: actions/cache@v4
        with:
          path: src/islenska/resources/KRISTINsnid.csv.zip
          key: kristin-csv-zip-v1
      - name: Download KRISTINsnid.csv.zip
        if: steps.cache-kristin.outputs.cache-hit != 'true'
        run: |
          mkdir -p src/islenska/resources
          wget -q -O src/islenska/resources/KRISTINsnid.csv.zip 'https://bin.arnastofnun.is/django/api/nidurhal/?file=KRISTINsnid.csv.zip'

  build:
    needs: prepare-cache
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest]
        # Test across Python versions to verify abi3 wheel compatibility
        # CPython: minimum (3.9), intermediate versions, latest stable
        # PyPy: only the version we build wheels for (see wheels.yml)
        python-version: ['3.9', '3.10', '3.11', '3.12', '3.13', '3.14', 'pypy-3.11']

    steps:
    - uses: actions/checkout@v4
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v5
      with:
        python-version: ${{ matrix.python-version }}

    - name: Restore cached KRISTINsnid.csv.zip
      id: cache-kristin
      uses: actions/cache@v4
      with:
        path: src/islenska/resources/KRISTINsnid.csv.zip
        key: kristin-csv-zip-v1

    # The prepare-cache job warms the cache, but GitHub's cache service is
    # eventually consistent: on the very first run (cold cache) the entry it
    # just saved is not always queryable when this restore runs, so the cache
    # can miss here even though prepare-cache succeeded. Download as a fallback
    # so a miss is never fatal; this job then saves the cache for later jobs.
    - name: Download KRISTINsnid.csv.zip
      if: steps.cache-kristin.outputs.cache-hit != 'true'
      run: |
        mkdir -p src/islenska/resources
        wget -q -O src/islenska/resources/KRISTINsnid.csv.zip 'https://bin.arnastofnun.is/django/api/nidurhal/?file=KRISTINsnid.csv.zip'

    - name: Install BinPackage
      run: |
        python -m pip install uv
        uv pip install --system --upgrade wheel setuptools pytest types-setuptools packaging
        uv pip install --system -e .

    - name: Lint with ruff
      run: |
        if [ "${{ matrix.python-version }}" == "3.9" ]; then uv pip install --system ruff; fi
        if [ "${{ matrix.python-version }}" == "3.9" ]; then ruff check src/islenska; fi

    - name: Type check with pyright
      run: |
        if [ "${{ matrix.python-version }}" == "3.9" ]; then uv pip install --system pyright; fi
        # pyrightconfig.json pins the target Python version and strict mode;
        # --pythonpath points pyright at the interpreter holding the deps
        if [ "${{ matrix.python-version }}" == "3.9" ]; then pyright --pythonpath "$(which python)"; fi

    - name: Build *.dawg.bin
      run: |
        python tools/dawgbuilder.py

    - name: Build compressed.bin
      run: |
        unzip -q -d src/islenska/resources/ src/islenska/resources/KRISTINsnid.csv.zip
        python tools/binpack.py

    - name: Test with pytest
      run: |
        python -m pytest

    - name: Slack notification
      uses: 8398a7/action-slack@v3
      with:
        status: ${{ job.status }}
        author_name: Integration Testing (Python ${{ matrix.python-version }})
      env:
        SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
      if: failure() # Pick up event if the job fails
