name: Scheduled Tests

on:
  schedule:
    # Every Monday at 07:00 UTC
    - cron: "0 7 * * 1"
  workflow_dispatch:  # Allow manual trigger

permissions:
  contents: read

jobs:
  test:
    name: Weekly Test Run (Python ${{ matrix.python-version }})
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        python-version: ["3.10", "3.11"]

    steps:
      - uses: actions/checkout@v6

      - uses: actions/setup-python@v6
        with:
          python-version: ${{ matrix.python-version }}
          cache: "pip"

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -e ".[test]" || pip install -r requirements.txt
          pip install pytest pytest-cov

      - name: Run tests
        run: |
          pytest test/backtesting/ -v --tb=short --durations=10

      - name: Upload results on failure
        if: failure()
        uses: actions/upload-artifact@v7
        with:
          name: test-results-py${{ matrix.python-version }}
          path: |
            .pytest_cache/
          retention-days: 7

  dependency-audit:
    name: Dependency Audit
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6

      - uses: actions/setup-python@v6
        with:
          python-version: "3.10"
          cache: "pip"

      - name: Install safety
        run: pip install safety

      - name: Check for known vulnerabilities
        run: |
          echo "=== Weekly dependency vulnerability scan ==="
          safety check -r requirements.txt --json || {
            echo "::warning::Vulnerabilities found — review and update dependencies"
            exit 0
          }
