name: release

on:
  push:
    tags:
      - 'v*'

permissions:
  contents: read
  id-token: write

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout tag
        uses: actions/checkout@v4
        with:
          fetch-depth: 0  # fetch tags for hatch-vcs
          ref: ${{ github.ref }}
      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.12'
      - name: Install
        run: |
          python -m pip install -U pip
          python -m pip install -r requirements.txt
          python -m pip install -r requirements-dev.txt
          python -m pip install hatch
      - name: Unit coverage (fail-under=85)
        run: |
          python -m pip install -e .
          pytest tests/unit --cov=teds_core --cov=teds --cov-branch --cov-report=term-missing --cov-fail-under=85 -q
      - name: CLI integration tests
        run: |
          pytest tests/cli -v

  build-and-publish:
    needs: test
    runs-on: ubuntu-latest
    steps:
      - name: Checkout tag
        uses: actions/checkout@v4
        with:
          fetch-depth: 0  # fetch tags for hatch-vcs
          ref: ${{ github.ref }}
      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.12'
      - name: Build
        run: |
          python -m pip install -U pip hatch
          rm -rf dist
          hatch build -t sdist -t wheel
          ls -la dist || true
      - name: Verify install and smoke-test
        run: |
          set -euo pipefail
          python -m pip install -U pip twine
          twine check dist/*
          python -m venv .pkg-smoke
          . .pkg-smoke/bin/activate
          python -m pip install -U pip
          pip install dist/*.whl
          REPO="${GITHUB_WORKSPACE}"
          run_dir=$(mktemp -d)
          cd "$run_dir"
          teds --version | sed -n '1p'
          python - << 'PY'
          from teds_core.resources import read_text_resource as r
          assert r('spec_schema.yaml')
          assert r('teds_compat.yaml')
          print('resource load OK')
          PY
          set +e
          teds verify "$REPO/demo/sample_tests.yaml" --output-level warning > /tmp/out.yaml
          rc=$?
          set -e
          echo "verify rc: $rc"
          test "$rc" -eq 1
          teds generate "$REPO/demo/sample_schemas.yaml=$run_dir/smoke.tests.yaml"
          test -f "$run_dir/smoke.tests.yaml"
          deactivate
      - name: Publish to PyPI (trusted publishing)
        uses: pypa/gh-action-pypi-publish@release/v1
