name: Frontend Workflow

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - "*"

permissions:
  contents: read

jobs:
  install-front:
    uses: ./.github/workflows/front-dependencies-installation.yml
    with:
      node_version: "22.x"

  build-mails:
    uses: ./.github/workflows/build-mails.yml

  lint-front:
    runs-on: ubuntu-latest
    needs: install-front
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: "22.x"
      - name: Restore the frontend cache
        uses: actions/cache@v4
        with:
          path: "src/frontend/**/node_modules"
          key: front-node_modules-${{ hashFiles('src/frontend/**/yarn.lock') }}
          fail-on-cache-miss: true

      - name: Check linting
        run: cd src/frontend/ && yarn lint

  test-unit:
    runs-on: ubuntu-latest
    needs: install-front
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: "22.x"

      - name: Restore the frontend cache
        uses: actions/cache@v4
        with:
          path: "src/frontend/**/node_modules"
          key: front-node_modules-${{ hashFiles('src/frontend/**/yarn.lock') }}
          fail-on-cache-miss: true

      - name: Run unit tests
        run: |
          cd src/frontend/apps/drive
          yarn test

  build-frontend-e2e:
    runs-on: ubuntu-latest
    needs: install-front
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: "22.x"

      - name: Restore the frontend cache
        uses: actions/cache@v4
        with:
          path: "src/frontend/**/node_modules"
          key: front-node_modules-${{ hashFiles('src/frontend/**/yarn.lock') }}
          fail-on-cache-miss: true

      - name: Build frontend (static export, .env.development)
        run: |
          cd src/frontend/apps/drive
          set -a
          . ./.env.development
          set +a
          yarn build

      - name: Upload frontend bundle
        uses: actions/upload-artifact@v4
        with:
          name: drive-frontend-out
          path: src/frontend/apps/drive/out
          retention-days: 1
          if-no-files-found: error

  test-e2e:
    runs-on: ubuntu-latest
    needs: [install-front, build-mails, build-frontend-e2e]
    strategy:
      fail-fast: false
      matrix:
        browser:
          - chromium
          - webkit
          - firefox
        shard:
          - "1/3"
          - "2/3"
          - "3/3"
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: "22.x"

      - name: Restore the frontend cache
        uses: actions/cache@v4
        with:
          path: "src/frontend/**/node_modules"
          key: front-node_modules-${{ hashFiles('src/frontend/**/yarn.lock') }}
          fail-on-cache-miss: true

      - name: Restore the mail templates
        uses: actions/cache@v4
        with:
          path: "src/backend/core/templates/mail"
          key: mail-templates-${{ hashFiles('src/mail/mjml') }}

      - name: Get Playwright version
        id: playwright-version
        run: |
          VERSION=$(node -p "require('./src/frontend/apps/e2e/package.json').devDependencies['@playwright/test'] || require('./src/frontend/apps/e2e/package.json').dependencies['@playwright/test']")
          echo "version=${VERSION}" >> "$GITHUB_OUTPUT"

      - name: Cache Playwright browsers
        id: playwright-cache
        uses: actions/cache@v4
        with:
          path: ~/.cache/ms-playwright
          key: playwright-${{ runner.os }}-${{ matrix.browser }}-${{ steps.playwright-version.outputs.version }}

      - name: Install Playwright Browsers (cache miss)
        if: steps.playwright-cache.outputs.cache-hit != 'true'
        run: |
          cd src/frontend/apps/e2e
          npx playwright install --with-deps ${{ matrix.browser }}

      - name: Install Playwright system deps (cache hit)
        if: steps.playwright-cache.outputs.cache-hit == 'true'
        run: |
          cd src/frontend/apps/e2e
          npx playwright install-deps ${{ matrix.browser }}

      - name: Build the drive SDK
        run: |
          cd src/frontend/packages/sdk
          yarn build

      - name: Start sdk-consumer dev server
        run: |
          cd src/frontend/apps/sdk-consumer
          nohup yarn dev > /tmp/sdk-consumer.log 2>&1 &
          echo "sdk-consumer PID $!"

      - name: Download frontend bundle
        uses: actions/download-artifact@v4
        with:
          name: drive-frontend-out
          path: src/frontend/apps/drive/out

      - name: Start Docker services
        run: |
          make bootstrap-e2e

      - name: Serve frontend (stock nginx + prod vhost)
        run: |
          docker run -d --rm --name drive-static -p 3000:3000 \
            -v "$PWD/src/frontend/apps/drive/out:/usr/share/nginx/html:ro" \
            -v "$PWD/src/frontend/apps/drive/conf/default.conf:/etc/nginx/conf.d/default.conf:ro" \
            nginx:1.25

      - name: Wait for Keycloak to be ready
        run: |
          timeout 30 bash -c 'while [[ "$(curl -s -o /dev/null -w ''%{http_code}'' http://localhost:8083/)" != "302" ]]; do echo "Waiting for Keycloak..." && sleep 2; done' && echo "Keycloak is ready!"

      - name: Wait for frontend to be ready
        run: |
          timeout 30 bash -c 'while [[ "$(curl -s -o /dev/null -w ''%{http_code}'' http://localhost:3000/)" != "200" ]]; do echo "Waiting for frontend..." && sleep 1; done' && echo "Frontend is ready!"

      - name: Run e2e tests
        run: |
          cd src/frontend/apps/e2e
          yarn test --project=${{ matrix.browser }} --shard=${{ matrix.shard }}

      - uses: actions/upload-artifact@v4
        if: always()
        with:
          name: report-${{ matrix.browser }}-shard-${{ strategy.job-index }}
          path: src/frontend/apps/e2e/report/
          retention-days: 7
