name: Django CI

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

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: ["3.13"]

    steps:
    - uses: actions/checkout@v4

    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v5
      with:
        python-version: ${{ matrix.python-version }}

    - name: Install uv
      uses: astral-sh/setup-uv@v4
      with:
        enable-cache: true
        cache-suffix: ${{ matrix.python-version }}

    - name: Install dependencies
      run: uv sync

# @IF use_vite
    - name: Install Node.js
      uses: actions/setup-node@v4
      with:
        node-version: 20

    - name: Build frontend
      run: |
        npm ci
        npm run build
# @ENDIF

    - name: Run linting
      run: uv run ruff check .

    - name: Format check
      run: uv run ruff format --check .

    - name: Run Django checks
      env:
        DJANGO_SETTINGS_MODULE: [[ module_name ]].settings.development
        SECRET_KEY: test-secret-key-for-ci
      run: |
        uv run python manage.py check

    - name: Run tests
      env:
        DJANGO_SETTINGS_MODULE: [[ module_name ]].settings.development
        SECRET_KEY: test-secret-key-for-ci
      run: |
        uv run python manage.py test

    - name: Check migrations
      env:
        DJANGO_SETTINGS_MODULE: [[ module_name ]].settings.development
        SECRET_KEY: test-secret-key-for-ci
      run: |
        uv run python manage.py makemigrations --check --dry-run

    - name: Security check
      env:
        DJANGO_SETTINGS_MODULE: [[ module_name ]].settings.development
        SECRET_KEY: test-secret-key-for-ci
      run: |
        uv run python manage.py check --deploy
