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: Verify Flutter setup
        shell: powershell
        run: |
          flutter --version
          flutter doctor -v

      - name: Create production .env file
        working-directory: flutter_app
        shell: powershell
        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: powershell
        run: flutter pub get

      - name: Setup Certificate for Signing
        working-directory: flutter_app
        shell: powershell
        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: powershell
        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)"
            }
          }
          
          # Build self-extracting portable exe (single file distribution)
          Write-Host "Building self-extracting portable executable..."
          .\build_sfx.ps1
          
          # Copy SFX exe to root for upload
          if (Test-Path "build\windows\sfx\rmtPocketWatcher-v$version-Portable.exe") {
            Copy-Item "build\windows\sfx\rmtPocketWatcher-v$version-Portable.exe" "rmtPocketWatcher-Windows-Portable-v$version.exe" -Force
            Write-Host "Created rmtPocketWatcher-Windows-Portable-v$version.exe"
          }
          
          # Copy MSIX to root for easier upload
          $msixFile = Get-ChildItem -Path "build\windows\x64\runner\Release" -Filter "*.msix" -ErrorAction SilentlyContinue | Select-Object -First 1
          if ($msixFile) {
            Copy-Item $msixFile.FullName "rmtPocketWatcher-Windows-v$version.msix" -Force
            Write-Host "Created rmtPocketWatcher-Windows-v$version.msix"
          }
          
          # Export certificate for user installation (if certificate exists)
          if (Test-Path "certificates\rmtPocketWatcher.pfx") {
            Write-Host "Exporting certificate for user installation..."
            try {
              # Use PowerShell to export the certificate
              $pfxPath = "certificates\rmtPocketWatcher.pfx"
              $cerPath = "rmtPocketWatcher-Certificate.cer"
              $certPassword = if ($env:CERT_PASSWORD) { $env:CERT_PASSWORD } else { "rmtPocketWatcher2024!" }
              
              # Load PFX and export public certificate
              $cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($pfxPath, $certPassword)
              $certBytes = $cert.Export([System.Security.Cryptography.X509Certificates.X509ContentType]::Cert)
              [System.IO.File]::WriteAllBytes($cerPath, $certBytes)
              
              Write-Host "✅ Certificate exported: $cerPath" -ForegroundColor Green
            } catch {
              Write-Warning "Failed to export certificate: $($_.Exception.Message)"
            }
          }
          
          # List created artifacts
          Write-Host "Artifacts created:"
          Get-ChildItem -Filter "*.zip" | ForEach-Object { Write-Host "  - $($_.Name)" }
          Get-ChildItem -Filter "*Portable*.exe" | ForEach-Object { Write-Host "  - $($_.Name)" }
          Get-ChildItem -Filter "*.msix" | ForEach-Object { Write-Host "  - $($_.Name)" }
          Get-ChildItem -Filter "*.cer" | ForEach-Object { Write-Host "  - $($_.Name)" }

      - name: Upload Windows artifacts
        uses: actions/upload-artifact@v3
        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 }}.exe
            flutter_app/rmtPocketWatcher-Windows-v${{ needs.get-version.outputs.version }}.msix
            flutter_app/rmtPocketWatcher-Certificate.cer
          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:
          channel: 'stable'
          cache: false

      - 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
          echo "Created .env file:"
          cat .env | sed 's/=.*/=***/' # Show keys but mask values

      - 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 --verbose

      - 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@v3
        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@v3
        with:
          name: rmtPocketWatcher-Windows
          path: ./artifacts

      - name: Download Android artifact
        uses: actions/download-artifact@v3
        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 }}.exe` - Single self-extracting executable
            - **Windows (Installer)**: `rmtPocketWatcher-Windows-v${{ needs.get-version.outputs.version }}.msix` - Windows Store-style installer
            - **Certificate**: `rmtPocketWatcher-Certificate.cer` - Required for signed executables (see installation notes)
            - **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
            
            #### Certificate Installation (Required for signed executables)
            If Windows shows "Unknown publisher" warnings, install the certificate first:
            1. Download `rmtPocketWatcher-Certificate.cer`
            2. Right-click → "Install Certificate"
            3. Choose "Local Machine" → "Place all certificates in the following store"
            4. Browse → Select "Trusted Root Certification Authorities" → OK
            5. Complete the installation
            
            #### Application Installation
            **Windows (Full)**: Extract the ZIP file and run `rmtpocketwatcher.exe` - includes all dependencies
            **Windows (Portable)**: Just run the .exe - auto-extracts to AppData and launches  
            **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 }}.exe
            ./artifacts/rmtPocketWatcher-Windows-v${{ needs.get-version.outputs.version }}.msix
            ./artifacts/rmtPocketWatcher-Certificate.cer
            ./artifacts/rmtPocketWatcher-Android-v${{ needs.get-version.outputs.version }}.apk
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
