# This workflow builds the project, creates packages, and publishes a release.
# It triggers on pushes to the main branch and on tags that start with 'v'.
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs

name: Release CI

permissions:
  contents: write

on:
  push:
    branches: ["main"]
    tags:
      - "v*" # Also triggered by tags starting with 'v'

jobs:
  build:
    runs-on: ubuntu-latest
    outputs:
      is_prerelease: ${{ steps.prerelease.outputs.is_prerelease }}

    steps:
      - uses: actions/checkout@v4.1.1
        with:
          submodules: recursive
      - name: Set up Docker environment
        run: |
          echo "TD_WEB_SERVER_HOST=http://127.0.0.1" >> $GITHUB_ENV
          echo "TD_WEB_SERVER_PORT=9981" >> $GITHUB_ENV
      - name: Docker Compose Action
        uses: hoverkraft-tech/compose-action@v2.2.0
        with:
          compose-file: "./docker-compose.yml"
          down-flags: "--volumes"
          services: |
            touchdesigner-mcp-server
      - name: Extract build artifacts and create package
        run: |
          # Create a temporary container to extract build artifacts
          docker create --name temp-mcp touchdesigner-mcp-server
          docker cp temp-mcp:/app/td/modules td/
          docker cp temp-mcp:/app/dist ./
          docker rm temp-mcp

          # Create a package for the td directory
          echo "Creating td directory package..."
          mkdir -p temp_td
          cp td/import_modules.py temp_td/
          cp td/mcp_webserver_base.tox temp_td/
          cp -r td/modules temp_td/
          cd temp_td && zip -r ../touchdesigner-mcp-td.zip .
          rm -rf temp_td
          echo "Build artifacts extracted and package created!"
      - name: Build MCPB package
        run: |
          # Build MCPB package
          npm run build:mcpb

          echo "MCPB package created successfully!"
          ls -la *.mcpb
      - name: Get Node.js project information
        id: projectinfo
        uses: gregoranders/nodejs-project-info@master
      - name: Determine if prerelease
        id: prerelease
        run: |
          VERSION="${{ steps.projectinfo.outputs.version }}"
          if [[ "$VERSION" == *"-"* ]] || [[ "$VERSION" == *"beta"* ]] || [[ "$VERSION" == *"alpha"* ]] || [[ "$VERSION" == *"rc"* ]] || [[ "$VERSION" == *"test"* ]]; then
            echo "is_prerelease=true" >> $GITHUB_OUTPUT
            echo "::notice::Detected prerelease version: $VERSION"
          else
            echo "is_prerelease=false" >> $GITHUB_OUTPUT
            echo "::notice::Detected production version: $VERSION"
          fi
      - run: |
          echo "v${{ steps.projectinfo.outputs.version }}" > TAG_NAME
          git tag $(cat TAG_NAME)
          git push origin $(cat TAG_NAME)
      - name: Create Release
        id: create_release
        uses: softprops/action-gh-release@v2
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: v${{ steps.projectinfo.outputs.version }}
          name: ${{ steps.projectinfo.outputs.name }} - ${{ steps.projectinfo.outputs.version }} Release
          body: |
            ## Downloads
            - **TouchDesigner Components**: [touchdesigner-mcp-td.zip](https://github.com/${{ github.repository }}/releases/download/v${{ steps.projectinfo.outputs.version }}/touchdesigner-mcp-td.zip)
            - **MCP Bundle (.mcpb)**: [touchdesigner-mcp.mcpb](https://github.com/${{ github.repository }}/releases/download/v${{ steps.projectinfo.outputs.version }}/touchdesigner-mcp.mcpb)

            ## Installation

            For complete installation instructions, see the **[Installation Guide](https://github.com/${{ github.repository }}/blob/main/docs/installation.md)**. _[日本語](https://github.com/${{ github.repository }}/blob/main/docs/installation.ja.md)_

            ### Quick Start (Claude Desktop)
            1. Download `TouchDesigner Components` and the `MCP Bundle` from the Downloads section above
            2. Extract `touchdesigner-mcp-td.zip` and import `mcp_webserver_base.tox` into your TouchDesigner project
            3. Double-click `touchdesigner-mcp.mcpb` to install in Claude Desktop

            ### Other AI Agents
            See the [Installation Guide](https://github.com/${{ github.repository }}/blob/main/docs/installation.md) _[日本語](https://github.com/${{ github.repository }}/blob/main/docs/installation.ja.md)_ for:
            - Claude Code
            - Codex
            - Docker deployment
            - Developer setup

            ## For Updates from Previous Versions
            1. Download `TouchDesigner Components` from the Downloads section above
            2. Replace TouchDesigner components:
              1. Delete the existing touchdesigner-mcp-td folder from your system
              2. Delete old mcp_webserver_base node from your TouchDesigner project
              3. Extract and import the new mcp_webserver_base.tox to your TouchDesigner project
            3. Restart TouchDesigner and the MCP client (e.g., Claude Desktop)

            For more details, see: https://github.com/8beeeaaat/touchdesigner-mcp#troubleshooting-version-compatibility

            ## Changes
            See [CHANGELOG.md](https://github.com/${{ github.repository }}/blob/main/CHANGELOG.md) for a detailed history of changes.
          files: |
            touchdesigner-mcp.mcpb
            touchdesigner-mcp-td.zip
          draft: false
          prerelease: ${{ steps.prerelease.outputs.is_prerelease == 'true' }}

  publish-npm:
    needs: build
    runs-on: ubuntu-latest
    permissions:
      contents: read
      id-token: write
    steps:
      - uses: actions/checkout@v4.1.1
      - uses: actions/setup-node@v4
        with:
          node-version-file: ".node-version"
          registry-url: "https://registry.npmjs.org"
      - run: npm ci
      - run: npm run build
      - name: Publish to npm
        run: |
          if [[ "${{ needs.build.outputs.is_prerelease }}" == "true" ]]; then
            echo "Publishing prerelease version with 'prerelease' tag..."
            npm publish --access public --tag prerelease --provenance
          else
            echo "Publishing production version with 'latest' tag..."
            npm publish --access public --provenance
          fi
