# 美家卡智影 - GitLab CI/CD 配置
# =======================================
# 覆盖范围: 前端多平台构建 (macOS Universal + Windows x64)
#
# Runner 环境要求:
#   - macOS runner (标签: macos, arm64):
#       Apple Silicon Mac, 已安装:
#         - Xcode Command Line Tools (15+)
#         - Node.js 22+ (建议通过 nvm 管理)
#         - Rust 1.94+ (通过 rustup)
#         - GitLab Runner (shell executor)
#   - Windows runner (标签: windows, x86_64):
#       Windows 10/11 x64, 已安装:
#         - Visual Studio 2022 Build Tools (含 C++ 桌面开发工具链)
#         - Node.js 22+ (建议通过 nvm-windows 管理)
#         - Rust 1.94+ (通过 rustup)
#         - GitLab Runner (shell executor)
#
# 触发条件: master 分支推送 或 tag 推送
# 产物保留: 30 天

variables:
  ARTIFACT_EXPIRE_DAYS: "30"

stages:
  - build-frontend

# ==========================================
# 通用模板: 前端构建
# ==========================================
.frontend_build:
  stage: build-frontend
  only:
    - master
    - tags
  cache:
    # Node 依赖缓存（基于 package-lock.json 变更）
    - key:
        files:
          - tauri-app/package-lock.json
      paths:
        - tauri-app/node_modules/
    # Rust 编译缓存（基于 Cargo.lock 变更）
    - key:
        files:
          - tauri-app/src-tauri/Cargo.lock
      paths:
        - tauri-app/src-tauri/target/

# ==========================================
# Job: macOS Universal 构建 (ARM64 + Intel)
# ==========================================
# 说明:
#   - 在 Apple Silicon Mac 上同时编译 ARM64 和 x86_64 两个架构
#   - 使用 lipo 合并为 universal Mach-O 可执行文件
#   - 产物为单一 .dmg，同时支持 M 系列和 Intel Mac
build-frontend-macos:
  extends: .frontend_build
  tags:
    - macos
    - arm64
  before_script:
    # 激活 Rust 环境 (rustup 默认安装路径)
    - source "$HOME/.cargo/env" 2>/dev/null || true
    # 安装 Intel 目标平台（构建 universal binary 必需）
    - rustup target add x86_64-apple-darwin
    # 验证构建环境
    - node --version
    - npm --version
    - cargo --version
    - rustc --print host
  script:
    - cd tauri-app
    - npm ci
    # 构建 universal macOS 应用
    # Tauri 会自动分别编译 aarch64 和 x86_64，再合并为 universal binary
    - npm run tauri -- build --target universal-apple-darwin
  artifacts:
    name: "meijiaka-macos-${CI_COMMIT_REF_NAME}-${CI_COMMIT_SHORT_SHA}"
    paths:
      # DMG 安装包 (推荐用户下载)
      - tauri-app/src-tauri/target/universal-apple-darwin/release/bundle/dmg/*.dmg
      # Updater 专用包 + 签名
      - tauri-app/src-tauri/target/universal-apple-darwin/release/bundle/macos/*.app.tar.gz
      - tauri-app/src-tauri/target/universal-apple-darwin/release/bundle/macos/*.app.tar.gz.sig
    expire_in: "${ARTIFACT_EXPIRE_DAYS} days"
  timeout: 45 minutes
  retry:
    max: 1
    when: runner_system_failure

# ==========================================
# Job: Windows x64 构建
# ==========================================
# 说明:
#   - 产物包含 NSIS (.exe) 和 MSI 两种安装包格式
#   - NSIS 已配置为简体中文安装界面
#   - sidecar (ffmpeg/ffprobe) 会自动嵌入 .exe 同目录
build-frontend-windows:
  extends: .frontend_build
  tags:
    - windows
    - x86_64
  before_script:
    # 验证构建环境
    - rustc --version
    - cargo --version
    - node --version
    - npm --version
  script:
    - cd tauri-app
    - npm ci
    # 构建 Windows x64 应用
    - npm run tauri -- build --target x86_64-pc-windows-msvc
  artifacts:
    name: "meijiaka-windows-${CI_COMMIT_REF_NAME}-${CI_COMMIT_SHORT_SHA}"
    paths:
      # Updater 专用包 + 签名
      - tauri-app/src-tauri/target/x86_64-pc-windows-msvc/release/bundle/nsis/*.exe
      - tauri-app/src-tauri/target/x86_64-pc-windows-msvc/release/bundle/nsis/*.exe.sig
      # NSIS 安装包 (推荐用户下载)
      - tauri-app/src-tauri/target/x86_64-pc-windows-msvc/release/bundle/nsis/*-setup.exe
      # MSI 安装包 (企业部署场景)
      - tauri-app/src-tauri/target/x86_64-pc-windows-msvc/release/bundle/msi/*.msi
    expire_in: "${ARTIFACT_EXPIRE_DAYS} days"
  timeout: 45 minutes
  retry:
    max: 1
    when: runner_system_failure
