name: CI/CD Peekiz

on:
  push:
    branches:
      - staging
      - develop
  pull_request:
    branches:
      - staging
      - develop

jobs:
  # Job de tests pour la branche develop
  tests-develop:
    if: github.base_ref == 'develop' || github.ref == 'refs/heads/develop'
    runs-on: ubuntu-latest

    services:
      mysql:
        image: mysql:8.0
        env:
          MYSQL_ROOT_PASSWORD: root
          MYSQL_DATABASE: test_database
        ports:
          - 3306:3306
        options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3

    steps:
      - uses: actions/checkout@v3

      # Configuration PHP
      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: '8.2'
          extensions: mbstring, intl, pdo_mysql
          coverage: xdebug

      # Installation des dépendances
      - name: Install Dependencies
        run: |
          composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist

      # Préparation de l'environnement
      - name: Setup environment
        run: |
          cp .env.example .env
          php artisan key:generate
          php artisan config:clear
          php artisan config:cache
        env:
          APP_ENV: testing
          DB_DATABASE: test_database
          DB_USERNAME: root
          DB_PASSWORD: root

      # Installation des dépendances front
      - name: Install Node.js
        uses: actions/setup-node@v3
        with:
          node-version: 18

      - name: Install ESLint dependencies only
        run: |
          npm install \
            eslint@8.45.0 \
            @typescript-eslint/eslint-plugin@6.0.0 \
            @typescript-eslint/parser@6.0.0 \
            eslint-plugin-prettier@5.2.3 \
            eslint-plugin-react@7.37.4 \
            eslint-plugin-react-hooks@4.6.0 \
            eslint-plugin-react-refresh@0.4.3

      # Linting Frontend
      - name: Frontend Linting
        run: npm run lint

      # Préparation de la base de données
      - name: Prepare Database
        run: |
          php artisan migrate:fresh --seed
          php artisan roles:update-permissions

      # Tests Backend
      - name: Backend Unit Tests
        run: php artisan test

      # Notification Slack pour les tests de develop
      - name: Slack Notification for Develop Tests
        uses: 8398a7/action-slack@v3
        if: always()
        with:
          status: ${{ job.status }}
          text: |
            ${{ job.status == 'success' && ':white_check_mark: Tests passed' || ':rotating_light: Tests failed' }} for *${{ github.ref == 'refs/heads/develop' && 'develop' || 'income' }}* branch
          fields: repo,message,commit,author,action,eventName,ref,workflow
        env:
          SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }}

  deploy-staging:
    #    needs: tests-develop
    if: github.ref == 'refs/heads/staging' || github.base_ref == 'staging'
    runs-on: ubuntu-latest
    timeout-minutes: 15

    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0

      # Backup du code
      - name: Backup Code
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.STAGING_SERVER_HOST }}
          username: ${{ secrets.STAGING_SERVER_USER }}
          port: ${{ secrets.STAGING_SERVER_PORT }}
          key: ${{ secrets.STAGING_SERVER_SSH_KEY }}
          command_timeout: "5m"
          script: |
            cd ~/www
            deploy/backup_code.sh


      # Backup de la base de données
      - name: Backup Database
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.STAGING_SERVER_HOST }}
          username: ${{ secrets.STAGING_SERVER_USER }}
          port: ${{ secrets.STAGING_SERVER_PORT }}
          key: ${{ secrets.STAGING_SERVER_SSH_KEY }}
          command_timeout: "5m"
          script: |
            cd ~/www
            deploy/backup_db.sh

      # Déploiement via le script entrypoint.sh
      - name: Deploy to Staging
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.STAGING_SERVER_HOST }}
          username: ${{ secrets.STAGING_SERVER_USER }}
          port: ${{ secrets.STAGING_SERVER_PORT }}
          key: ${{ secrets.STAGING_SERVER_SSH_KEY }}
          command_timeout: "5m"
          script: |
            cd ~/www
            deploy/staging/deploy.sh

      # Vérification de la santé du site après déploiement
      - name: Check deployment health
        if: success()
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.STAGING_SERVER_HOST }}
          username: ${{ secrets.STAGING_SERVER_USER }}
          port: ${{ secrets.STAGING_SERVER_PORT }}
          key: ${{ secrets.STAGING_SERVER_SSH_KEY }}
          script: |
            curl -s --head https://staging.peekiz.com | grep "200 OK" && echo "Le site répond correctement" || echo "ATTENTION: Le site ne répond pas correctement"

      # Notification Slack pour le déploiement sur staging
      - name: Slack Notification for Staging Deployment
        uses: 8398a7/action-slack@v3
        if: always()
        with:
          status: ${{ job.status }}
          text: |
            🚀 *Staging Deployment ${{ job.status == 'success' && 'Successful' || 'Failed' }}*
            Déployé par: ${{ github.actor }}
            Commit: ${{ github.sha }}
          fields: repo,message,commit,author,action,eventName,ref,workflow
          mention: here
          if_mention: failure
        env:
          SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }}


