name: Flutter Release

on:
  workflow_dispatch:
  push:
    paths:
      - 'flutter_app/pubspec.yaml'
    branches:
      - main

jobs:
  get-version:
    runs-on: ubuntu-latest
    outputs:
      version: ${{ steps.version.outputs.VERSION }}
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
      
      - name: Get version from pubspec.yaml
        id: version
        working-directory: flutter_app
        run: |
          VERSION=$(grep '^version:' pubspec.yaml | sed 's/version: //' | sed 's/+.*//')
          echo "VERSION=$VERSION" >> $GITHUB_OUTPUT
          echo "Version: $VERSION"

  build-windows:
    runs-on: windows
    needs: get-version
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Setup Flutter
        uses: subosito/flutter-action@v2
        with:
          flutter-version: '3.22.3'
          channel: 'stable'
          cache: true

      - name: Enable Windows desktop
        run: flutter config --enable-windows-desktop

      - name: Create production .env file
        working-directory: flutter_app
        env:
          WS_URL: ${{ secrets.WS_URL }}
          API_URL: ${{ secrets.API_URL }}
        run: |
          echo "WS_URL=$env:WS_URL" > .env
          echo "API_URL=$env:API_URL" >> .env

      - name: Install dependencies
        working-directory: flutter_app
        run: flutter pub get

      - name: Flutter doctor
        run: flutter doctor -v

      - name: Build Windows release
        working-directory: flutter_app
        run: flutter build windows --release

      - name: Create Windows archive
        working-directory: flutter_app
        run: |
          Compress-Archive -Path "build\windows\x64\runner\Release\*" -DestinationPath "rmtPocketWatcher-Windows-v${{ needs.get-version.outputs.version }}.zip"

      - name: Upload Windows artifact
        uses: actions/upload-artifact@v4
        with:
          name: rmtPocketWatcher-Windows
          path: flutter_app/rmtPocketWatcher-Windows-v${{ needs.get-version.outputs.version }}.zip
          retention-days: 30

  build-android:
    runs-on: ubuntu-latest
    needs: get-version
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Setup Java
        uses: actions/setup-java@v4
        with:
          distribution: 'temurin'
          java-version: '17'

      - name: Setup Android SDK
        uses: android-actions/setup-android@v3
        with:
          api-level: 34
          build-tools: 34.0.0
          cmake: 3.22.1
          ndk: 25.1.8937393

      - name: Accept Android SDK licenses
        run: yes | $ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager --licenses || true

      - name: Setup Flutter
        uses: subosito/flutter-action@v2
        with:
          flutter-version: '3.22.3'
          channel: 'stable'
          cache: true

      - name: Accept Flutter Android licenses
        run: flutter doctor --android-licenses || true

      - name: Flutter doctor
        run: flutter doctor -v

      - name: Create production .env file
        working-directory: flutter_app
        env:
          WS_URL: ${{ secrets.WS_URL }}
          API_URL: ${{ secrets.API_URL }}
        run: |
          echo "WS_URL=$WS_URL" > .env
          echo "API_URL=$API_URL" >> .env

      - name: Install dependencies
        working-directory: flutter_app
        run: flutter pub get

      - name: Clean build
        working-directory: flutter_app
        run: flutter clean

      - name: Build Android APK
        working-directory: flutter_app
        run: flutter build apk --release --verbose

      - name: Verify APK exists
        working-directory: flutter_app
        run: |
          ls -la build/app/outputs/flutter-apk/
          file build/app/outputs/flutter-apk/app-release.apk

      - name: Rename APK
        working-directory: flutter_app
        run: |
          cp build/app/outputs/flutter-apk/app-release.apk rmtPocketWatcher-Android-v${{ needs.get-version.outputs.version }}.apk

      - name: Upload Android artifact
        uses: actions/upload-artifact@v4
        with:
          name: rmtPocketWatcher-Android
          path: flutter_app/rmtPocketWatcher-Android-v${{ needs.get-version.outputs.version }}.apk
          retention-days: 30

  create-release:
    runs-on: ubuntu-latest
    needs: [get-version, build-windows, build-android]
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Download Windows artifact
        uses: actions/download-artifact@v4
        with:
          name: rmtPocketWatcher-Windows
          path: ./artifacts

      - name: Download Android artifact
        uses: actions/download-artifact@v4
        with:
          name: rmtPocketWatcher-Android
          path: ./artifacts

      - name: Create Release
        uses: softprops/action-gh-release@v1
        with:
          tag_name: v${{ needs.get-version.outputs.version }}
          name: rmtPocketWatcher v${{ needs.get-version.outputs.version }}
          draft: false
          prerelease: false
          body: |
            ## rmtPocketWatcher v${{ needs.get-version.outputs.version }}
            
            **Lambda Banking Conglomerate** - Star Citizen AUEC Price Tracker
            
            ### Downloads
            - **Windows**: `rmtPocketWatcher-Windows-v${{ needs.get-version.outputs.version }}.zip`
            - **Android**: `rmtPocketWatcher-Android-v${{ needs.get-version.outputs.version }}.apk`
            
            ### Features
            - Real-time AUEC price tracking from multiple vendors
            - Bloomberg-style terminal interface
            - Cross-platform native notifications with custom sound
            - Historical price charts and trend analysis
            - Client-side price alerts
            - Vendor comparison tables
            
            ### Installation
            **Windows**: Extract the ZIP file and run `rmtpocketwatcher.exe`
            **Android**: Install the APK file (enable "Install from unknown sources")
            
            ---
            *Built with Flutter for cross-platform compatibility*
          files: |
            ./artifacts/rmtPocketWatcher-Windows-v${{ needs.get-version.outputs.version }}.zip
            ./artifacts/rmtPocketWatcher-Android-v${{ needs.get-version.outputs.version }}.apk
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
