name: Release Build

on:
  push:
    tags:
      - 'v*'
  workflow_dispatch:

env:
  CARGO_TERM_COLOR: always
  RUST_BACKTRACE: 1

jobs:
  build:
    name: Build Release
    strategy:
      matrix:
        include:
          - os: macos-latest
            target: x86_64-apple-darwin
            name: macOS-Intel
            archive: readflow-${{ github.ref_name }}-macos-x86_64.zip
            
          - os: macos-latest
            target: aarch64-apple-darwin
            name: macOS-Apple-Silicon
            archive: readflow-${{ github.ref_name }}-macos-aarch64.zip
            
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            name: Linux
            archive: readflow-${{ github.ref_name }}-linux-x86_64.tar.gz
            
          - os: ubuntu-latest
            target: x86_64-unknown-linux-musl
            name: Linux-MUSL
            archive: readflow-${{ github.ref_name }}-linux-x86_64-musl.tar.gz
            
          - os: windows-latest
            target: x86_64-pc-windows-msvc
            name: Windows
            archive: readflow-${{ github.ref_name }}-windows-x86_64.zip

    runs-on: ${{ matrix.os }}
    
    steps:
    - name: Checkout code
      uses: actions/checkout@v4
      
    - name: Setup Rust toolchain
      uses: actions-rs/toolchain@v1
      with:
        target: ${{ matrix.target }}
        profile: minimal
        components: rustfmt, clippy
      
    - name: Cache cargo registry
      uses: actions/cache@v4
      with:
        path: ~/.cargo/registry
        key: ${{ runner.os }}-cargo-registry-${{ matrix.target }}-${{ hashFiles('**/Cargo.lock') }}
      
    - name: Cache cargo index
      uses: actions/cache@v4
      with:
        path: ~/.cargo/git
        key: ${{ runner.os }}-cargo-index-${{ matrix.target }}-${{ hashFiles('**/Cargo.lock') }}
      
    - name: Build release
      run: cargo build --release
      
    - name: Run tests
      run: cargo test
      
    - name: Package macOS
      if: matrix.name == 'macOS-Intel' || matrix.name == 'macOS-Apple-Silicon'
      run: |
        mkdir -p release
        cp target/release/readflow release/
        chmod +x release/readflow
        # Create Info.plist
        cat > release/Info.plist << EOF
        <?xml version="1.0" encoding="UTF-8"?>
        <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
        <plist version="1.0">
        <dict>
          <key>CFBundleExecutable</key>
          <string>readflow</string>
          <key>CFBundleIdentifier</key>
          <string>com.readflow.app</string>
          <key>CFBundleName</key>
          <string>ReadFlow</string>
          <key>CFBundleDisplayName</key>
          <string>ReadFlow</string>
          <key>CFBundleShortVersionString</key>
          <string>${{ github.ref_name }}</string>
          <key>CFBundleVersion</key>
          <string>${{ github.ref_name }}</string>
          <key>CFBundlePackageType</key>
          <string>APPL</string>
          <key>NSHighResolutionCapable</key>
          <true/>
        </dict>
        </plist>
        EOF
        cd release
        mkdir -p ReadFlow.app/Contents/MacOS
        mkdir -p ReadFlow.app/Contents/Resources
        mv readflow ReadFlow.app/Contents/MacOS/
        mv Info.plist ReadFlow.app/Contents/
        cp -r ../assets ReadFlow.app/Contents/Resources/ || true
        cd ..
        zip -r -X ${{ matrix.archive }} ReadFlow.app
        
    - name: Package Linux
      if: matrix.name == 'Linux' || matrix.name == 'Linux-MUSL'
      run: |
        mkdir -p release
        cp target/release/readflow release/readflow
        chmod +x release/readflow
        # Create .desktop file
        cat > release/readflow.desktop << EOF
        [Desktop Entry]
        Name=ReadFlow
        Comment=ReadFlow - 面向开发者和知识工作者的阅读工具
        Exec=/usr/local/bin/readflow
        Icon=readflow
        Terminal=false
        Type=Application
        Categories=Utility;Reading;
        EOF
        cd release
        if [ "${{ matrix.name }}" == "Linux-MUSL" ]; then
          tar -czf ${{ matrix.archive }} readflow
        else
          tar -czf ${{ matrix.archive }} readflow readflow.desktop
        fi
        
    - name: Package Windows
      if: matrix.name == 'Windows'
      run: |
        mkdir -p release
        cp target/release/readflow.exe release/
        # Create NSIS installer (simplified)
        # For now, just create a simple wrapper script
        cat > release/install.bat << 'EOF'
        @echo off
        echo Installing ReadFlow...
        copy readflow.exe %LOCALAPPDATA%\readflow\
        echo Installation complete!
        EOF
        cd release
        powershell Compress-Archive -Path ${{ matrix.archive }} -DestinationPath . readflow.exe install.bat
      
    - name: Upload Release Asset
      uses: softprops/action-gh-release@v2
      with:
        name: ${{ matrix.name }}
        files: |
          ${{ matrix.archive }}
        body: |
          ## 🎉 ReadFlow ${{ github.ref_name }} for ${{ matrix.name }}
          
          ### 下载
          - 下方附件中的安装包
          
          ### 快速开始
          - 解压后运行 readflow 即可
          
          ### 功能
          - ✅ EPUB/MOBI/Markdown/PDF 阅读
          - ✅ 20+ 编程语言语法高亮
          - ✅ 双语翻译对照
          - ✅ 笔记与书签系统
          - ✅ 插件系统
          - ✅ 主题商店
          
          ---
          
          **下载附件**: ${{ matrix.name }}
        generate_release_notes: true
        tag_name: ${{ github.ref_name }}
        token: ${{ secrets.GITHUB_TOKEN }}
        fail_on_unmatched_files: true
