name: CI

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

jobs:
  rust-checks:
    name: Rust Checks
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:15
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: password
          POSTGRES_DB: json_register_test
        ports:
          - 5432:5432
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy, rustfmt
      - name: Check formatting
        run: cargo fmt --all -- --check
      - name: Clippy
        run: cargo clippy --all-features -- -D warnings
      - name: Test
        run: cargo test --all-features
      - name: Integration Test
        env:
          TEST_DB_HOST: localhost
          TEST_DB_PORT: 5432
          TEST_DB_USER: postgres
          TEST_DB_PASSWORD: password
          TEST_DB_NAME: json_register_test
        run: cargo test --test integration_test --no-default-features -- --ignored

  python-tests:
    name: Python Tests
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:15
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: password
          POSTGRES_DB: json_register_test
        ports:
          - 5432:5432
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: '3.12'
      - uses: dtolnay/rust-toolchain@stable
      - name: Create virtualenv
        run: |
          python -m venv .venv
          echo "VIRTUAL_ENV=$(pwd)/.venv" >> $GITHUB_ENV
          echo "$(pwd)/.venv/bin" >> $GITHUB_PATH
      - name: Install dependencies
        run: |
          pip install --upgrade pip
          pip install pytest maturin "psycopg[binary]"
      - name: Build and install extension
        env:
          DATABASE_URL: postgres://postgres:password@localhost:5432/json_register_test
        run: maturin develop
      - name: Run Python tests
        env:
          DATABASE_URL: postgres://postgres:password@localhost:5432/json_register_test
        run: pytest tests/test_python_*.py

  code-quality:
    name: Code Quality
    uses: telicent-oss/shared-workflows/.github/workflows/python-ci-qa.yml@main
    with:
      PYTHON_VERSION: "3.12"
      OS: "ubuntu-latest"
      SKIP_BUILD: true
      TEST_COMMAND: "echo 'Tests run in python-tests job'"

  # Build wheels for Linux
  linux:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        target: [x86_64-unknown-linux-gnu, aarch64-unknown-linux-gnu]
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: '3.12'
      - name: Build wheels
        uses: PyO3/maturin-action@v1
        with:
          target: ${{ matrix.target }}
          args: --release --out dist --find-interpreter
          sccache: 'true'
          manylinux: auto
        env:
          # Fix for ring crate on Linux ARM64
          CFLAGS_aarch64_unknown_linux_gnu: "-D__ARM_ARCH=8"
          TARGET_CC: "clang"
      - name: Upload wheels
        uses: actions/upload-artifact@v4
        with:
          name: wheels-linux-${{ matrix.target }}
          path: dist

  # Build wheels for Windows
  windows:
    runs-on: windows-latest
    strategy:
      fail-fast: false
      matrix:
        target: [x86_64-pc-windows-msvc]
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: '3.12'
      - name: Build wheels
        uses: PyO3/maturin-action@v1
        with:
          target: ${{ matrix.target }}
          args: --release --out dist --find-interpreter
          sccache: 'true'
      - name: Upload wheels
        uses: actions/upload-artifact@v4
        with:
          name: wheels-windows-${{ matrix.target }}
          path: dist

  # Build wheels for macOS
  macos:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - target: x86_64-apple-darwin
            os: macos-15-intel
          - target: aarch64-apple-darwin
            os: macos-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: '3.12'
      - name: Build wheels
        uses: PyO3/maturin-action@v1
        with:
          target: ${{ matrix.target }}
          args: --release --out dist -i python
          sccache: 'true'
      - name: Upload wheels
        uses: actions/upload-artifact@v4
        with:
          name: wheels-macos-${{ matrix.target }}
          path: dist

