name: Tests

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

jobs:
  test:
    name: Test Python ${{ matrix.python-version }}
    runs-on: ubuntu-latest
    
    strategy:
      matrix:
        python-version: ["3.10", "3.11", "3.12"]
        
    services:
      postgres:
        image: postgres:14-alpine
        env:
          POSTGRES_PASSWORD: test_password
          POSTGRES_USER: test_user
          POSTGRES_DB: test_db
        ports:
          - 5432:5432
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
          
    steps:
    - name: Checkout code
      uses: actions/checkout@v4
      
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v4
      with:
        python-version: ${{ matrix.python-version }}
        
    - name: Cache pip packages
      uses: actions/cache@v3
      with:
        path: ~/.cache/pip
        key: ${{ runner.os }}-pip-${{ hashFiles('backend/requirements.txt') }}
        restore-keys: |
          ${{ runner.os }}-pip-
          
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        cd backend
        pip install -r requirements.txt
        pip install pytest pytest-cov pytest-asyncio
        
    - name: Run tests with coverage
      env:
        DATABASE_URL: postgresql://test_user:test_password@localhost:5432/test_db
        SECRET_KEY: test_secret_key_for_ci
      run: |
        cd backend
        pytest --cov=app --cov-report=xml --cov-report=term-missing
        
    - name: Upload coverage to Codecov
      uses: codecov/codecov-action@v3
      with:
        file: ./backend/coverage.xml
        flags: unittests
        name: codecov-umbrella
        fail_ci_if_error: false
        
  lint:
    name: Lint and Format Check
    runs-on: ubuntu-latest
    
    steps:
    - name: Checkout code
      uses: actions/checkout@v4
      
    - name: Set up Python
      uses: actions/setup-python@v4
      with:
        python-version: '3.10'
        
    - name: Install linting tools
      run: |
        python -m pip install --upgrade pip
        pip install pylint black flake8 isort mypy
        cd backend && pip install -r requirements.txt
        
    - name: Run Black (format check)
      run: |
        black --check backend/app
        
    - name: Run isort (import order check)
      run: |
        isort --check-only backend/app
        
    - name: Run Flake8
      run: |
        flake8 backend/app --max-line-length=100 --extend-ignore=E203,W503
        
    - name: Run Pylint
      run: |
        pylint backend/app --max-line-length=100 --disable=C0111,R0903
      continue-on-error: true
      
  docker-build:
    name: Docker Build Test
    runs-on: ubuntu-latest
    
    steps:
    - name: Checkout code
      uses: actions/checkout@v4
      
    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@v2
      
    - name: Build Docker image
      run: |
        docker compose build
        
    - name: Test Docker image
      run: |
        docker compose up -d
        sleep 10
        docker compose ps
        docker compose logs
        docker compose down
