name: Release Bichon

on:
  push:
    tags:
      - '[0-9]+.[0-9]+.[0-9]+'
env:
  BINARY_NAME: bichon
  BINARY_CTL: bichonctl

permissions:
  contents: write

jobs:
  build:
    name: Build ${{ matrix.target }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
          - target: x86_64-unknown-linux-musl
            os: ubuntu-latest
          - target: x86_64-apple-darwin
            os: macos-latest
          - target: x86_64-pc-windows-msvc
            os: windows-latest
          - target: aarch64-unknown-linux-gnu
            os: ubuntu-latest
          - target: aarch64-apple-darwin
            os: macos-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Verify Cargo.toml version matches git tag
        shell: bash
        run: |
          TAG_VERSION="${GITHUB_REF_NAME}"

          CARGO_VERSION=$(grep '^version' Cargo.toml | head -n1 | cut -d '"' -f2)

          echo "Git tag version:    $TAG_VERSION"
          echo "Cargo.toml version: $CARGO_VERSION"

          if [ "$TAG_VERSION" != "$CARGO_VERSION" ]; then
            echo "::error::Version mismatch! Git tag ($TAG_VERSION) does not match Cargo.toml version ($CARGO_VERSION)"
            exit 1
          fi


      - name: Install Rust
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          target: ${{ matrix.target }}
          override: true

      - name: Setup Node.js & pnpm
        uses: actions/setup-node@v4
        with:
          node-version: 20

      - name: Install pnpm
        run: npm install -g pnpm

      - name: Build frontend (web)
        working-directory: ./web
        run: |
          pnpm install
          pnpm run build

      - name: Install musl-tools (Linux musl only)
        if: matrix.target == 'x86_64-unknown-linux-musl'
        run: sudo apt-get update && sudo apt-get install -y musl-tools

      - name: Build aarch64 Rust backend
        if: matrix.target == 'aarch64-unknown-linux-gnu'
        run: |
          cargo install cross --force
          cross build --release --features vendored-openssl --target=${{ matrix.target }}

      - name: Build Rust backend
        if: matrix.target != 'aarch64-unknown-linux-gnu'
        run: |
          cargo build --release --features vendored-openssl --target=${{ matrix.target }}  

      - name: Strip binary (Linux and macOS)
        if: matrix.os != 'windows-latest' && matrix.target != 'aarch64-unknown-linux-gnu'
        run: |
          strip target/${{ matrix.target }}/release/${{ env.BINARY_NAME }}
          strip target/${{ matrix.target }}/release/${{ env.BINARY_CTL }}

      - name: Pack artifact (Linux/macOS)
        if: matrix.os != 'windows-latest'
        shell: bash
        run: |
          mkdir -p release
          BINARY="target/${{ matrix.target }}/release/${{ env.BINARY_NAME }}"
          cp README.md LICENSE release/
          cp target/${{ matrix.target }}/release/${{ env.BINARY_NAME }} release/
          cp target/${{ matrix.target }}/release/${{ env.BINARY_CTL }} release/
          tar -czvf "${{ env.BINARY_NAME }}-${{ github.ref_name }}-${{ matrix.target }}.tar.gz" -C release .
          mv "${{ env.BINARY_NAME }}-${{ github.ref_name }}-${{ matrix.target }}.tar.gz" release/

      - name: Upload build artifact
        if: matrix.os != 'windows-latest'
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.target }}
          path: release/${{ env.BINARY_NAME }}-${{ github.ref_name }}-${{ matrix.target }}.tar.gz
      
      - name: Create zip archive (Windows)
        if: matrix.os == 'windows-latest'
        shell: pwsh
        run: |
          mkdir -p release

          Copy-Item "target/${{ matrix.target }}/release/${{ env.BINARY_NAME }}.exe" release/
          Copy-Item "target/${{ matrix.target }}/release/${{ env.BINARY_CTL }}.exe" release/

          Copy-Item -Path README.md -Destination release/
          Copy-Item -Path LICENSE -Destination release/
          
          Compress-Archive -Path release\* -DestinationPath "release/${{ env.BINARY_NAME }}-${{ github.ref_name }}-${{ matrix.target }}.zip" -Force

      - name: Upload build artifact
        if: matrix.os == 'windows-latest'
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.target }}
          path: release/${{ env.BINARY_NAME }}-${{ github.ref_name }}-${{ matrix.target }}.zip

  release:
    name: Create GitHub Release
    needs: build
    runs-on: ubuntu-latest

    steps:
      - name: Download build artifacts
        uses: actions/download-artifact@v4
        with:
          path: artifacts
          pattern: "*"
          merge-multiple: true
      
      - name: Flatten artifacts
        run: |
          mkdir -p artifacts
          find artifacts -type f -name "*.zip" -exec mv {} artifacts/ \;
          find artifacts -type f -name "*.tar.gz" -exec mv {} artifacts/ \;
      
      - name: Debug list files
        run: ls -R artifacts

      - name: Generate SHA256 checksums
        run: |
            cd artifacts
            sha256sum * > SHA256SUMS.txt
            cat SHA256SUMS.txt

      - name: Publish GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          name: "Release ${{ github.ref_name }}"
          generate_release_notes: true
          files: |
            artifacts/*
  docker:
    name: Build and Push Docker Image
    needs: [build, release]
    runs-on: ubuntu-latest
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        
      - name: Download Linux x86_64 artifact
        uses: actions/download-artifact@v4
        with:
          name: x86_64-unknown-linux-gnu
          path: artifacts

      - name: Download Linux aarch64 artifact
        uses: actions/download-artifact@v4
        with:
          name: aarch64-unknown-linux-gnu
          path: artifacts
          
      - name: Extract amd64 binary
        run: |
          mkdir -p docker/amd64
          AMD64_FILE=$(ls artifacts | grep x86_64-unknown-linux-gnu.tar.gz)
          echo "Extracting $AMD64_FILE"
          tar -xzf artifacts/$AMD64_FILE -C docker/amd64

      - name: Extract arm64 binary
        run: |
          mkdir -p docker/arm64
          ARM64_FILE=$(ls artifacts | grep aarch64-unknown-linux-gnu.tar.gz)
          echo "Extracting $ARM64_FILE"
          tar -xzf artifacts/$ARM64_FILE -C docker/arm64
          
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
        
      - name: Login to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
          
      - name: Build and push Docker image
        uses: docker/build-push-action@v5
        with:
          context: ./docker
          push: true
          platforms: linux/amd64,linux/arm64
          tags: |
            rustmailer/bichon:${{ github.ref_name }}
            rustmailer/bichon:latest
          build-args: |
            CRATE_VERSION=${{ github.ref_name }}
        