name: "Build Browser Extension"
description: "Builds, tests, lints, zips, and optionally uploads a browser extension"
inputs:
  browser:
    description: "Target browser to build for (chrome, firefox, edge)"
    required: true
  release_tag:
    description: "If set, upload the resulting zip(s) to the existing GitHub Release at this tag (draft or published). Fails if no such release exists."
    required: false
    default: ""

runs:
  using: "composite"
  steps:
    - name: Setup Node.js
      uses: actions/setup-node@v6
      with:
        node-version: '24'
        cache: 'npm'
        cache-dependency-path: apps/browser-extension/package-lock.json

    - name: Setup Rust toolchain
      uses: dtolnay/rust-toolchain@stable
      with:
        targets: wasm32-unknown-unknown

    - name: Cache Rust dependencies
      uses: Swatinem/rust-cache@v2
      with:
        workspaces: core/rust

    - name: Install wasm-pack
      shell: bash
      run: cargo install wasm-pack --locked

    - name: Build core libraries
      shell: bash
      run: |
        cd ./core
        chmod +x build-and-distribute.sh
        ./build-and-distribute.sh

    - name: Verify core library distribution
      shell: bash
      run: |
        # Verify critical rust WASM files exist
        if [ ! -f "apps/browser-extension/src/utils/dist/core/rust/aliasvault_core_bg.wasm" ]; then
          echo "Error: Rust WASM files not found in browser extension"
          exit 1
        fi
        echo "Core library distribution verified"

    - name: Install dependencies
      run: npm ci
      shell: bash
      working-directory: apps/browser-extension

    - name: Build extension
      run: npm run build:${{ inputs.browser }}
      shell: bash
      working-directory: apps/browser-extension

    - name: Run tests
      run: npm run test
      shell: bash
      working-directory: apps/browser-extension

    - name: Run linting
      run: npm run lint
      shell: bash
      working-directory: apps/browser-extension

    - name: Zip Extension
      run: npm run zip:${{ inputs.browser }}
      shell: bash
      working-directory: apps/browser-extension

    - name: Extract version
      run: |
        VERSION=$(node -p "require('./package.json').version")
        echo "VERSION=$VERSION" >> $GITHUB_ENV
      shell: bash
      working-directory: apps/browser-extension

    - name: Unzip extension
      run: |
        mkdir -p dist/${{ inputs.browser }}-unpacked
        unzip dist/aliasvault-browser-extension-${{ env.VERSION }}-${{ inputs.browser }}.zip -d dist/${{ inputs.browser }}-unpacked
      shell: bash
      working-directory: apps/browser-extension

    - name: Upload artifact
      uses: actions/upload-artifact@v7
      with:
        name: aliasvault-${{ env.VERSION }}-${{ inputs.browser }}
        path: apps/browser-extension/dist/${{ inputs.browser }}-unpacked

    - name: Unzip and upload Firefox sources
      if: ${{ inputs.browser == 'firefox' }}
      run: |
        mkdir -p dist/sources-unpacked
        unzip dist/aliasvault-browser-extension-${{ env.VERSION }}-sources.zip -d dist/sources-unpacked
      shell: bash
      working-directory: apps/browser-extension

    - name: Upload Firefox sources artifact
      if: ${{ inputs.browser == 'firefox' }}
      uses: actions/upload-artifact@v7
      with:
        name: aliasvault-${{ env.VERSION }}-browser-extension-sources
        path: apps/browser-extension/dist/sources-unpacked

    - name: Rename zip files
      run: |
        mv apps/browser-extension/dist/aliasvault-browser-extension-${{ env.VERSION }}-${{ inputs.browser }}.zip apps/browser-extension/dist/aliasvault-${{ env.VERSION }}-${{ inputs.browser }}.zip
        if [ -f apps/browser-extension/dist/aliasvault-browser-extension-${{ env.VERSION }}-sources.zip ]; then
          mv apps/browser-extension/dist/aliasvault-browser-extension-${{ env.VERSION }}-sources.zip apps/browser-extension/dist/aliasvault-${{ env.VERSION }}-browser-extension-sources.zip
        fi
      shell: bash

    - name: Upload to GitHub Release
      if: ${{ inputs.release_tag != '' }}
      shell: bash
      env:
        GH_TOKEN: ${{ env.GITHUB_TOKEN }}
        RELEASE_TAG: ${{ inputs.release_tag }}
        BROWSER: ${{ inputs.browser }}
      run: |
        if ! gh release view "$RELEASE_TAG" --repo "$GITHUB_REPOSITORY" > /dev/null 2>&1; then
          echo "❌ No release exists at tag '$RELEASE_TAG'."
          echo "Create a draft release first, e.g.:"
          echo "  gh release create $RELEASE_TAG --draft --title $RELEASE_TAG --notes ''"
          exit 1
        fi

        FILES=("apps/browser-extension/dist/aliasvault-${VERSION}-${BROWSER}.zip")
        if [ "$BROWSER" = "firefox" ]; then
          FILES+=("apps/browser-extension/dist/aliasvault-${VERSION}-browser-extension-sources.zip")
        fi

        echo "Uploading ${#FILES[@]} file(s) to release $RELEASE_TAG..."
        gh release upload "$RELEASE_TAG" "${FILES[@]}" --clobber --repo "$GITHUB_REPOSITORY"
