name: tests

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

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - name: 🔍 Checkout
        uses: actions/checkout@v6

      - name: 🐘 Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: "8.4"

      - name: 🟢 Setup Node
        uses: actions/setup-node@v6
        with:
          node-version: "25"
          cache: "yarn"

      - name: 📦 Install dependencies
        run: |
          composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
          yarn

      - name: 📥 Cache vendor folder
        uses: actions/cache@v3
        with:
          path: vendor
          key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}

      - name: 🔍 Lint backend
        run: vendor/bin/pint --test --bail

      - name: 🔍 Check backend
        run: vendor/bin/phpstan analyze

      - name: 🔎 Lint frontend
        run: yarn lint
  test:
    runs-on: ubuntu-latest

    services:
      mysql:
        image: mysql:8
        env:
          MYSQL_ALLOW_EMPTY_PASSWORD: false
          MYSQL_ROOT_PASSWORD: root
          MYSQL_DATABASE: ilec
        ports:
          - 3306/tcp
        options: >-
          --health-cmd="mysqladmin ping"
          --health-interval=10s
          --health-timeout=5s
          --health-retries=3

    steps:
      - name: Checkout
        uses: actions/checkout@v6

      - name: 🐘 Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: "8.4"

      - name: 🟢 Setup Node
        uses: actions/setup-node@v6
        with:
          node-version: "25"
          cache: "yarn"

      - name: 📦 Install dependencies
        run: |
          composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
          yarn

      - name: 📥 Cache vendor folder
        uses: actions/cache@v3
        with:
          path: vendor
          key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}

      - name: 🔒 Copy environment file
        run: cp .env.testing .env

      - name: 🔑 Generate application key
        run: php artisan key:generate

      - name: 🗄 Setup database
        run: php artisan ilec:remigrate -vvv
        env:
          DB_PORT: ${{ job.services.mysql.ports['3306'] }}

      - name: 🎨 Build assets
        run: yarn build

      - name: ▶️ Start application
        run: php artisan serve &
        env:
          DB_PORT: ${{ job.services.mysql.ports['3306'] }}

      - name: ✅ Running E2E tests
        uses: cypress-io/github-action@v6
        with:
          install: false
          wait-on: http://127.0.0.1:8000
      - uses: actions/upload-artifact@v6
        if: failure()
        with:
          name: cypress-screenshots
          path: cypress/screenshots
      - uses: actions/upload-artifact@v6
        if: failure()
        with:
          name: cypress-videos
          path: cypress/videos
      - uses: actions/upload-artifact@v6
        if: failure()
        with:
          name: logs
          path: storage
