name: CI

on:
  push:
    branches: [main]
  pull_request:

jobs:
  ruff:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7.0.0
      - uses: astral-sh/setup-uv@v8.2.0
        with:
          version: "latest"
      - run: uv sync --group dev
      - name: Ruff check
        run: uv run ruff check src/ tests/
      - name: Ruff format
        run: uv run ruff format --check src/ tests/

  mypy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7.0.0
      - uses: astral-sh/setup-uv@v8.2.0
        with:
          version: "latest"
      - run: uv sync --group dev
      - name: Mypy
        run: uv run mypy src/

  pytest:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7.0.0
      - uses: astral-sh/setup-uv@v8.2.0
        with:
          version: "latest"
      - run: uv sync --group dev
      - name: NLTK data (punkt_tab for sent_tokenize)
        run: uv run python -c "import nltk; nltk.download('punkt_tab')"
      - name: Pytest
        run: uv run pytest tests/ -x -q

  package:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7.0.0
      - uses: astral-sh/setup-uv@v8.2.0
        with:
          version: "latest"
      - name: Build distributions
        run: uv build
      - name: Check package metadata
        run: uvx twine check --strict dist/*
      - uses: actions/upload-artifact@v7
        with:
          name: dist
          path: dist/*

  install-smoke:
    name: Install smoke (${{ matrix.label }})
    needs: package
    runs-on: ${{ matrix.os }}
    timeout-minutes: 30
    strategy:
      fail-fast: false
      matrix:
        include:
          - label: linux
            os: ubuntu-latest
          - label: macos-arm64
            os: macos-14
    steps:
      - uses: actions/checkout@v7.0.0
      - uses: actions/setup-python@v6.3.0
        with:
          python-version: "3.11"
      - uses: actions/download-artifact@v8
        with:
          name: dist
          path: dist
      - name: Install wheel with pip
        shell: bash
        run: |
          python -m venv .venv-smoke
          source .venv-smoke/bin/activate
          python -m pip install --upgrade pip
          python -m pip install dist/speech_to_speech-*.whl
          python -m pip check
      - name: Run installed CLI smoke test
        shell: bash
        run: |
          source .venv-smoke/bin/activate
          unset OPENAI_API_KEY
          # Validate the installed entry point without loading models or calling OpenAI.
          python tests/install_smoke.py
