name: 🚀 Deploy Web Sentinel API to taaazzz-prog.fr

on:
  push:
    branches: [ main, production ]
  workflow_dispatch:

env:
  DOCKER_REGISTRY: ghcr.io
  IMAGE_NAME: taaazzz-prog/web-sentinel-api

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
      
    steps:
    - name: 📥 Checkout code
      uses: actions/checkout@v4
      
    - name: 🐳 Set up Docker Buildx
      uses: docker/setup-buildx-action@v3
      
    - name: 🔐 Log in to Container Registry
      uses: docker/login-action@v3
      with:
        registry: ${{ env.DOCKER_REGISTRY }}
        username: ${{ github.actor }}
        password: ${{ secrets.GITHUB_TOKEN }}
        
    - name: 🏷️ Extract metadata
      id: meta
      uses: docker/metadata-action@v5
      with:
        images: ${{ env.DOCKER_REGISTRY }}/${{ env.IMAGE_NAME }}
        tags: |
          type=ref,event=branch
          type=ref,event=pr
          type=sha,prefix={{branch}}-
          type=raw,value=latest,enable={{is_default_branch}}
          
    - name: 🔨 Build and push Docker image
      uses: docker/build-push-action@v5
      with:
        context: .
        file: ./Dockerfile.api
        push: true
        tags: ${{ steps.meta.outputs.tags }}
        labels: ${{ steps.meta.outputs.labels }}
        cache-from: type=gha
        cache-to: type=gha,mode=max
        
    - name: 🚀 Deploy to production server
      if: github.ref == 'refs/heads/main'
      uses: appleboy/ssh-action@v1.0.0
      with:
        host: ${{ secrets.PROD_HOST }}
        username: ${{ secrets.PROD_USER }}
        key: ${{ secrets.PROD_SSH_KEY }}
        port: ${{ secrets.PROD_PORT }}
        script: |
          # Se connecter au serveur de production
          cd /opt/web-sentinel
          
          # Sauvegarder la configuration actuelle
          docker-compose -f deployment/docker/docker-compose.websentinel.yml logs --tail=100 > logs_backup.txt
          
          # Mettre à jour l'image
          docker-compose -f deployment/docker/docker-compose.websentinel.yml pull web-sentinel-api
          
          # Redémarrer les services
          docker-compose -f deployment/docker/docker-compose.websentinel.yml up -d web-sentinel-api
          
          # Vérifier le déploiement
          sleep 10
          curl -f https://api.taaazzz-prog.fr/api/v1/health || exit 1
          
          # Nettoyer les anciennes images
          docker image prune -f
          
          echo "✅ Déploiement réussi sur taaazzz-prog.fr"
          
  notify-deployment:
    needs: build-and-deploy
    runs-on: ubuntu-latest
    if: always()
    
    steps:
    - name: 📧 Notify deployment status
      if: github.ref == 'refs/heads/main'
      run: |
        if [ "${{ needs.build-and-deploy.result }}" == "success" ]; then
          echo "✅ Web Sentinel API déployé avec succès sur https://api.taaazzz-prog.fr"
          # Optionnel: envoyer une notification Slack/Discord/email
        else
          echo "❌ Échec du déploiement Web Sentinel API"
          exit 1
        fi