name: Run Tests

on:
  push:
    branches:
      - develop
  pull_request:
    branches:
      - main
      - develop

jobs:
  tests:
    runs-on: ubuntu-latest

    services:
      mysql:
        image: mysql:8.0.30
        env:
          MYSQL_ROOT_PASSWORD: root
          MYSQL_DATABASE: testing
        ports:
          - 3306:3306

    steps:
    - name: Checkout code
      uses: actions/checkout@v3

    - name: Setup PHP
      uses: shivammathur/setup-php@v2
      with:
        php-version: '8.2.4'
        extensions: mbstring, bcmath, gd, pdo_mysql
        ini-values: memory_limit=512M # Pour GD

    - name: Composer Install
      run: composer install --no-progress --no-suggest --prefer-dist

    - 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_CONNECTION: mysql
        DB_HOST: 127.0.0.1
        DB_PORT: 3306
        DB_DATABASE: testing
        DB_USERNAME: root
        DB_PASSWORD: root

    - 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

    - name: Run Linter
      run: npm run lint

    - name: Migration
      run: |
        php artisan migrate:fresh --seed
        php artisan roles:update-permissions 



    - name: Run phpunit tests
      run: php artisan test