name: Release

on:
  push:
    tags:
      - 'v*'
  workflow_dispatch:
    inputs:
      tag:
        description: 'Release tag (e.g., v1.0.0)'
        required: true
        default: 'v0.1.0'

permissions:
  contents: write
  actions: read

env:
  CARGO_TERM_COLOR: always
  OPENSSL_STATIC: true
  OPENSSL_VENDOR: true

jobs:
  build:
    name: Build ${{ matrix.target }}
    runs-on: ${{ matrix.os }}
    timeout-minutes: 60
    strategy:
      matrix:
        include:
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
            artifact_name: ailog
            asset_name: ailog-linux-x86_64
          - target: aarch64-unknown-linux-gnu
            os: ubuntu-latest
            artifact_name: ailog
            asset_name: ailog-linux-aarch64
          - target: x86_64-apple-darwin
            os: macos-latest
            artifact_name: ailog
            asset_name: ailog-macos-x86_64
          - target: aarch64-apple-darwin
            os: macos-latest
            artifact_name: ailog
            asset_name: ailog-macos-aarch64

    steps:
    - uses: actions/checkout@v4

    - name: Setup Rust
      uses: dtolnay/rust-toolchain@stable
      with:
        targets: ${{ matrix.target }}

    - name: Install cross-compilation tools (Linux)
      if: matrix.os == 'ubuntu-latest' && matrix.target == 'aarch64-unknown-linux-gnu'
      run: |
        sudo apt-get update
        sudo apt-get install -y gcc-aarch64-linux-gnu binutils-aarch64-linux-gnu

    - name: Configure cross-compilation (Linux ARM64)
      if: matrix.target == 'aarch64-unknown-linux-gnu'
      run: |
        echo '[target.aarch64-unknown-linux-gnu]' >> ~/.cargo/config.toml
        echo 'linker = "aarch64-linux-gnu-gcc"' >> ~/.cargo/config.toml

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

    - name: Cache target directory
      uses: actions/cache@v4
      with:
        path: target
        key: ${{ runner.os }}-${{ matrix.target }}-target-${{ hashFiles('**/Cargo.lock') }}

    - name: Build
      run: cargo build --release --target ${{ matrix.target }}

    - name: Prepare binary
      shell: bash
      run: |
        cd target/${{ matrix.target }}/release
        
        # Use appropriate strip command for cross-compilation
        if [[ "${{ matrix.target }}" == "aarch64-unknown-linux-gnu" ]]; then
          aarch64-linux-gnu-strip ${{ matrix.artifact_name }} || echo "Strip failed, continuing..."
        elif [[ "${{ matrix.os }}" == "windows-latest" ]]; then
          strip ${{ matrix.artifact_name }} || echo "Strip failed, continuing..."
        else
          strip ${{ matrix.artifact_name }} || echo "Strip failed, continuing..."
        fi
        
        # Create archive
        if [[ "${{ matrix.target }}" == *"windows"* ]]; then
          7z a ../../../${{ matrix.asset_name }}.zip ${{ matrix.artifact_name }}
        else
          tar czvf ../../../${{ matrix.asset_name }}.tar.gz ${{ matrix.artifact_name }}
        fi

    - name: Upload binary
      uses: actions/upload-artifact@v4
      with:
        name: ${{ matrix.asset_name }}
        path: ${{ matrix.asset_name }}.tar.gz

  release:
    name: Create Release
    needs: build
    runs-on: ubuntu-latest
    permissions:
      contents: write
      actions: read
    steps:
    - uses: actions/checkout@v4

    - name: Download all artifacts
      uses: actions/download-artifact@v4
      with:
        path: artifacts

    - name: Generate release notes
      run: |
        echo "## What's Changed" > release_notes.md
        echo "" >> release_notes.md
        echo "### Features" >> release_notes.md
        echo "- AI-powered static blog generator" >> release_notes.md
        echo "- AtProto OAuth integration" >> release_notes.md
        echo "- Automatic translation support" >> release_notes.md
        echo "- AI comment system" >> release_notes.md
        echo "" >> release_notes.md
        echo "### Platforms" >> release_notes.md
        echo "- Linux (x86_64, aarch64)" >> release_notes.md
        echo "- macOS (Intel, Apple Silicon)" >> release_notes.md
        echo "" >> release_notes.md
        echo "### Installation" >> release_notes.md
        echo "\`\`\`bash" >> release_notes.md
        echo "# Linux/macOS" >> release_notes.md
        echo "tar -xzf ailog-linux-x86_64.tar.gz" >> release_notes.md
        echo "chmod +x ailog" >> release_notes.md
        echo "sudo mv ailog /usr/local/bin/" >> release_notes.md
        echo "" >> release_notes.md
        echo "\`\`\`" >> release_notes.md

    - name: Get tag name
      id: tag_name
      run: |
        if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
          echo "tag=${{ github.event.inputs.tag }}" >> $GITHUB_OUTPUT
        else
          echo "tag=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
        fi

    - name: Create Release
      uses: softprops/action-gh-release@v1
      with:
        tag_name: ${{ steps.tag_name.outputs.tag }}
        name: ailog ${{ steps.tag_name.outputs.tag }}
        body_path: release_notes.md
        draft: false
        prerelease: ${{ contains(steps.tag_name.outputs.tag, 'alpha') || contains(steps.tag_name.outputs.tag, 'beta') || contains(steps.tag_name.outputs.tag, 'rc') }}
        files: artifacts/*/ailog-*.tar.gz
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}