name: Laravel

on:
  push:
    branches: [ "main", "v0" ]
  pull_request:
    branches: [ "main", "v0" ]
    types: [opened, reopened, ready_for_review]
    
jobs:
  quality-test:
    name: Test Code Quality

    runs-on: ubuntu-latest

    steps:
    - uses: shivammathur/setup-php@15c43e89cdef867065b0213be354c2841860869e
      with:
        php-version: '8.3'
    - uses: actions/checkout@v4
    - name: Copy .env
      run: php -r "file_exists('.env') || copy('.env.example', '.env');"
    - name: Install Dependencies
      run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
    - name: Generate key
      run: php artisan key:generate
    - name: Directory Permissions
      run: chmod -R 777 storage bootstrap/cache
    - name: Create Database
      run: |
        mkdir -p database
        touch database/database.sqlite
    - name: Rector Analysis
      run : ./vendor/bin/rector --dry-run
    - name: Larastan Analysis
      run: ./vendor/bin/phpstan analyse
    - name: Laravel Pint Formatter
      run: ./vendor/bin/pint --test
    - name: Slack Notification
      if: always()
      uses: rtCamp/action-slack-notify@v2
      env:
        SLACK_COLOR: ${{ job.status == 'success' && 'success' || 'failure' }}
        SLACK_MESSAGE_ON_SUCCESS: 🚀 Le test de qualité a réussi 🚀
        SLACK_MESSAGE_ON_FAILURE: 🧨 Le test de qualité a échoué 💥
        SLACK_TITLE: Code Quality Test
        SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}

  deployment:
    name: Deploy App on Production
    needs: quality-test
    runs-on: ubuntu-latest
    steps: 
    - name: Deploy
      uses: fjogeleit/http-request-action@v1.16.2
      with:
        url: ${{secrets.WEBHOOK_SERVER_PROD}}
        method: 'GET'
