name: CI - Tests et Syntax
on:
  push:
    branches: [main, dev]
  pull_request:
    branches: [main]

jobs:
  lint-and-test:
    runs-on: ubuntu-latest
    container: python:3.13-slim
    
    services:
      mariadb:
        image: mariadb:11
        env:
          MARIADB_ROOT_PASSWORD: root123
          MARIADB_DATABASE: gmao_test_db
          MARIADB_USER: gmao
          MARIADB_PASSWORD: gmao123
        ports:
          - 3306:3306
        options: --health-cmd="healthcheck.sh --connect --innodb_initialized" --health-interval=10s --health-timeout=5s --health-retries=5
    
    env:
      DATABASE_URL: mysql+pymysql://gmao:gmao123@mariadb/gmao_test_db?charset=utf8mb4
      SECRET_KEY: test-secret-key-with-at-least-32-characters
      OUTLOOK_ENCRYPTION_KEY: S1Baix4126-5QfNnofmCV5qKqrMMvBoJj7MZ6WVFpQE=
      SETUP_TOKEN: test-setup-token-with-at-least-32-characters
      FLASK_DEBUG: "0"
    
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      
      - name: Install dependencies
        run: |
          apt-get update && apt-get install -y --no-install-recommends gcc libffi-dev libssl-dev && rm -rf /var/lib/apt/lists/*
          pip install --no-cache-dir -r requirements.txt
      
      - name: Syntax check
        run: |
          python3 -m py_compile app_new/__init__.py
          python3 -m py_compile app_new/outlook/auth.py
          python3 -m py_compile app_new/outlook/models.py
          python3 -m py_compile app_new/status/routes.py
          python3 -m py_compile app_new/gmao_config/routes.py
          python3 -m py_compile gmao_watchdog.py
          python3 -m py_compile ent_watchdog.py
          python3 -m py_compile pronote_watchdog.py
          python3 -m py_compile watchdog_dnd.py
          echo "Syntax check OK"
      
      - name: Jinja2 template parse
        run: |
          python3 -c "
          from jinja2 import Environment
          env = Environment()
          import glob
          errors = []
          for f in glob.glob('app_new/**/templates/**/*.html', recursive=True):
              try:
                  with open(f) as fh:
                      env.parse(fh.read())
              except Exception as e:
                  errors.append(f'{f}: {e}')
          if errors:
              for e in errors:
                  print(f'ERROR: {e}')
              exit(1)
          print(f'All {len(glob.glob(\"app_new/**/templates/**/*.html\", recursive=True))} templates OK')
          "
      
      - name: Wait for MariaDB
        run: |
          for i in $(seq 1 30); do
            python3 -c "import pymysql; pymysql.connect(host='mariadb', port=3306, user='root', password='root123')" 2>/dev/null && echo "MariaDB ready" && break
            echo "Waiting for MariaDB... ($i)"
            sleep 2
          done
      
      - name: Run unit tests
        run: python3 -m pytest tests/unit/ -x --tb=short -q
      
      - name: Run integration tests
        run: python3 -m pytest tests/integration/ -x --tb=short -q
