name: Release

on:
  workflow_dispatch:

env:
  VERSION: 0.5.0

jobs:
  build:
    name: Build
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: windows-latest
            target: x86_64-pc-windows-msvc
          - os: windows-11-arm
            target: aarch64-pc-windows-msvc
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
          - os: ubuntu-24.04-arm
            target: aarch64-unknown-linux-gnu
          - os: macos-latest
            target: aarch64-apple-darwin
          - os: macos-15-intel
            target: x86_64-apple-darwin

    steps:
      - uses: actions/checkout@v6
      - name: Update Version
        shell: pwsh
        run: |
          $version = (Get-Content -Path Cargo.toml -Raw) -replace 'version = "0.1.0"', 'version = "${{ env.VERSION }}"'
          $version | Set-Content -Path Cargo.toml
      - name: Install Rust
        uses: christianhelle/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}
      - name: Rust cache
        uses: christianhelle/rust-cache@v2
      - name: Build
        run: cargo build --release
      - name: Zip artifacts
        shell: pwsh
        run: |
          mv target/release/azdocli* ./
          Compress-Archive azdocli*, README.md, LICENSE ${{ matrix.target }}.zip
      - name: Upload artifacts
        uses: actions/upload-artifact@v7
        with:
          name: ${{ matrix.target }}
          path: ${{ matrix.target }}.zip

  release:
    name: Release
    runs-on: ubuntu-latest
    needs: build
    steps:
      - uses: actions/checkout@v6
      - name: Create tag
        uses: actions/github-script@v9
        with:
          github-token: ${{ github.token }}
          script: |
            github.rest.git.createRef({
              owner: context.repo.owner,
              repo: context.repo.repo,
              ref: "refs/tags/${{ env.VERSION }}",
              sha: context.sha
            })
      - name: Download artifacts
        uses: actions/download-artifact@v8
        with:
          path: artifacts
      - name: Create release
        id: create-release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ env.VERSION }}
          release_name: v${{ env.VERSION }}
          draft: true
          prerelease: false
      - name: Upload Windows x64 asset
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create-release.outputs.upload_url }}
          asset_path: artifacts/x86_64-pc-windows-msvc/x86_64-pc-windows-msvc.zip
          asset_name: windows-x64.zip
          asset_content_type: application/zip
      - name: Upload Windows ARM64 asset
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create-release.outputs.upload_url }}
          asset_path: artifacts/aarch64-pc-windows-msvc/aarch64-pc-windows-msvc.zip
          asset_name: windows-arm64.zip
          asset_content_type: application/zip
      - name: Upload MacOS ARM64 asset
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create-release.outputs.upload_url }}
          asset_path: artifacts/aarch64-apple-darwin/aarch64-apple-darwin.zip
          asset_name: macos-arm64.zip
          asset_content_type: application/zip
      - name: Upload MacOS x64 asset
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create-release.outputs.upload_url }}
          asset_path: artifacts/x86_64-apple-darwin/x86_64-apple-darwin.zip
          asset_name: macos-x64.zip
          asset_content_type: application/zip
      - name: Upload Linux x64 asset
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create-release.outputs.upload_url }}
          asset_path: artifacts/x86_64-unknown-linux-gnu/x86_64-unknown-linux-gnu.zip
          asset_name: linux-x64.zip
          asset_content_type: application/zip
      - name: Upload Linux ARM64 asset
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create-release.outputs.upload_url }}
          asset_path: artifacts/aarch64-unknown-linux-gnu/aarch64-unknown-linux-gnu.zip
          asset_name: linux-arm64.zip
          asset_content_type: application/zip

  publish-crates:
    name: Distrubute to Crates.io
    runs-on: ubuntu-latest
    needs: release
    steps:
      - uses: actions/checkout@v6
      - name: Update Version
        shell: pwsh
        run: |
          $version = (Get-Content -Path Cargo.toml -Raw) -replace 'version = "0.1.0"', 'version = "${{ env.VERSION }}"'
          $version | Set-Content -Path Cargo.toml
      - name: Install Rust
        uses: christianhelle/rust-toolchain@stable
      - name: Rust cache
        uses: christianhelle/rust-cache@v2
      - name: Publish to crates.io
        run: cargo publish --allow-dirty --token ${{ secrets.CRATES_TOKEN }}

  homebrew-distribute:
    name: Distribute to Homebrew
    runs-on: ubuntu-latest
    needs: release
    steps:
      - uses: actions/checkout@v6
      - name: Download artifacts
        uses: actions/download-artifact@v8
        with:
          path: artifacts
      - name: Calculate SHA256 checksums
        id: checksums
        run: |
          echo "macos_x64_sha=$(sha256sum artifacts/x86_64-apple-darwin/x86_64-apple-darwin.zip | cut -d' ' -f1)" >> $GITHUB_OUTPUT
          echo "macos_arm64_sha=$(sha256sum artifacts/aarch64-apple-darwin/aarch64-apple-darwin.zip | cut -d' ' -f1)" >> $GITHUB_OUTPUT
      - name: Generate Homebrew formula
        run: |
          ./scripts/generate-homebrew.sh "${{ env.VERSION }}" "${{ steps.checksums.outputs.macos_x64_sha }}" "${{ steps.checksums.outputs.macos_arm64_sha }}"
      - name: Upload Homebrew artifacts
        uses: actions/upload-artifact@v7
        with:
          name: homebrew-package
          path: homebrew/azdocli-${{ env.VERSION }}.rb
      - name: Create Homebrew PR
        if: github.event_name == 'push' && github.ref == 'refs/heads/main'
        env:
          GITHUB_TOKEN: ${{ secrets.HOMEBREW_GITHUB_TOKEN }}
        run: |
          # Note: This would require setting up a separate repository for Homebrew tap
          # For now, we just generate the formula
          echo "Homebrew formula generated. Manual submission to homebrew-core or create a tap required."
          echo "Formula location: homebrew/azdocli-${{ env.VERSION }}.rb"

  chocolatey-distribute:
    name: Distribute to Chocolatey
    runs-on: windows-latest
    needs: release
    steps:
      - uses: actions/checkout@v6
      - name: Download artifacts
        uses: actions/download-artifact@v8
        with:
          path: artifacts
      - name: Update Version
        working-directory: chocolatey
        shell: pwsh
        run: |
          $version = (Get-Content -Path azdocli.nuspec -Raw) -replace 'VERSION_PLACEHOLDER', '${{ env.VERSION }}'
          $version | Set-Content -Path azdocli.nuspec
          $checksum = (Get-FileHash -Path artifacts/x86_64-pc-windows-msvc/x86_64-pc-windows-msvc.zip -Algorithm SHA256).Hash
          (Get-Content -Path chocolatey/tools/chocolateyinstall.ps1 -Raw) -Replace 'SHA256_X64_PLACEHOLDER', $checksum | Set-Content -Path chocolatey/tools/chocolateyinstall.ps1
      - name: Create Chocolatey package
        working-directory: chocolatey
        run: |
          choco pack
      - name: Publish to Chocolatey
        working-directory: chocolatey
        continue-on-error: true
        run: |
          choco push ./azdocli.${{ env.VERSION }}.nupkg --source https://push.chocolatey.org/ --api-key ${{ secrets.CHOCOLATEY_API_KEY }}

  winget-distribute:
    name: Distribute to WinGet
    runs-on: windows-latest
    needs: release
    steps:
      - uses: actions/checkout@v6
      - name: Download artifacts
        uses: actions/download-artifact@v8
        with:
          path: artifacts
      - name: Calculate SHA256 checksums
        id: checksums
        run: |
          echo "windows_x64_sha=$(sha256sum artifacts/x86_64-pc-windows-msvc/x86_64-pc-windows-msvc.zip | cut -d' ' -f1)" >> $GITHUB_OUTPUT
          echo "windows_arm64_sha=$(sha256sum artifacts/aarch64-pc-windows-msvc/aarch64-pc-windows-msvc.zip | cut -d' ' -f1)" >> $GITHUB_OUTPUT
      - name: Generate WinGet manifests
        run: |
          ./scripts/generate-winget.sh "${{ env.VERSION }}" "${{ steps.checksums.outputs.windows_x64_sha }}" "${{ steps.checksums.outputs.windows_arm64_sha }}"
      - name: Upload WinGet artifacts
        uses: actions/upload-artifact@v7
        with:
          name: winget-package
          path: winget-${{ env.VERSION }}/
      - name: Submit to WinGet
        if: github.event_name == 'push' && github.ref == 'refs/heads/main'
        env:
          GITHUB_TOKEN: ${{ secrets.WINGET_GITHUB_TOKEN }}
        run: |
          # Note: This would require setting up winget-cli or creating a PR to winget-pkgs
          # For now, we just generate the manifests
          echo "WinGet manifests generated. Manual submission to microsoft/winget-pkgs required."
          echo "Manifests location: winget-${{ env.VERSION }}/"
