name: Release

on:
  push:
    tags:
      - "v*"

permissions:
  contents: write

jobs:
  build:
    strategy:
      matrix:
        include:
          - os: ubuntu-latest
            artifact: vr-linux-amd64
            binary: vr
          - os: macos-latest
            artifact: vr-macos-arm64
            binary: vr
          - os: windows-latest
            artifact: vr-windows-amd64.exe
            binary: vr.exe

    runs-on: ${{ matrix.os }}

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

      - name: Install V (unix)
        if: runner.os != 'Windows'
        shell: bash
        run: |
          git clone --depth 1 https://github.com/vlang/v /tmp/v
          cd /tmp/v && make
          sudo ln -sf /tmp/v/v /usr/local/bin/v
          v version

      - name: Install V (windows)
        if: runner.os == 'Windows'
        shell: bash
        run: |
          curl -L -o v_windows.zip https://github.com/vlang/v/releases/latest/download/v_windows.zip
          unzip -o v_windows.zip -d /tmp/vwin
          echo "/tmp/vwin/v" >> "$GITHUB_PATH"
          v version

      - name: Build toolchain
        shell: bash
        run: v -o ${{ matrix.binary }} .

      - name: Smoke test
        shell: bash
        run: ./${{ matrix.binary }} run examples/hello.vr

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.artifact }}
          path: ${{ matrix.binary }}
          if-no-files-found: error

  release:
    needs: build
    runs-on: ubuntu-latest

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

      - name: Verify tag matches v.mod version
        run: |
          TAG="${GITHUB_REF#refs/tags/v}"
          MOD_VERSION=$(grep -oP "version: '\\K[^\"]+" v.mod)
          if [ "$TAG" != "$MOD_VERSION" ]; then
            echo "::error::Tag version (v${TAG}) does not match v.mod version (${MOD_VERSION})"
            exit 1
          fi
          echo "Version check passed: v${TAG}"

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

      - name: Prepare release assets
        run: |
          mkdir -p release
          for dir in artifacts/*/; do
            name=$(basename "$dir")
            cp "$dir"/* release/"$name"
            chmod +x release/"$name"
          done
          ls -lh release/

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          generate_release_notes: true
          files: release/*
