name: Release

on:
  push:
    tags:
      - 'v*'
  workflow_dispatch:
    inputs:
      version:
        description: '版本号 (例如 1.5.16)'
        required: true
        type: string
      platform:
        description: '构建平台'
        required: true
        type: choice
        options:
          - all
          - macos
          - windows
        default: all

jobs:
  build-macos:
    name: Build macOS (Universal)
    if: ${{ github.event_name == 'push' || inputs.platform == 'all' || inputs.platform == 'macos' }}
    runs-on: macos-latest
    permissions:
      contents: write
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: 22

      - name: Setup Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: x86_64-apple-darwin,aarch64-apple-darwin

      - name: Cache Rust dependencies
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            tauri-app/src-tauri/target
          key: ${{ runner.os }}-cargo-${{ hashFiles('tauri-app/src-tauri/Cargo.lock') }}

      - name: Cache Node dependencies
        uses: actions/cache@v4
        with:
          path: tauri-app/node_modules
          key: ${{ runner.os }}-node-${{ hashFiles('tauri-app/package-lock.json') }}

      - name: Download sidecar binaries
        run: |
          mkdir -p tauri-app/src-tauri/binaries
          gh release download v0.0.0-sidecar --repo ${{ github.repository }} --pattern "sidecar-binaries.tar.gz" --dir /tmp
          tar xzf /tmp/sidecar-binaries.tar.gz -C tauri-app/src-tauri/binaries/
          chmod +x tauri-app/src-tauri/binaries/ffmpeg-* tauri-app/src-tauri/binaries/ffprobe-*
          # Create universal binary for macOS universal-apple-darwin target
          # by combining aarch64 and x86_64 binaries with lipo
          if [ -f tauri-app/src-tauri/binaries/ffmpeg-aarch64-apple-darwin ] && [ -f tauri-app/src-tauri/binaries/ffmpeg-x86_64-apple-darwin ]; then
            lipo -create \
              tauri-app/src-tauri/binaries/ffmpeg-aarch64-apple-darwin \
              tauri-app/src-tauri/binaries/ffmpeg-x86_64-apple-darwin \
              -output tauri-app/src-tauri/binaries/ffmpeg-universal-apple-darwin
            lipo -create \
              tauri-app/src-tauri/binaries/ffprobe-aarch64-apple-darwin \
              tauri-app/src-tauri/binaries/ffprobe-x86_64-apple-darwin \
              -output tauri-app/src-tauri/binaries/ffprobe-universal-apple-darwin
          fi
        env:
          GH_TOKEN: ${{ github.token }}

      - name: Install dependencies
        working-directory: tauri-app
        run: npm ci

      - name: Update version
        run: |
          if [ -n "${{ inputs.version }}" ]; then
            VERSION="${{ inputs.version }}"
          else
            VERSION="${GITHUB_REF_NAME#v}"
          fi
          perl -pi -e "s/\"version\"\s*:\s*\"[^\"]*\"/\"version\": \"$VERSION\"/" tauri-app/src-tauri/tauri.conf.json
          perl -pi -e "s/^version = \"[^\"]*\"/version = \"$VERSION\"/" tauri-app/src-tauri/Cargo.toml
          echo "Version updated to: $VERSION"

      - name: Build macOS Universal
        working-directory: tauri-app
        run: npm run tauri -- build --target universal-apple-darwin
        env:
          VITE_API_BASE_URL: https://dev.tapi.meijiaka.cn/api/v1
          TAURI_SIGNING_PRIVATE_KEY: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY }}
          TAURI_SIGNING_PRIVATE_KEY_PASSWORD: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY_PASSWORD }}

      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: macos-universal
          path: |
            tauri-app/src-tauri/target/universal-apple-darwin/release/bundle/dmg/*.dmg
            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
          retention-days: 3

      - name: Upload to GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: ${{ github.ref_name }}
          files: |
            tauri-app/src-tauri/target/universal-apple-darwin/release/bundle/dmg/*.dmg
            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

  build-windows:
    name: Build Windows (x64)
    if: ${{ github.event_name == 'push' || inputs.platform == 'all' || inputs.platform == 'windows' }}
    runs-on: windows-latest
    permissions:
      contents: write
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: 22

      - name: Setup Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: x86_64-pc-windows-msvc

      - name: Cache Rust dependencies
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            tauri-app/src-tauri/target
          key: ${{ runner.os }}-cargo-${{ hashFiles('tauri-app/src-tauri/Cargo.lock') }}

      - name: Cache Node dependencies
        uses: actions/cache@v4
        with:
          path: tauri-app/node_modules
          key: ${{ runner.os }}-node-${{ hashFiles('tauri-app/package-lock.json') }}

      - name: Download sidecar binaries
        shell: pwsh
        run: |
          New-Item -ItemType Directory -Force -Path tauri-app/src-tauri/binaries
          gh release download v0.0.0-sidecar --repo ${{ github.repository }} --pattern "sidecar-binaries.tar.gz" --dir $env:TEMP
          tar xzf "$env:TEMP\sidecar-binaries.tar.gz" -C tauri-app/src-tauri/binaries/
          Get-ChildItem tauri-app/src-tauri/binaries/
        env:
          GH_TOKEN: ${{ github.token }}

      - name: Install dependencies
        working-directory: tauri-app
        run: npm ci

      - name: Update version
        shell: pwsh
        run: |
          $version = if ("${{ inputs.version }}") { "${{ inputs.version }}" } else { $env:GITHUB_REF_NAME -replace '^v', '' }
          $content = Get-Content tauri-app/src-tauri/tauri.conf.json -Raw
          $content = $content -replace '"version"\s*:\s*"[^"]*"', "`"version`": `"$version`""
          Set-Content tauri-app/src-tauri/tauri.conf.json -Value $content
          $cargo = Get-Content tauri-app/src-tauri/Cargo.toml -Raw
          $cargo = $cargo -replace '^version = "[^"]*"', "version = `"$version`""
          Set-Content tauri-app/src-tauri/Cargo.toml -Value $cargo
          Write-Host "Version updated to: $version"

      - name: Build Windows x64
        working-directory: tauri-app
        shell: pwsh
        run: npm run tauri -- build --target x86_64-pc-windows-msvc
        env:
          VITE_API_BASE_URL: https://dev.tapi.meijiaka.cn/api/v1
          TAURI_SIGNING_PRIVATE_KEY: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY }}
          TAURI_SIGNING_PRIVATE_KEY_PASSWORD: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY_PASSWORD }}

      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: windows-x64
          path: |
            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
            tauri-app/src-tauri/target/x86_64-pc-windows-msvc/release/bundle/nsis/*-setup.exe
          retention-days: 3

      - name: Upload to GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: ${{ github.ref_name }}
          files: |
            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
            tauri-app/src-tauri/target/x86_64-pc-windows-msvc/release/bundle/nsis/*-setup.exe

