name: FailDaily CI/CD Pipeline

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

env:
  NODE_VERSION: '18'
  DOCKER_REGISTRY: 'your-registry.com'
  PROJECT_NAME: 'faildaily'

jobs:
  # Tests unitaires et validation
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        component: [frontend, backend]
    
    steps:
    - name: 📥 Checkout code
      uses: actions/checkout@v4
      
    - name: 🟢 Setup Node.js
      uses: actions/setup-node@v4
      with:
        node-version: ${{ env.NODE_VERSION }}
        cache: 'npm'
        
    - name: 📦 Install root dependencies
      run: npm ci
      
    - name: 📦 Install ${{ matrix.component }} dependencies
      run: |
        cd ${{ matrix.component }}
        npm ci
        
    - name: 🧪 Run ${{ matrix.component }} tests
      run: |
        cd ${{ matrix.component }}
        npm run test -- --watch=false --browsers=ChromeHeadless --code-coverage
        
    - name: 📊 Upload coverage to Codecov
      uses: codecov/codecov-action@v3
      with:
        file: ./${{ matrix.component }}/coverage/lcov.info
        flags: ${{ matrix.component }}

  # Tests de validation d'âge
  age-validation-tests:
    runs-on: ubuntu-latest
    needs: test
    
    services:
      mysql:
        image: mysql:8.0
        env:
          MYSQL_ROOT_PASSWORD: testpassword
          MYSQL_DATABASE: faildaily_test
        options: >-
          --health-cmd "mysqladmin ping"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
    
    steps:
    - name: 📥 Checkout code
      uses: actions/checkout@v4
      
    - name: 🟢 Setup Node.js
      uses: actions/setup-node@v4
      with:
        node-version: ${{ env.NODE_VERSION }}
        
    - name: 📦 Install dependencies
      run: |
        npm ci
        cd backend-api && npm ci
        cd ../frontend && npm ci
        
    - name: 🗄️ Setup test database
      run: |
        cd backend-api
        npm run db:migrate:test
        npm run db:seed:test
        
    - name: 🚀 Start backend service
      run: |
        cd backend-api
        npm run start:test &
        sleep 10
        
    - name: 👶 Run age validation tests
      run: node devops/scripts/test-age-validation.js

  # Build et sécurité
  build-and-security:
    runs-on: ubuntu-latest
    needs: test
    
    steps:
    - name: 📥 Checkout code
      uses: actions/checkout@v4
      
    - name: 🟢 Setup Node.js
      uses: actions/setup-node@v4
      with:
        node-version: ${{ env.NODE_VERSION }}
        
    - name: 📦 Install dependencies
      run: |
        npm ci
        cd frontend && npm ci
        cd ../backend-api && npm ci
        
    - name: 🔍 Security audit
      run: |
        npm audit --audit-level moderate
        cd frontend && npm audit --audit-level moderate
        cd ../backend-api && npm audit --audit-level moderate
        
    - name: 🔨 Build frontend
      run: |
        cd frontend
        npm run build --prod
        
    - name: 🔨 Build backend
      run: |
        cd backend-api
        npm run build
        
    - name: 📁 Upload build artifacts
      uses: actions/upload-artifact@v3
      with:
        name: build-artifacts
        path: |
          frontend/www/
          backend-api/dist/
          
  # Tests E2E
  e2e-tests:
    runs-on: ubuntu-latest
    needs: [test, age-validation-tests]
    
    steps:
    - name: 📥 Checkout code
      uses: actions/checkout@v4
      
    - name: 🟢 Setup Node.js
      uses: actions/setup-node@v4
      with:
        node-version: ${{ env.NODE_VERSION }}
        
    - name: 📦 Install dependencies
      run: |
        npm ci
        cd frontend && npm ci
        cd ../backend-api && npm ci
        
    - name: 🚀 Start services
      run: |
        cd backend-api && npm run start:test &
        cd frontend && npm run start &
        sleep 30
        
    - name: 🎭 Run E2E tests
      run: |
        cd frontend
        npm run e2e -- --headless

  # Build Docker images
  docker-build:
    runs-on: ubuntu-latest
    needs: [build-and-security, e2e-tests]
    if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/develop'
    
    steps:
    - name: 📥 Checkout code
      uses: actions/checkout@v4
      
    - name: 🐳 Set up Docker Buildx
      uses: docker/setup-buildx-action@v3
      
    - name: 🔑 Login to Docker Registry
      uses: docker/login-action@v3
      with:
        registry: ${{ env.DOCKER_REGISTRY }}
        username: ${{ secrets.DOCKER_USERNAME }}
        password: ${{ secrets.DOCKER_PASSWORD }}
        
    - name: 📁 Download build artifacts
      uses: actions/download-artifact@v3
      with:
        name: build-artifacts
        
    - name: 🏗️ Build and push Docker images
      run: |
        cd docker
        
        # Tag pour la branche
        if [ "${{ github.ref }}" = "refs/heads/main" ]; then
          TAG="latest"
        else
          TAG="develop"
        fi
        
        # Build frontend
        docker build -t ${{ env.DOCKER_REGISTRY }}/${{ env.PROJECT_NAME }}-frontend:$TAG -f Dockerfile.frontend ../
        docker push ${{ env.DOCKER_REGISTRY }}/${{ env.PROJECT_NAME }}-frontend:$TAG
        
        # Build backend
        docker build -t ${{ env.DOCKER_REGISTRY }}/${{ env.PROJECT_NAME }}-backend:$TAG -f Dockerfile.backend ../
        docker push ${{ env.DOCKER_REGISTRY }}/${{ env.PROJECT_NAME }}-backend:$TAG

  # Déploiement staging
  deploy-staging:
    runs-on: ubuntu-latest
    needs: docker-build
    if: github.ref == 'refs/heads/develop'
    environment: staging
    
    steps:
    - name: 📥 Checkout code
      uses: actions/checkout@v4
      
    - name: 🚀 Deploy to staging
      run: |
        echo "Deploying to staging environment..."
        # Ici vous ajouteriez vos commandes de déploiement
        # Par exemple: kubectl, docker-compose, ou scripts personnalisés
        
  # Déploiement production
  deploy-production:
    runs-on: ubuntu-latest
    needs: docker-build
    if: github.ref == 'refs/heads/main'
    environment: production
    
    steps:
    - name: 📥 Checkout code
      uses: actions/checkout@v4
      
    - name: 🔴 Deploy to production
      run: |
        echo "Deploying to production environment..."
        # Ici vous ajouteriez vos commandes de déploiement production
        
  # Notification
  notify:
    runs-on: ubuntu-latest
    needs: [deploy-staging, deploy-production]
    if: always()
    
    steps:
    - name: 📢 Notify deployment status
      run: |
        if [ "${{ job.status }}" = "success" ]; then
          echo "✅ Deployment successful!"
        else
          echo "❌ Deployment failed!"
        fi
