name: CI

on:
  push:
    branches: [main, master]
  pull_request:
    branches: [main, master]

env:
  CARGO_TERM_COLOR: always
  RUST_BACKTRACE: 1

jobs:
  fmt:
    name: Format
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt
      - run: cargo fmt --all --check

  clippy:
    name: Clippy
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy
      - uses: Swatinem/rust-cache@v2
      - run: cargo clippy --all-targets --all-features -- -D warnings

  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - run: cargo build --all-features

  test:
    name: Test
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - run: cargo test --lib --all-features

  audit:
    name: Security Audit
    runs-on: ubuntu-latest
    permissions:
      checks: write
      contents: read
    steps:
      - uses: actions/checkout@v4
      - uses: rustsec/audit-check@v2
        with:
          token: ${{ secrets.GITHUB_TOKEN }}

  coverage:
    name: Coverage
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: llvm-tools-preview
      - uses: taiki-e/install-action@cargo-llvm-cov
      - uses: Swatinem/rust-cache@v2
      - name: Generate coverage report
        run: cargo llvm-cov --lib --all-features --lcov --output-path lcov.info
      - name: Upload to Codecov
        if: ${{ env.CODECOV_TOKEN != '' }}
        uses: codecov/codecov-action@v4
        with:
          files: lcov.info
          fail_ci_if_error: false
        env:
          CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

  integration:
    name: Integration Tests
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - name: Pre-pull postgres image via ECR Public mirror
        run: |
          for attempt in 1 2 3 4 5; do
            docker pull public.ecr.aws/docker/library/postgres:16.12 && break
            echo "Pull attempt $attempt failed, retrying in $((attempt * 10))s..."
            sleep $((attempt * 10))
          done
          docker tag public.ecr.aws/docker/library/postgres:16.12 postgres:16
      - name: Pre-pull postgis image
        run: |
          for attempt in 1 2 3 4 5; do
            docker pull postgis/postgis:16-3.4 && break
            echo "Pull attempt $attempt failed, retrying in $((attempt * 10))s..."
            sleep $((attempt * 10))
          done
      - name: Run integration tests
        run: cargo test --all-features --test '*' -- --test-threads=4

  corpus:
    name: Corpus Convergence
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - name: Pre-pull postgres image via ECR Public mirror
        run: |
          for attempt in 1 2 3 4 5; do
            docker pull public.ecr.aws/docker/library/postgres:16.12 && break
            echo "Pull attempt $attempt failed, retrying in $((attempt * 10))s..."
            sleep $((attempt * 10))
          done
          docker tag public.ecr.aws/docker/library/postgres:16.12 postgres:16
      - name: Run corpus convergence tests
        run: cargo test --all-features --test corpus -- --ignored --test-threads=1

  corpus-sagri-mrv:
    name: Corpus Convergence (sagri/mrv)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - name: Pre-pull postgis image
        run: |
          for attempt in 1 2 3 4 5; do
            docker pull postgis/postgis:16-3.4 && break
            echo "Pull attempt $attempt failed, retrying in $((attempt * 10))s..."
            sleep $((attempt * 10))
          done
      - name: Run sagri/mrv snapshot convergence
        run: cargo test --all-features --test corpus_sagri_mrv -- --ignored --test-threads=1 --nocapture

  parser-coverage-triage:
    name: Parser Coverage Triage
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - name: Run parser coverage triage report
        run: cargo test --all-features --test parser_coverage_triage -- --ignored --nocapture

  pg-version-matrix:
    name: PG ${{ matrix.pg-version }}
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      max-parallel: 2
      matrix:
        include:
          - pg-version: "13.23"
          - pg-version: "14.20"
          - pg-version: "15.17"
          - pg-version: "16.12"
          - pg-version: "17.9"
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - name: Pre-pull postgres image via ECR Public mirror
        run: |
          for attempt in 1 2 3 4 5; do
            docker pull public.ecr.aws/docker/library/postgres:${{ matrix.pg-version }} && break
            echo "Pull attempt $attempt failed, retrying in $((attempt * 10))s..."
            sleep $((attempt * 10))
          done
          docker tag public.ecr.aws/docker/library/postgres:${{ matrix.pg-version }} postgres:${{ matrix.pg-version }}
      - name: Run integration tests against PostgreSQL ${{ matrix.pg-version }}
        env:
          PGMOLD_TEST_PG_VERSION: ${{ matrix.pg-version }}
        run: cargo test --all-features --test '*' -- --test-threads=4
