name: E2E Tests with Playwright

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

jobs:
  test:
    timeout-minutes: 60
    runs-on: ubuntu-latest
    
    steps:
    - name: Checkout code
      uses: actions/checkout@v4

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

    - name: Setup Python
      uses: actions/setup-python@v4
      with:
        python-version: '3.12'

    - name: Setup Miniconda
      uses: conda-incubator/setup-miniconda@v2
      with:
        auto-update-conda: true
        python-version: 3.12
        environment-file: environment.yml
        activate-environment: autobackups

    - name: Install Python dependencies
      shell: bash -l {0}
      run: |
        conda activate autobackups
        pip install -r requirements.txt

    - name: Install Node.js dependencies
      run: npm install

    - name: Install Playwright browsers
      run: npx playwright install --with-deps

    - name: Create test configuration files
      shell: bash -l {0}
      run: |
        # Create minimal test config files if they don't exist
        if [ ! -f config.json ]; then
          echo '{"backup_destination": "/tmp/test-backups", "observation_directories": ["/tmp/test-projects"], "global_settings": {"schedule": "manual"}, "web_interface": {"port": 5000}, "everything_api": {"enabled": false}}' > config.json
        fi
        if [ ! -f projects.json ]; then
          echo '{}' > projects.json
        fi

    - name: Run Playwright tests
      shell: bash -l {0}
      run: |
        conda activate autobackups
        npx playwright test

    - name: Upload test results
      uses: actions/upload-artifact@v3
      if: always()
      with:
        name: playwright-report
        path: test-results/
        retention-days: 30
