name: NordaBiz Tests

on:
  push:
    branches: [master, develop]
  pull_request:
    branches: [master]

env:
  PYTHON_VERSION: '3.11'

jobs:
  # =============================================================================
  # Unit and Integration Tests
  # =============================================================================
  unit-tests:
    name: Unit & Integration Tests
    runs-on: ubuntu-latest

    services:
      postgres:
        image: postgres:14
        env:
          POSTGRES_USER: nordabiz_test
          POSTGRES_PASSWORD: testpassword
          POSTGRES_DB: nordabiz_test
        ports:
          - 5432:5432
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

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

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

      - name: Install dependencies
        run: |
          pip install --upgrade pip
          pip install -r requirements.txt

      - name: Run unit tests
        run: |
          pytest tests/unit/ -v --cov=. --cov-report=xml
        env:
          TESTING: 'true'
          SECRET_KEY: 'test-secret-key-for-ci-cd-minimum-32-chars-long'
          DATABASE_URL: postgresql://nordabiz_test:testpassword@localhost:5432/nordabiz_test

      - name: Run integration tests
        run: |
          pytest tests/integration/ -v --cov=. --cov-report=xml --cov-append
        env:
          TESTING: 'true'
          SECRET_KEY: 'test-secret-key-for-ci-cd-minimum-32-chars-long'
          DATABASE_URL: postgresql://nordabiz_test:testpassword@localhost:5432/nordabiz_test

      - name: Run security tests
        run: |
          pytest tests/security/ -v
        env:
          TESTING: 'true'
          SECRET_KEY: 'test-secret-key-for-ci-cd-minimum-32-chars-long'
          DATABASE_URL: postgresql://nordabiz_test:testpassword@localhost:5432/nordabiz_test

      - name: Check coverage
        run: |
          coverage report --fail-under=80
        continue-on-error: true  # Don't fail build on coverage (for now)

      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v4
        with:
          files: ./coverage.xml
          fail_ci_if_error: false

  # =============================================================================
  # E2E Tests (on staging)
  # =============================================================================
  e2e-tests:
    name: E2E Tests (Playwright)
    runs-on: ubuntu-latest
    needs: unit-tests  # Only run if unit tests pass

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

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

      - name: Install dependencies
        run: |
          pip install --upgrade pip
          pip install -r requirements.txt
          playwright install chromium

      - name: Run E2E tests on staging
        run: |
          pytest tests/e2e/ -v --base-url=${{ secrets.STAGING_URL }}
        env:
          BASE_URL: ${{ secrets.STAGING_URL }}
          TEST_USER_EMAIL: ${{ secrets.TEST_USER_EMAIL }}
          TEST_USER_PASSWORD: ${{ secrets.TEST_USER_PASSWORD }}
        continue-on-error: true  # E2E tests may fail if staging unavailable

  # =============================================================================
  # Smoke Tests (on production) - only on master
  # =============================================================================
  smoke-tests:
    name: Smoke Tests (Production)
    runs-on: ubuntu-latest
    needs: unit-tests
    if: github.ref == 'refs/heads/master'

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

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

      - name: Install dependencies
        run: |
          pip install pytest requests

      - name: Run smoke tests
        run: |
          pytest tests/smoke/test_production_health.py -v
        env:
          PROD_URL: https://nordabiznes.pl

  # =============================================================================
  # Notify on Failure
  # =============================================================================
  notify-on-failure:
    name: Send Failure Notification
    runs-on: ubuntu-latest
    needs: [unit-tests, e2e-tests]
    if: failure()

    steps:
      - name: Send failure email
        uses: dawidd6/action-send-mail@v3
        with:
          server_address: smtp.gmail.com
          server_port: 587
          username: ${{ secrets.EMAIL_USERNAME }}
          password: ${{ secrets.EMAIL_PASSWORD }}
          subject: "❌ NordaBiz Tests Failed - ${{ github.ref_name }}"
          to: ${{ secrets.NOTIFY_EMAIL }}
          from: NordaBiz CI <ci@nordabiznes.pl>
          body: |
            Testy nie przeszły!

            Branch: ${{ github.ref_name }}
            Commit: ${{ github.sha }}
            Autor: ${{ github.actor }}
            Wiadomość: ${{ github.event.head_commit.message }}

            Zobacz szczegóły:
            ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
