name: Flutter Test & Quality Check

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

jobs:
  test:
    name: Test on ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest]
        
    steps:
    - name: Checkout repository
      uses: actions/checkout@v3
      
    - name: Setup Flutter
      uses: subosito/flutter-action@v2
      with:
        flutter-version: '3.x'
        channel: 'stable'
        
    - name: Get dependencies
      run: flutter pub get
      
    - name: Run build_runner
      run: flutter pub run build_runner build --delete-conflicting-outputs
      
    - name: Analyze code
      run: flutter analyze
      
    - name: Run unit tests
      run: flutter test test/unit --coverage --reporter json > test-results-unit.json
      continue-on-error: true
      
    - name: Run widget tests  
      run: flutter test test/widget --coverage --reporter json > test-results-widget.json
      continue-on-error: true
      
    - name: Run integration tests
      run: flutter test test/integration --reporter json > test-results-integration.json
      continue-on-error: true
      
    - name: Upload test results
      uses: actions/upload-artifact@v3
      if: always()
      with:
        name: test-results-${{ matrix.os }}
        path: test-results-*.json
        
    - name: Upload coverage to Codecov
      uses: codecov/codecov-action@v3
      if: matrix.os == 'ubuntu-latest'
      with:
        file: ./coverage/lcov.info
        flags: unittests
        name: codecov-umbrella
        
    - name: Generate test report
      if: always()
      run: |
        echo "# Test Results Summary" > test-summary.md
        echo "## Platform: ${{ matrix.os }}" >> test-summary.md
        echo "### Unit Tests" >> test-summary.md
        if [ -f test-results-unit.json ]; then
          echo '```json' >> test-summary.md
          cat test-results-unit.json | head -20 >> test-summary.md
          echo '```' >> test-summary.md
        fi
        
    - name: Comment PR with test results
      uses: actions/github-script@v6
      if: github.event_name == 'pull_request'
      with:
        script: |
          const fs = require('fs');
          const testSummary = fs.readFileSync('test-summary.md', 'utf8');
          github.rest.issues.createComment({
            issue_number: context.issue.number,
            owner: context.repo.owner,
            repo: context.repo.repo,
            body: testSummary
          });

  build:
    name: Build APK
    runs-on: ubuntu-latest
    needs: test
    
    steps:
    - uses: actions/checkout@v3
    
    - uses: subosito/flutter-action@v2
      with:
        flutter-version: '3.x'
        
    - run: flutter pub get
    
    - run: flutter pub run build_runner build --delete-conflicting-outputs
    
    - name: Build APK
      run: flutter build apk --release
      
    - name: Upload APK
      uses: actions/upload-artifact@v3
      with:
        name: app-release
        path: build/app/outputs/flutter-apk/app-release.apk