name: Build and Release Components

on:
  push:
    tags: ['v*']
  workflow_dispatch:
    inputs:
      components:
        description: 'Components to build (comma-separated, or "all")'
        required: false
        default: 'all'

permissions:
  contents: write

jobs:
  # ------------------------------------------------------------------
  # PM3: compile natively on ARM64 runner (no QEMU overhead)
  # ------------------------------------------------------------------
  build-pm3:
    runs-on: ubuntu-24.04-arm64
    strategy:
      matrix:
        python_version: ['3.11', '3.12']
    steps:
      - uses: actions/checkout@v4

      - name: Build PM3 natively on aarch64
        env:
          PYTHON_VERSION: ${{ matrix.python_version }}
        run: |
          docker run --rm \
            -v "${GITHUB_WORKSPACE}:/workspace" \
            -e "PYTHON_VERSION=${PYTHON_VERSION}" \
            -e OUTPUT_DIR=/workspace/dist \
            -w /workspace \
            debian:trixie \
            bash ci/build-pm3.sh

      - uses: actions/upload-artifact@v4
        with:
          name: pm3-cp${{ matrix.python_version }}
          path: dist/pm3-*.tar.gz

  # ------------------------------------------------------------------
  # Frontend: build Remix app
  # ------------------------------------------------------------------
  build-frontend:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: '18'

      - name: Build frontend
        env:
          REF_NAME: ${{ github.ref_name }}
        run: |
          chmod +x ci/build-frontend.sh
          VERSION="${REF_NAME#v}" ci/build-frontend.sh

      - uses: actions/upload-artifact@v4
        with:
          name: frontend
          path: dist/frontend-*.tar.gz

  # ------------------------------------------------------------------
  # Backend: package Python source
  # ------------------------------------------------------------------
  build-backend:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Package backend
        env:
          REF_NAME: ${{ github.ref_name }}
        run: |
          chmod +x ci/build-backend.sh
          VERSION="${REF_NAME#v}" ci/build-backend.sh

      - uses: actions/upload-artifact@v4
        with:
          name: backend
          path: dist/backend-*.tar.gz

  # ------------------------------------------------------------------
  # Theme: build token CSS from design system
  # ------------------------------------------------------------------
  build-theme:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Checkout design system
        uses: actions/checkout@v4
        with:
          repository: dangerous-tacos/dt-design-system
          path: dt-design-system

      - uses: actions/setup-node@v4
        with:
          node-version: '18'

      - name: Build theme
        env:
          REF_NAME: ${{ github.ref_name }}
        run: |
          chmod +x ci/build-theme.sh
          VERSION="${REF_NAME#v}" \
          DESIGN_SYSTEM_DIR="${GITHUB_WORKSPACE}/dt-design-system" \
          ci/build-theme.sh

      - uses: actions/upload-artifact@v4
        with:
          name: theme
          path: dist/theme-*.tar.gz

  # ------------------------------------------------------------------
  # Create release with all component assets
  # ------------------------------------------------------------------
  create-release:
    needs: [build-pm3, build-frontend, build-backend, build-theme]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-python@v5
        with:
          python-version: '3.12'

      - uses: actions/download-artifact@v4
        with:
          path: dist/
          merge-multiple: true

      - name: Generate release manifest
        env:
          REF_NAME: ${{ github.ref_name }}
        run: |
          python3 ci/generate-manifest.py dist/ \
            --version "${REF_NAME#v}"

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          files: |
            dist/*.tar.gz
            dist/release-manifest.json
          generate_release_notes: true
