name: Flutter CI/CD — PluriWave

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  flutter-ci:
    name: Test + Build
    #runs-on: macos-14
    runs-on: [self-hosted, macos, arm64, flutter]
    env:
      ANDROID_HOME: /Users/freetlab/Library/Android/sdk

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Flutter pub get
        run: flutter pub get

      - name: Run tests
        run: flutter test

      - name: Build APK (release)
        run: flutter build apk --release

      - name: Build AppBundle (release)
        run: flutter build appbundle --release

      - name: Upload APK artifact
        uses: actions/upload-artifact@v4
        with:
          name: pluriwave-apk-${{ gitea.sha }}
          path: build/app/outputs/apk/release/app-release.apk
          if-no-files-found: error

      - name: Upload AppBundle artifact
        uses: actions/upload-artifact@v4
        with:
          name: pluriwave-aab-${{ gitea.sha }}
          path: build/app/outputs/bundle/release/app-release.aab
          if-no-files-found: error

      - name: Notify Telegram — éxito
        if: success()
        run: |
          curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
            -d chat_id="${TELEGRAM_CHAT_ID}" \
            -d parse_mode="HTML" \
            -d text="✅ <b>PluriWave CI OK</b>%0ABranch: <code>${{ gitea.ref_name }}</code>%0ACommit: <code>${{ gitea.sha }}</code>%0AAPKs subidos como artifacts en Gitea."
        env:
          TELEGRAM_BOT_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }}
          TELEGRAM_CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }}

      - name: Notify Telegram — fallo
        if: failure()
        run: |
          curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
            -d chat_id="${TELEGRAM_CHAT_ID}" \
            -d parse_mode="HTML" \
            -d text="❌ <b>PluriWave CI FALLÓ</b>%0ABranch: <code>${{ gitea.ref_name }}</code>%0ACommit: <code>${{ gitea.sha }}</code>%0ARevisa el log en Gitea."
        env:
          TELEGRAM_BOT_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }}
          TELEGRAM_CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }}
