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
        shell: bash
        run: |
          VERSION=$(grep '^version:' pubspec.yaml | sed 's/version: //' | sed 's/+.*//')
          echo "VERSION=$VERSION" >> $GITHUB_OUTPUT
          echo "Version: $VERSION"

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

      - name: Setup Git Bash (for Flutter internal scripts)
        shell: pwsh
        run: |
          # Add Git Bash to PATH for Flutter internal scripts that might need it
          $gitPath = "${env:ProgramFiles}\Git\bin"
          if (Test-Path $gitPath) {
            $env:PATH = "$gitPath;$env:PATH"
            echo "PATH=$env:PATH" >> $env:GITHUB_ENV
            Write-Host "Added Git Bash to PATH: $gitPath"
          } else {
            Write-Host "Git Bash not found at expected location"
          }

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

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

      - name: Verify Flutter setup
        shell: pwsh
        run: flutter doctor -v

      - name: Create production .env file
        working-directory: flutter_app
        shell: pwsh
        env:
          WS_URL: ${{ secrets.WS_URL }}
          API_URL: ${{ secrets.API_URL }}
        run: |
          "WS_URL=$env:WS_URL" | Out-File -FilePath .env -Encoding utf8
          "API_URL=$env:API_URL" | Out-File -FilePath .env -Append -Encoding utf8

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

      - name: Setup Certificate for Signing
        working-directory: flutter_app
        shell: pwsh
        env:
          CERT_BASE64: ${{ secrets.CERT_BASE64 }}
          CERT_PASSWORD: ${{ secrets.CERT_PASSWORD }}
        run: |
          if ($env:CERT_BASE64) {
            Write-Host "Setting up certificate for code signing..." -ForegroundColor Green
            
            # Create certificates directory if it doesn't exist
            if (-not (Test-Path "certificates")) {
              New-Item -ItemType Directory -Path "certificates" -Force | Out-Null
            }
            
            # Decode base64 certificate and save as PFX
            $certBytes = [System.Convert]::FromBase64String($env:CERT_BASE64)
            [System.IO.File]::WriteAllBytes("certificates\rmtPocketWatcher.pfx", $certBytes)
            
            Write-Host "✅ Certificate installed successfully" -ForegroundColor Green
          } else {
            Write-Host "⚠️ No certificate provided - building unsigned" -ForegroundColor Yellow
          }

      - name: Build Windows release with installer
        working-directory: flutter_app
        shell: pwsh
        env:
          CERT_PASSWORD: ${{ secrets.CERT_PASSWORD }}
        run: |
          # Set certificate password environment variable for build script
          if ($env:CERT_PASSWORD) {
            $env:MSIX_CERTIFICATE_PASSWORD = $env:CERT_PASSWORD
          }
          
          # Run our custom build script
          .\build_windows.ps1 -Release
          
          # The build script creates: build\rmtPocketWatcher-Windows-v{version}-release.zip
          # Rename to simpler format for release
          $version = "${{ needs.get-version.outputs.version }}"
          
          # Find the generated zip and rename it
          $sourceZip = "build\rmtPocketWatcher-Windows-v$version-release.zip"
          if (Test-Path $sourceZip) {
            Move-Item $sourceZip "rmtPocketWatcher-Windows-v$version.zip" -Force
            Write-Host "Created rmtPocketWatcher-Windows-v$version.zip"
          } else {
            # Fallback: find any matching zip
            $zipFiles = Get-ChildItem -Path "build" -Filter "rmtPocketWatcher-Windows-*.zip" -ErrorAction SilentlyContinue
            if ($zipFiles) {
              Move-Item $zipFiles[0].FullName "rmtPocketWatcher-Windows-v$version.zip" -Force
              Write-Host "Created rmtPocketWatcher-Windows-v$version.zip from $($zipFiles[0].Name)"
            }
          }
          
          # Create portable (single exe) archive
          if (Test-Path "build\windows\standalone\rmtpocketwatcher.exe") {
            Compress-Archive -Path "build\windows\standalone\rmtpocketwatcher.exe" -DestinationPath "rmtPocketWatcher-Windows-Portable-v$version.zip" -CompressionLevel Optimal -Force
            Write-Host "Created rmtPocketWatcher-Windows-Portable-v$version.zip"
          }
          
          # List created artifacts
          Write-Host "Artifacts created:"
          Get-ChildItem -Filter "*.zip" | ForEach-Object { Write-Host "  - $($_.Name)" }

      - name: Upload Windows artifacts
        uses: actions/upload-artifact@v4
        with:
          name: rmtPocketWatcher-Windows
          path: |
            flutter_app/rmtPocketWatcher-Windows-v${{ needs.get-version.outputs.version }}.zip
            flutter_app/rmtPocketWatcher-Windows-Portable-v${{ needs.get-version.outputs.version }}.zip
            flutter_app/build/windows/x64/runner/Release/*.msix
          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 Flutter
        uses: subosito/flutter-action@v2
        with:
          flutter-version: '3.38.4'
          channel: 'stable'
          cache: true

      - name: Setup Android SDK
        uses: android-actions/setup-android@v3
        
      - name: Accept Android licenses
        shell: bash
        run: yes | sdkmanager --licenses || true
        
      - name: Verify Flutter setup
        shell: bash
        run: flutter doctor -v
          
      - name: Create production .env file
        working-directory: flutter_app
        shell: bash
        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
        shell: bash
        run: flutter pub get

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

      - name: Rename APK
        working-directory: flutter_app
        shell: bash
        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 (Full)**: `rmtPocketWatcher-Windows-v${{ needs.get-version.outputs.version }}.zip` - Complete standalone package
            - **Windows (Portable)**: `rmtPocketWatcher-Windows-Portable-v${{ needs.get-version.outputs.version }}.zip` - Single executable only
            - **Windows (Installer)**: `*.msix` - Windows Store-style installer (if available)
            - **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 (Full)**: Extract the ZIP file and run `rmtpocketwatcher.exe` - includes all dependencies
            **Windows (Portable)**: Single executable, no installation needed - just run `rmtpocketwatcher.exe`
            **Windows (Installer)**: Double-click the MSIX file for Windows Store-style installation
            **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-Windows-Portable-v${{ needs.get-version.outputs.version }}.zip
            ./artifacts/*.msix
            ./artifacts/rmtPocketWatcher-Android-v${{ needs.get-version.outputs.version }}.apk
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
