name: PHP Code Testing

on:
  push:
    branches:
      - master
      - 'releases/**'
      - 'feat_**'
      - staging
      - production
  pull_request:
  workflow_dispatch:

jobs:
  tests:
    if: "! contains(toJSON(github.event.commits.*.message), '[skip-ci]')"
    runs-on: ubuntu-latest
    strategy:
      matrix:
        php-versions: ['8.0']
    name: Testing ${{ matrix.php-versions }}
    steps:
    - name: Checkout
      uses: actions/checkout@v4

    - name: Dump GitHub context
      env:
        GITHUB_CONTEXT: ${{ toJson(github) }}
      run: echo "$GITHUB_CONTEXT"

    - name: Setup PHP
      uses: shivammathur/setup-php@v2
      with:
        php-version: ${{ matrix.php-versions }}
        extensions: mbstring, intl
        ini-values: post_max_size=256M, log_errors = on, error_reporting = 32767, error_log = /tmp/php.log
        coverage: xdebug, pcov

    - name: Setup NodeJS
      uses: actions/setup-node@v4
      with:
        node-version: '14'

    - name: Build frontend
      working-directory: frontend
      run: |
        npm ci
        npm run build

    - name: Install dependencies
      working-directory: backend
      run: |
        composer install --prefer-dist --no-progress

  staging_deployment:
    if: github.event.ref == 'refs/heads/staging'
    needs: tests
    runs-on: ubuntu-latest
    environment: staging
    name: Deploy to staging
    steps:
    - name: Checkout
      uses: actions/checkout@v4

    - name: Setup PHP
      uses: shivammathur/setup-php@v2
      with:
        php-version: 8.0
        extensions: mbstring, intl
        ini-values: post_max_size=256M, log_errors = on, error_reporting = 32767, error_log = /tmp/php.log
        coverage: xdebug, pcov

    - name: Setup NodeJS
      uses: actions/setup-node@v4
      with:
        node-version: '14'

    - name: Deploy
      run: |
        ls
        sudo wget -O deployment.phar https://github.com/dg/ftp-deployment/releases/download/v3.4.0/deployment.phar
        echo '<?php $remotes = [ "staging" => [ "remote" => "${{ secrets.DEPLOY_URL }}", "sentry_env" => "staging", "skip_composer_upload" => true ] ]; ?>' > deployment_remotes.php
        cat deployment_remotes.php
        ls
        php deployment.phar deployment.php --section staging
        php -r 'require("deployment_remotes.php");'
        cat deployment.log | grep "After-jobs:" ; exit $?

    - uses: actions/upload-artifact@v4
      if: ${{ always() }}
      with:
        name: deploy_php_log_${{ github.run_id }}
        path: /tmp/php.log

    - uses: actions/upload-artifact@v4
      if: ${{ always() }}
      with:
        name: deploy_log_${{ github.run_id }}
        path: deployment.log

  production_deployment:
    if: github.event.ref == 'refs/heads/production'
    needs: tests
    runs-on: ubuntu-latest
    environment: production
    name: Deploy to production
    steps:
    - name: Checkout
      uses: actions/checkout@v4

    - name: Setup PHP
      uses: shivammathur/setup-php@v2
      with:
        php-version: 8.0
        extensions: mbstring, intl
        ini-values: post_max_size=256M, log_errors = on, error_reporting = 32767, error_log = /tmp/php.log
        coverage: xdebug, pcov

    - name: Setup NodeJS
      uses: actions/setup-node@v4
      with:
        node-version: '14'

    - name: Deploy
      run: |
        ls
        sudo wget -O deployment.phar https://github.com/dg/ftp-deployment/releases/download/v3.4.0/deployment.phar
        echo '<?php $remotes = [ "production" => [ "remote" => "${{ secrets.DEPLOY_URL }}", "sentry_env" => "prod", "skip_composer_upload" => true ] ]; ?>' > deployment_remotes.php
        cat deployment_remotes.php
        ls
        php deployment.phar deployment.php --section production
        php -r 'require("deployment_remotes.php");'
        cat deployment.log | grep "After-jobs:" ; exit $?

    - uses: actions/upload-artifact@v4
      if: ${{ always() }}
      with:
        name: deploy_php_log_${{ github.run_id }}
        path: /tmp/php.log

    - uses: actions/upload-artifact@v4
      if: ${{ always() }}
      with:
        name: deploy_log_${{ github.run_id }}
        path: deployment.log

