name: Publish to npm

on:
  push:
    tags:
      - 'v*'

jobs:
  npm-publish:
    runs-on: ubuntu-latest

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

      - name: Set up Deno
        uses: denoland/setup-deno@v1
        with:
          deno-version: v2.x

      - name: Setup Node.js for npm publishing
        uses: actions/setup-node@v4
        with:
          node-version: '18.x'
          registry-url: 'https://registry.npmjs.org/'

      - name: Get version from tag
        id: version
        run: |
          VERSION=${GITHUB_REF#refs/tags/}
          echo "version=$VERSION" >> $GITHUB_OUTPUT
          echo "version_number=${VERSION#v}" >> $GITHUB_OUTPUT
          echo "Publishing version: $VERSION"

      - name: Verify deno.json version matches tag
        run: |
          DENO_VERSION=$(grep -o '"version": "[^"]*"' deno.json | cut -d'"' -f4)
          TAG_VERSION="${{ steps.version.outputs.version_number }}"
          echo "deno.json version: $DENO_VERSION"
          echo "Tag version: $TAG_VERSION"
          if [ "$DENO_VERSION" != "$TAG_VERSION" ]; then
            echo "ERROR: Version mismatch!"
            echo "deno.json has version $DENO_VERSION but tag is $TAG_VERSION"
            exit 1
          fi

      - name: Compile binaries for npm package
        run: |
          echo "Compiling binaries for npm package..."
          deno task compile
          echo ""
          echo "Binary sizes:"
          ls -lh dist/binaries/

      - name: Generate SHA256 checksums
        run: |
          cd dist/binaries
          sha256sum * > SHA256SUMS
          cat SHA256SUMS
          cd ../..

      - name: Sync package.json version
        run: |
          VERSION="${{ steps.version.outputs.version_number }}"
          echo "Syncing package.json to version ${VERSION}..."
          npm version ${VERSION} --no-git-tag-version --allow-same-version
          echo "package.json version: $(grep '"version"' package.json | head -1)"

      - name: Create npm package
        run: |
          echo "Creating npm package..."
          npm pack
          echo ""
          echo "Package created:"
          ls -lh *.tgz

      - name: Test local installation
        run: |
          echo "Testing local package installation..."
          PACKAGE_FILE=$(ls *.tgz)
          npm install -g ${PACKAGE_FILE}
          echo ""
          echo "Testing mailer command:"
          mailer --version || echo "Note: Binary execution may fail in CI environment"
          echo ""
          echo "Checking installed files:"
          npm ls -g @serve.zone/mailer || true

      - name: Publish to npm
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
        run: |
          echo "Publishing to npm registry..."
          npm publish --access public
          echo ""
          echo "✅ Successfully published @serve.zone/mailer to npm!"
          echo ""
          echo "Package info:"
          npm view @serve.zone/mailer

      - name: Verify npm package
        run: |
          echo "Waiting for npm propagation..."
          sleep 30
          echo ""
          echo "Verifying published package..."
          npm view @serve.zone/mailer
          echo ""
          echo "Testing installation from npm:"
          npm install -g @serve.zone/mailer
          echo ""
          echo "Package installed successfully!"
          which mailer || echo "Binary location check skipped"

      - name: Publish Summary
        run: |
          echo "================================================"
          echo "  npm Publish Complete!"
          echo "================================================"
          echo ""
          echo "✅ Package: @serve.zone/mailer"
          echo "✅ Version: ${{ steps.version.outputs.version }}"
          echo ""
          echo "Installation:"
          echo "  npm install -g @serve.zone/mailer"
          echo ""
          echo "Registry:"
          echo "  https://www.npmjs.com/package/@serve.zone/mailer"
          echo ""
