name: Create GitHub Release

on:
  push:
    tags:
      - 'v*.*.*'

permissions:
  contents: write

jobs:
  release:
    name: Create Release
    runs-on: ubuntu-latest
    timeout-minutes: 30

    steps:
      - name: Checkout code
        uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3
        with:
          fetch-depth: 0

      - name: Setup Node.js
        uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
        with:
          node-version: '20'
          cache: 'npm'

      - name: Install dependencies
        run: npm ci

      - name: Build package
        run: npm run build

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

      - name: Extract changelog for this version
        id: changelog
        env:
          VERSION: ${{ steps.version.outputs.version }}
          TAG: ${{ steps.version.outputs.tag }}
        run: |
          CHANGELOG=$(awk -v ver="$VERSION" '
            $0 ~ "## .*\\[" ver "\\]" { found=1; next }
            found && /^## .*\[/ { exit }
            found { print }
          ' CHANGELOG.md)

          if [ -z "$CHANGELOG" ]; then
            CHANGELOG="Release $TAG"
          fi

          echo "$CHANGELOG" > release_notes.md
          echo "Generated release notes:"
          cat release_notes.md

      - name: Create dist archive
        env:
          VERSION: ${{ steps.version.outputs.version }}
        run: |
          mkdir -p release-artifacts

          # Pre-built archive (ready to run)
          tar -czvf release-artifacts/unreal-engine-mcp-server-${VERSION}.tar.gz \
            dist/ \
            package.json \
            README.md \
            CHANGELOG.md \
            LICENSE

          zip -r release-artifacts/unreal-engine-mcp-server-${VERSION}.zip \
            dist/ \
            package.json \
            README.md \
            CHANGELOG.md \
            LICENSE

          # Source archive (for rebuilding)
          OPTIONAL_PATHS=""
          [ -d "wasm" ] && OPTIONAL_PATHS="$OPTIONAL_PATHS wasm/"
          [ -f "server.json" ] && OPTIONAL_PATHS="$OPTIONAL_PATHS server.json"

          tar -czvf release-artifacts/unreal-engine-mcp-server-${VERSION}-source.tar.gz \
            dist/ \
            src/ \
            package.json \
            package-lock.json \
            tsconfig.json \
            eslint.config.mjs \
            README.md \
            CHANGELOG.md \
            LICENSE \
            $OPTIONAL_PATHS

          zip -r release-artifacts/unreal-engine-mcp-server-${VERSION}-source.zip \
            dist/ \
            src/ \
            package.json \
            package-lock.json \
            tsconfig.json \
            eslint.config.mjs \
            README.md \
            CHANGELOG.md \
            LICENSE \
            $OPTIONAL_PATHS

      - name: Create Plugin archive
        env:
          VERSION: ${{ steps.version.outputs.version }}
        run: |
          if [ -d "plugins/McpAutomationBridge" ]; then
            tar -czvf release-artifacts/McpAutomationBridge-plugin-${VERSION}.tar.gz \
              plugins/McpAutomationBridge/

            zip -r release-artifacts/McpAutomationBridge-plugin-${VERSION}.zip \
              plugins/McpAutomationBridge/

            echo "Plugin archive created"
          else
            echo "Plugin directory not found, skipping plugin archive"
          fi

      - name: Create GitHub Release
        uses: softprops/action-gh-release@b4309332981a82ec1c5618f44dd2e27cc8bfbfda # v3.0.0
        with:
          name: Release ${{ steps.version.outputs.tag }}
          body_path: release_notes.md
          draft: false
          prerelease: ${{ contains(github.ref, 'alpha') || contains(github.ref, 'beta') || contains(github.ref, 'rc') }}
          files: |
            release-artifacts/*
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Release Summary
        env:
          TAG: ${{ steps.version.outputs.tag }}
        run: |
          echo "## Release Created Successfully! 🎉" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "**Version:** $TAG" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "### Artifacts:" >> $GITHUB_STEP_SUMMARY
          ls -la release-artifacts/ >> $GITHUB_STEP_SUMMARY
