name: Server Tests

on:
  push:
    branches: [ main, develop ]
    paths:
      - 'packages/server/**'
      - '.github/workflows/server-tests.yml'
  pull_request:
    branches: [ main ]
    paths:
      - 'packages/server/**'
      - '.github/workflows/server-tests.yml'
  workflow_dispatch:

jobs:
  server-tests:
    runs-on: ubuntu-latest
    timeout-minutes: 15

    services:
      postgres:
        image: postgres:17
        env:
          POSTGRES_DB: test_d8dai
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: test_password
        options: >-
          --health-cmd="pg_isready -U postgres"
          --health-interval=10s
          --health-timeout=5s
          --health-retries=3
        ports:
          - 5432:5432

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

    - name: Setup Node.js
      uses: actions/setup-node@v4
      with:
        node-version: '20'
        cache: 'pnpm'

    - name: Install pnpm
      uses: pnpm/action-setup@v2
      with:
        version: 8

    - name: Install dependencies
      run: pnpm install --frozen-lockfile

    - name: Setup test environment
      run: |
        cp .env.test.example .env.test
        echo "TEST_DATABASE_URL=postgresql://postgres:test_password@localhost:5432/test_d8dai" >> .env.test
        echo "NODE_ENV=test" >> .env.test
        echo "JWT_SECRET=test_jwt_secret_1234567890" >> .env.test

    - name: Run database migrations
      run: |
        export NODE_ENV=test
        cd packages/server
        pnpm db:migrate

    - name: Run server unit tests
      run: |
        export NODE_ENV=test
        cd packages/server
        pnpm test:unit

    - name: Run server integration tests
      run: |
        export NODE_ENV=test
        cd packages/server
        pnpm test:integration

    - name: Generate server test coverage
      run: |
        export NODE_ENV=test
        cd packages/server
        pnpm test:coverage

    - name: Upload test results
      if: always()
      uses: actions/upload-artifact@v4
      with:
        name: server-test-results
        path: |
          packages/server/coverage/
          packages/server/test-results/
        retention-days: 7

    - name: Upload coverage to Codecov
      if: success()
      uses: codecov/codecov-action@v3
      with:
        file: ./packages/server/coverage/coverage-final.json
        flags: server-tests

    - name: Generate test summary
      if: always()
      uses: test-summary/action@v2
      with:
        paths: packages/server/test-results/junit.xml

    - name: Notify on failure
      if: failure()
      uses: 8398a7/action-slack@v3
      with:
        status: ${{ job.status }}
        channel: '#ci-notifications'
        webhook_url: ${{ secrets.SLACK_WEBHOOK }}
      env:
        SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }}

    - name: Send test results to GitHub
      if: always()
      uses: dorny/test-reporter@v1
      with:
        name: Server Tests
        path: packages/server/test-results/junit.xml
        reporter: jest-junit
        fail-on-error: false