name: Build and Release FLUX (Multi-Platform)
on:
  push:
    tags:
      - 'v*'

jobs:
  # -----------------------------------------------------------------
  # JOB 1: WINDOWS (Gira sul PC del collega appena si libera)
  # -----------------------------------------------------------------
  build-windows:
    runs-on: windows-native
    steps:
      - name: Checkout del codice
        uses: actions/checkout@v3

      - name: Crea file .env
        run: |
          Set-Content -Path ".env" -Value "${{ secrets.ENV_FILE_CONTENT }}"

      - name: Build Flutter Windows
        run: flutter build windows --release

      # 1. FIRMA DELL'ESEGUIBILE RAW
      - name: Firma Eseguibile Flutter
        run: |
          # Cerca dinamicamente signtool.exe nell'SDK di Windows per non sbagliare percorso
          $Signtool = (Get-ChildItem "C:\Program Files (x86)\Windows Kits\10\bin\*\x64\signtool.exe" | Sort-Object FullName -Descending | Select-Object -First 1).FullName
          
          # Sostituisci il percorso con dove hai salvato fisicamente il file .pfx sul PC del runner!
          & $Signtool sign /f "C:\flux_privato.pfx" /p "${{ secrets.PFX_PASSWORD }}" /fd SHA256 "build\windows\x64\runner\Release\flux.exe"

      - name: Build Windows Installer
        run: |
          $TagVersion = "${{ github.ref_name }}".Substring(1)
          & "C:\Program Files (x86)\Inno Setup 6\ISCC.exe" "/DMyAppVersion=$TagVersion" "win_installer.iss"

      # 2. FIRMA DELL'INSTALLER GENERATO DA INNO SETUP
      - name: Firma Installer
        run: |
          $Signtool = (Get-ChildItem "C:\Program Files (x86)\Windows Kits\10\bin\*\x64\signtool.exe" | Sort-Object FullName -Descending | Select-Object -First 1).FullName
          
          & $Signtool sign /f "C:\flux_privato.pfx" /p "${{ secrets.PFX_PASSWORD }}" /fd SHA256 "build\windows\installer\FluxInstaller.exe"

      # Nel dubbio usiamo l'action per caricare l'asset
      - name: Upload Windows Asset
        uses: https://gitea.com/actions/release-action@main
        with:
          files: "build/windows/installer/FluxInstaller.exe"
          api_key: ${{ secrets.MYRELEASE_TOKEN }}

      - name: Pulisci Workspace Windows
        if: always()
        run: Remove-Item -Recurse -Force ./*

  # -----------------------------------------------------------------
  # JOB 2: ANDROID APK (Gira sul tuo MacBook)
  # -----------------------------------------------------------------
  build-android:
    runs-on: macos-runner # <--- Etichetta del tuo Mac
    steps:
      - name: Checkout del codice
        uses: actions/checkout@v3

      # Logica Bash per Mac: usiamo le virgolette singole forti per evitare escape strani
      - name: Crea file .env
        run: |
          cat << 'EOF' > .env
          ${{ secrets.ENV_FILE_CONTENT }}
          EOF

      - name: Build Flutter APK
        run: flutter build apk --release

      # Carichiamo l'APK universale o quelli splittati nelle release di Gitea
      - name: Upload Android Asset
        uses: https://gitea.com/actions/release-action@main
        with:
          files: "build/app/outputs/flutter-apk/app-release.apk"
          api_key: ${{ secrets.MYRELEASE_TOKEN }}

      - name: Aggiorna Link Android su Supabase
        run: |
          curl -X PATCH "https://pvqpjloswwvtfoxbkfbh.supabase.co/rest/v1/app_config?id=eq.49f18b19-2129-46c0-b690-a97db725b5a8" -H "apikey: ${{ secrets.SUPABASE_SERVICE_KEY }}" -H "Authorization: Bearer ${{ secrets.SUPABASE_SERVICE_KEY }}" -H "Content-Type: application/json" -d "{\"download_url\": \"https://gitea.catelli.it/brontomark/flux/releases/download/${{ github.ref_name }}/app-release.apk\"}"

      - name: Aggiorna Link Windows su Supabase
        run: |
          curl -X PATCH "https://pvqpjloswwvtfoxbkfbh.supabase.co/rest/v1/app_config?id=eq.1f888b30-5cbf-4a16-820c-5036a3af0cf8" -H "apikey: ${{ secrets.SUPABASE_SERVICE_KEY }}" -H "Authorization: Bearer ${{ secrets.SUPABASE_SERVICE_KEY }}" -H "Content-Type: application/json" -d "{\"download_url\": \"https://gitea.catelli.it/brontomark/flux/releases/download/${{ github.ref_name }}/FluxInstaller.exe\"}"


  # -----------------------------------------------------------------
  # JOB 3: WEB & CLOUDFLARE DEPLOY (Gira sul tuo MacBook)
  # -----------------------------------------------------------------
  build-web:
    runs-on: macos-runner # <--- Etichetta del tuo Mac
    steps:
      - name: Checkout del codice
        uses: actions/checkout@v3

      - name: Crea file .env
        run: |
          cat << 'EOF' > .env
          ${{ secrets.ENV_FILE_CONTENT }}
          EOF

      - name: Build Flutter Web
        run: flutter build web --release

      # Sfruttiamo npx (incluso in Node.js) per lanciare wrangler al volo senza installarlo globalmente
      # Sto assumendo che usi Cloudflare Pages che è perfetto per Flutter Web statico
      - name: Deploy su Cloudflare Pages
        env:
          CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
        run: |
          npx wrangler pages deploy build/web --project-name="flux" --branch="main"