# climaybe — Stores to Root / Root to Stores (Multi-store, staging-*)
# Push to staging-* from MAIN (merge): copy stores/<alias>/ → root (store-specific on top of main).
# Push to staging-* from ELSEWHERE (Shopify, direct, feature branch): copy root → stores/<alias>/
# so store-specific JSON changes are persisted, then hotfix-to-main can backport.

name: Stores to Root

on:
  push:
    branches:
      - 'staging-*'
  workflow_dispatch:

# Prevent concurrent runs per branch
concurrency:
  group: stores-to-root-${{ github.ref_name }}
  cancel-in-progress: false

jobs:
  sync:
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v4
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          fetch-depth: 0

      - name: Skip if this is our own sync commit
        id: gate
        run: |
          if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
            echo "skip=false" >> $GITHUB_OUTPUT
            echo "from_main=false" >> $GITHUB_OUTPUT
            exit 0
          fi
          COMMIT_MSG="${{ github.event.head_commit.message }}"
          if echo "$COMMIT_MSG" | grep -qE "\[stores-to-root\]|\[root-to-stores\]"; then
            echo "skip=true" >> $GITHUB_OUTPUT
            echo "from_main=false" >> $GITHUB_OUTPUT
            exit 0
          fi
          # Detect: is this push a merge FROM main? (merge commit with second parent = main)
          MAIN_SHA=$(git rev-parse origin/main 2>/dev/null || echo "")
          if [ -z "$MAIN_SHA" ]; then
            echo "from_main=false" >> $GITHUB_OUTPUT
          else
            SECOND_PARENT=$(git rev-parse HEAD^2 2>/dev/null || echo "")
            if [ "$SECOND_PARENT" = "$MAIN_SHA" ]; then
              echo "from_main=true" >> $GITHUB_OUTPUT
            else
              echo "from_main=false" >> $GITHUB_OUTPUT
            fi
          fi
          echo "skip=false" >> $GITHUB_OUTPUT

      - name: Extract store alias from branch name
        if: steps.gate.outputs.skip != 'true'
        id: alias
        run: |
          BRANCH="${{ github.ref_name }}"
          ALIAS="${BRANCH#staging-}"
          echo "alias=$ALIAS" >> $GITHUB_OUTPUT
          echo "Store alias: $ALIAS (from_main=${{ steps.gate.outputs.from_main }})"

      # --- When push is FROM MAIN: stores → root, and restore store-specific locales ---
      - name: Keep store-specific paths (e.g. locales) after main sync
        if: steps.gate.outputs.skip != 'true' && steps.gate.outputs.from_main == 'true'
        run: |
          if [ ! -d "locales" ]; then
            exit 0
          fi
          PARENT=$(git rev-parse HEAD^1 2>/dev/null || echo "")
          if [ -z "$PARENT" ]; then
            exit 0
          fi
          find locales -name "*.json" 2>/dev/null | while read -r f; do
            git checkout "$PARENT" -- "$f" 2>/dev/null || true
          done
          if ! git diff --quiet; then
            echo "Restored store-specific locale files from pre-merge commit."
          fi

      - name: Sync stores/<alias>/ to root (main merged in)
        if: steps.gate.outputs.skip != 'true' && steps.gate.outputs.from_main == 'true'
        run: |
          ALIAS="${{ steps.alias.outputs.alias }}"
          STORE_DIR="stores/${ALIAS}"
          if [ ! -d "$STORE_DIR" ]; then
            echo "Store directory $STORE_DIR does not exist, skipping."
            exit 0
          fi
          for DIR in config templates sections; do
            SRC="${STORE_DIR}/${DIR}"
            if [ -d "$SRC" ]; then
              find "$SRC" -name "*.json" | while read -r FILE; do
                REL_PATH="${FILE#$STORE_DIR/}"
                if [ "$REL_PATH" = "config/settings_schema.json" ]; then
                  continue
                fi
                mkdir -p "$(dirname "$REL_PATH")"
                cp "$FILE" "$REL_PATH"
                echo "  Copied: $REL_PATH"
              done
            fi
          done

      - name: Commit stores→root
        if: steps.gate.outputs.skip != 'true' && steps.gate.outputs.from_main == 'true'
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git add -A
          if ! git diff --cached --quiet; then
            git commit -m "chore: sync stores/${{ steps.alias.outputs.alias }}/ to root [stores-to-root]"
            git push
          else
            echo "No changes to commit."
          fi

      # --- When push is NOT from main: root → stores/<alias>/ (persist store JSONs) ---
      - name: Sync root to stores/<alias>/ (external push, e.g. Shopify)
        if: steps.gate.outputs.skip != 'true' && steps.gate.outputs.from_main == 'false'
        run: |
          ALIAS="${{ steps.alias.outputs.alias }}"
          STORE_DIR="stores/${ALIAS}"
          mkdir -p "${STORE_DIR}/config" "${STORE_DIR}/templates" "${STORE_DIR}/sections"
          for DIR in config templates sections; do
            if [ -d "$DIR" ]; then
              find "$DIR" -name "*.json" | while read -r FILE; do
                if [ "$FILE" = "config/settings_schema.json" ]; then
                  continue
                fi
                DEST="${STORE_DIR}/${FILE}"
                mkdir -p "$(dirname "$DEST")"
                cp "$FILE" "$DEST"
                echo "  Synced: $FILE → $DEST"
              done
            fi
          done

      - name: Commit root→stores
        if: steps.gate.outputs.skip != 'true' && steps.gate.outputs.from_main == 'false'
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git add -A
          if ! git diff --cached --quiet; then
            git commit -m "chore: sync root to stores/${{ steps.alias.outputs.alias }}/ [root-to-stores]"
            git push
          else
            echo "No changes to commit."
          fi
