name: Quality Assurance

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

jobs:
  broken-links:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install uv
        uses: astral-sh/setup-uv@v3
        with:
          enable-cache: true

      - name: Get changed files
        id: changed
        uses: tj-actions/changed-files@v45
        with:
          files_yaml: |
            logic:
              - tools/scripts/check_broken_links.py
              - tools/tests/test_check_broken_links.py
              - tools/scripts/paths.py
            docs:
              - "**/*.md"
          safe_output: true

      - name: Run Logic Tests
        if: steps.changed.outputs.logic_any_changed == 'true'
        env:
          PYTHONPATH: ${{ github.workspace }}
        run: uv run pytest tools/tests/test_check_broken_links.py

      - name: Run Link Check on All Files
        if: steps.changed.outputs.docs_any_changed == 'true'
        env:
          PYTHONPATH: ${{ github.workspace }}
        run: uv run tools/scripts/check_broken_links.py --verbose

  link-format:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install uv
        uses: astral-sh/setup-uv@v3
        with:
          enable-cache: true

      - name: Get changed files
        id: changed
        uses: tj-actions/changed-files@v45
        with:
          files_yaml: |
            logic:
              - tools/scripts/check_link_format.py
              - tools/tests/test_check_link_format.py
              - tools/scripts/paths.py
            docs:
              - "**/*.md"
          safe_output: true

      - name: Run Logic Tests
        if: steps.changed.outputs.logic_any_changed == 'true'
        env:
          PYTHONPATH: ${{ github.workspace }}
        run: uv run pytest tools/tests/test_check_link_format.py

      - name: Run Link Format Check on All Files
        if: steps.changed.outputs.docs_any_changed == 'true'
        env:
          PYTHONPATH: ${{ github.workspace }}
        run: uv run tools/scripts/check_link_format.py --verbose

  jupytext:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install uv
        uses: astral-sh/setup-uv@v3
        with:
          enable-cache: true

      - name: Get changed files
        id: changed
        uses: tj-actions/changed-files@v45
        with:
          files_yaml: |
            logic:
              - tools/scripts/jupytext_sync.py
              - tools/scripts/jupytext_verify_pair.py
              - tools/tests/test_jupytext_sync.py
              - tools/tests/test_jupytext_verify_pair.py
              - tools/scripts/paths.py
            notebooks:
              - "**/*.md"
              - "**/*.ipynb"
          safe_output: true

      - name: Run Jupytext Script Tests
        if: steps.changed.outputs.logic_any_changed == 'true'
        env:
          PYTHONPATH: ${{ github.workspace }}
        run: uv run pytest tools/tests/test_jupytext_sync.py tools/tests/test_jupytext_verify_pair.py

      - name: Verify Notebook Sync on Changed Files
        if: steps.changed.outputs.notebooks_any_changed == 'true'
        env:
          PYTHONPATH: ${{ github.workspace }}
        run: |
          uv run tools/scripts/jupytext_sync.py --test ${{ steps.changed.outputs.notebooks_all_changed_files }}

  api-keys:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install uv
        uses: astral-sh/setup-uv@v3
        with:
          enable-cache: true

      - name: Get changed files
        id: changed
        uses: tj-actions/changed-files@v45
        with:
          files_yaml: |
            logic:
              - tools/scripts/check_api_keys.py
              - tools/tests/test_check_api_keys.py
              - tools/scripts/paths.py
            scan:
              - "**"
          files_ignore: |
            tools/tests/test_check_api_keys.py
            tools/docs/scripts_instructions/check_api_keys_py_script.md
          safe_output: true

      - name: Run Logic Tests
        if: steps.changed.outputs.logic_any_changed == 'true'
        env:
          PYTHONPATH: ${{ github.workspace }}
        run: uv run pytest tools/tests/test_check_api_keys.py

      - name: Run API Key Check on Changed Files
        if: steps.changed.outputs.scan_any_changed == 'true'
        env:
          PYTHONPATH: ${{ github.workspace }}
        run: |
          uv run tools/scripts/check_api_keys.py ${{ steps.changed.outputs.scan_all_changed_files }} --verbose

  json-validation:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install uv
        uses: astral-sh/setup-uv@v3
        with:
          enable-cache: true

      - name: Get changed files
        id: changed
        uses: tj-actions/changed-files@v45
        with:
          files_yaml: |
            logic:
              - tools/scripts/check_json_files.py
              - tools/tests/test_check_json_files.py
              - tools/scripts/paths.py
            json:
              - "**/*.json"
          safe_output: true

      - name: Run Logic Tests
        if: steps.changed.outputs.logic_any_changed == 'true'
        env:
          PYTHONPATH: ${{ github.workspace }}
        run: uv run pytest tools/tests/test_check_json_files.py

      - name: Run JSON Validation on Changed Files
        if: steps.changed.outputs.json_any_changed == 'true'
        env:
          PYTHONPATH: ${{ github.workspace }}
        run: |
          uv run tools/scripts/check_json_files.py ${{ steps.changed.outputs.json_all_changed_files }} --verbose

  prepare-prompt:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install uv
        uses: astral-sh/setup-uv@v3
        with:
          enable-cache: true

      - name: Get changed files
        id: changed
        uses: tj-actions/changed-files@v45
        with:
          files_yaml: |
            logic:
              - tools/scripts/prepare_prompt.py
              - tools/tests/test_prepare_prompt.py
              - tools/scripts/paths.py
          safe_output: true

      - name: Run Logic Tests
        if: steps.changed.outputs.logic_any_changed == 'true'
        env:
          PYTHONPATH: ${{ github.workspace }}
        run: uv run pytest tools/tests/test_prepare_prompt.py

  script-suite:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install uv
        uses: astral-sh/setup-uv@v3
        with:
          enable-cache: true

      - name: Get changed files
        id: changed
        uses: tj-actions/changed-files@v45
        with:
          files_yaml: |
            logic:
              - tools/scripts/check_script_suite.py
              - tools/tests/test_check_script_suite.py
              - tools/scripts/paths.py
            suite:
              - tools/scripts/*.py
              - tools/tests/test_*.py
              - tools/docs/scripts_instructions/*_py_script.md
          safe_output: true

      - name: Run Logic Tests
        if: steps.changed.outputs.logic_any_changed == 'true'
        env:
          PYTHONPATH: ${{ github.workspace }}
        run: uv run pytest tools/tests/test_check_script_suite.py

      - name: Run Script Suite Check
        if: steps.changed.outputs.suite_any_changed == 'true'
        env:
          PYTHONPATH: ${{ github.workspace }}
        run: uv run tools/scripts/check_script_suite.py --verbose

  adr-index:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install uv
        uses: astral-sh/setup-uv@v3
        with:
          enable-cache: true

      - name: Get changed files
        id: changed
        uses: tj-actions/changed-files@v45
        with:
          files_yaml: |
            logic:
              - tools/scripts/check_adr_index.py
              - tools/tests/test_check_adr_index.py
            adr:
              - architecture/adr/adr_*.md
              - architecture/adr_index.md
          safe_output: true

      - name: Run Logic Tests
        if: steps.changed.outputs.logic_any_changed == 'true'
        env:
          PYTHONPATH: ${{ github.workspace }}
        run: uv run pytest tools/tests/test_check_adr_index.py

      - name: Run ADR Index Check
        if: steps.changed.outputs.adr_any_changed == 'true'
        env:
          PYTHONPATH: ${{ github.workspace }}
        run: uv run tools/scripts/check_adr_index.py --verbose
