name: Build Flutter App

on:
  push:
    branches: [ master, main ]
  workflow_dispatch:

jobs:
  build-android:
    runs-on: ubuntu-latest
    
    steps:
    - uses: actions/checkout@v4
    
    - name: Setup Flutter
      uses: subosito/flutter-action@v2
      with:
        flutter-version: '3.29.0'
        channel: 'stable'
    
    - name: Backup custom code from frontend/
      run: |
        mkdir -p /tmp/flutter_app_backup/lib/pages
        cp -r frontend/lib/* /tmp/flutter_app_backup/lib/ || true
        cp frontend/pubspec.yaml /tmp/flutter_app_backup/ || true
        echo "Backup completed"
        ls -la /tmp/flutter_app_backup/
    
    - name: Remove old frontend and create new Flutter project
      run: |
        rm -rf frontend
        flutter create --org com.homerenovation --project-name home_renovation_app frontend
        echo "New Flutter project created"
    
    - name: Restore custom code
      run: |
        cp -r /tmp/flutter_app_backup/lib/* frontend/lib/ || true
        cp /tmp/flutter_app_backup/pubspec.yaml frontend/pubspec.yaml || true
        echo "Code restored"
        cat frontend/lib/main.dart | head -10
    
    - name: Update API endpoint for web testing
      run: |
        # 替换为你的 OCI 后端公网 IP
        sed -i 's|http://localhost:8000|http://192.18.149.172:8000|g' frontend/lib/pages/upload_page.dart || true
        echo "API endpoint updated to 192.18.149.172"
    
    - name: Get dependencies
      working-directory: ./frontend
      run: flutter pub get
    
    - name: Build APK
      working-directory: ./frontend
      run: flutter build apk --release
    
    - name: Upload APK
      uses: actions/upload-artifact@v4
      with:
        name: app-release-apk
        path: frontend/build/app/outputs/flutter-apk/app-release.apk

  build-web:
    runs-on: ubuntu-latest
    
    steps:
    - uses: actions/checkout@v4
    
    - name: Setup Flutter
      uses: subosito/flutter-action@v2
      with:
        flutter-version: '3.29.0'
        channel: 'stable'
    
    - name: Backup custom code from frontend/
      run: |
        mkdir -p /tmp/flutter_app_backup/lib/pages
        cp -r frontend/lib/* /tmp/flutter_app_backup/lib/ || true
        cp frontend/pubspec.yaml /tmp/flutter_app_backup/ || true
        echo "Backup completed"
    
    - name: Remove old frontend and create new Flutter project
      run: |
        rm -rf frontend
        flutter create --org com.homerenovation --project-name home_renovation_app frontend
    
    - name: Restore custom code
      run: |
        cp -r /tmp/flutter_app_backup/lib/* frontend/lib/ || true
        cp /tmp/flutter_app_backup/pubspec.yaml frontend/pubspec.yaml || true
        echo "Code restored"
    
    - name: Update API endpoint for web testing
      run: |
        sed -i 's|http://localhost:8000|http://your-oci-ip:8000|g' frontend/lib/pages/upload_page.dart || true
    
    - name: Get dependencies
      working-directory: ./frontend
      run: flutter pub get
    
    - name: Build Web
      working-directory: ./frontend
      run: flutter build web --release
    
    - name: Upload Web Build
      uses: actions/upload-artifact@v4
      with:
        name: web-release
        path: frontend/build/web/
