name: CI

on:
  push:
    branches: [main]
    tags: ["v*"]
  pull_request:
    branches: [main]
  workflow_dispatch:

# This pipeline targets a self-hosted Windows runner inside mainland China.
# Notes on the design (so it works behind the GFW):
#   * Actions are fetched from the gitea.com mirror, NOT github.com, which is
#     unreachable here. (Alternatively set DEFAULT_ACTIONS_URL=https://gitea.com
#     in the runner's config and drop the full URL prefix.)
#   * Flutter is expected to be pre-installed on the runner (the same machine
#     used for local development) — we do NOT download the SDK from Google.
#   * pub / Flutter artifacts use the flutter-io.cn mirrors.
#   * The sqlite3 native binary is vendored in the repo (vendor/sqlite3/), so no
#     GitHub-releases download happens during the build.
env:
  PUB_HOSTED_URL: https://pub.flutter-io.cn
  FLUTTER_STORAGE_BASE_URL: https://storage.flutter-io.cn

jobs:
  flutter:
    name: Flutter (analyze, test, Windows build)
    runs-on: windows-latest
    steps:
      - name: Checkout
        uses: https://gitea.com/actions/checkout@v4

      - name: Flutter version (must be pre-installed on the runner)
        run: flutter --version

      - name: Install dependencies
        run: flutter pub get

      - name: Verify formatting
        run: dart format --output=none --set-exit-if-changed lib test

      - name: Static analysis
        run: flutter analyze

      - name: Run tests
        run: flutter test --reporter expanded

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

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

      - name: Package artifact
        run: Compress-Archive -Path "build/windows/x64/runner/Release/*" -DestinationPath "badnote-windows-x64.zip" -Force

      - name: Upload artifact
        uses: https://gitea.com/actions/upload-artifact@v3
        with:
          name: badnote-windows-x64
          path: badnote-windows-x64.zip
          if-no-files-found: error

  server:
    name: Server tests (optional)
    runs-on: windows-latest
    # The Python backend is optional/experimental; never block the pipeline.
    continue-on-error: true
    steps:
      - name: Checkout
        uses: https://gitea.com/actions/checkout@v4

      - name: Install dependencies (Tsinghua PyPI mirror)
        working-directory: server
        run: |
          python -m pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple
          pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple

      - name: Run tests
        working-directory: server
        env:
          BADNOTE_JWT_SECRET: ci-test-secret
        run: pytest -q
