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@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          fetch-depth: 0

      - name: Setup Node.js
        uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f # v6.3.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@153bb8e04406b158c6c84fc1615b65b24149a1fe # v2.6.1
        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
