name: build-pr

permissions:
  contents: read

on:
  pull_request:
    branches:
      - master
      - dev
      - testing
    paths-ignore:
      - "**.md"
      - ".github/ISSUE_TEMPLATE/**"

env:
  BUN_VERSION: latest
  NODE_VERSION: 20
  PYTHON_VERSION: "3.13"

jobs:
  test:
    name: Run Tests & Build Validation
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: ${{ env.PYTHON_VERSION }}

      - name: Install uv
        run: pip install uv

      - name: Install Python dependencies
        run: uv sync

      - name: Run Python linting
        run: uv run ruff check app/

      - name: Run Python tests
        run: uv run pytest app/tests/ -v --tb=short

      - name: Cache bun installation
        id: cache-bun
        uses: actions/cache@v4
        with:
          path: ~/.bun/install/cache
          key: ${{ runner.os }}-bun-${{ hashFiles('**/bun.lock') }}
          restore-keys: |
            ${{ runner.os }}-bun-

      - name: Install Node.js
        uses: actions/setup-node@v4
        with:
          node-version: ${{ env.NODE_VERSION }}

      - name: Install bun
        uses: oven-sh/setup-bun@v2
        with:
          bun-version: ${{ env.BUN_VERSION }}

      - name: Install frontend dependencies
        working-directory: ui
        env:
          NODE_ENV: development
        run: bun install --frozen-lockfile

      - name: Prepare frontend (nuxt prepare)
        working-directory: ui
        env:
          NODE_ENV: development
        run: bun nuxt prepare

      - name: Run frontend linting
        working-directory: ui
        run: bun run lint

      - name: Run frontend type checking
        working-directory: ui
        run: bun run typecheck

      - name: Run frontend tsc
        working-directory: ui
        run: bun run lint:tsc

      - name: Run frontend tests
        working-directory: ui
        run: bun run test:ci

      - name: Build frontend
        working-directory: ui
        run: bun run generate

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Validate Docker build
        uses: docker/build-push-action@v5
        with:
          context: .
          platforms: linux/amd64
          push: false
          cache-from: type=gha,scope=${{ github.workflow }}
          cache-to: type=gha,mode=max,scope=${{ github.workflow }}
          provenance: false
