name: "Discover and Detect Skins"
description: "Find all skins and detect which ones changed since last tag or based on inputs"

inputs:
  force_rebuild:
    description: "Force rebuild all skins"
    required: false
    default: "false"
  target_skins:
    description: "Comma-separated list of skins to rebuild"
    required: false
    default: ""

outputs:
  changed_skins_file:
    description: "Path to file containing changed skins"
    value: ${{ steps.detect.outputs.changed_skins_file }}
  all_skins:
    description: "All discovered skins (JSON array)"
    value: ${{ steps.discover_all.outputs.all_skins }}

runs:
  using: "composite"
  steps:
    - name: Discover all skins
      id: discover_all
      shell: bash
      run: |
        echo "Discovering all skins in $SKINS_DIR…"
        mapfile -t skins < <(
          find "$SKINS_DIR" -mindepth 1 -maxdepth 1 -type d \
            | sed 's|'"$SKINS_DIR"'/||'
        )

        # Convert to compact JSON
        json=$(printf '%s\n' "${skins[@]}" | jq -R . | jq -cs .)

        {
          echo "all_skins<<EOF"
          echo "$json"
          echo "EOF"
        } >> "$GITHUB_OUTPUT"

    - name: Detect Changed Skin Directories
      id: detect
      shell: bash
      run: |
        echo "[Detect Changed Skin Directories Started]"

        # Parse JSON back into Bash array
        readarray -t all_skins < <(echo '${{ steps.discover_all.outputs.all_skins }}' | jq -r '.[]')

        force_rebuild="${{ inputs.force_rebuild }}"
        target_skins="${{ inputs.target_skins }}"
        skins=()
        deleted_skins=()

        echo "→ Force rebuild flag: $force_rebuild"
        echo "→ Target skins input: $target_skins"

        if [[ "$force_rebuild" == "true" ]]; then
          echo "→ Force rebuild is enabled. Using ALL_SKINS_DIR for full list…"
          skins=("${all_skins[@]}")
          echo "    ✓ Found ${#skins[@]} skin directories (from ALL_SKINS_DIR)"

        elif [[ -n "$target_skins" ]]; then
          echo "→ Target skins specified. Using target_skins input…"
          IFS=',' read -r -a input_skins <<< "$target_skins"
          for s in "${input_skins[@]}"; do
            s="${s#"${s%%[![:space:]]*}"}"
            s="${s%"${s##*[![:space:]]}"}"
            [[ -n "$s" ]] && skins+=("$s")
          done
          echo "    ✓ Found ${#skins[@]} skin(s) from target_skins input"

        else
          echo "→ No rebuild flags set. Finding latest git tag..."
          latest_tag=$(git tag --sort=-creatordate | head -n 1 || true)

          if [[ -n "$latest_tag" ]]; then
            echo "→ Latest tag found: $latest_tag"
            echo "→ Finding added/modified skins since $latest_tag…"

            mapfile -t skins < <(
              git diff --name-only -z --diff-filter=AM "$latest_tag" HEAD \
              | while IFS= read -r -d '' file; do
                  [[ $file == Skins/* ]] && echo "${file#Skins/}" | cut -d/ -f1
                done | sort -u
            )
            echo "    ✓ Found ${#skins[@]} added/modified skins"

            echo "→ Finding deleted skins since $latest_tag…"
            mapfile -t deleted_skins < <(
              git diff --name-only -z --diff-filter=D "$latest_tag" HEAD \
              | while IFS= read -r -d '' file; do
                  [[ $file == Skins/* ]] && echo "${file#Skins/}" | cut -d/ -f1
                done | sort -u
            )
            if [ "${#deleted_skins[@]}" -gt 0 ]; then
              for d in "${deleted_skins[@]}"; do
                echo "→ Skin '$d' was deleted"
              done
            else
              echo "    ✓ No skins deleted"
            fi
          else
            echo "→ No tag found. Falling back to ALL_SKINS_DIR for full list…"
            skins=("${all_skins[@]}")
            echo "    ✓ Found ${#skins[@]} skin directories (from ALL_SKINS_DIR)"
          fi
        fi

        echo ""
        echo "[Cleaning Skin Names]"
        uniq_skins=()
        for skin in "${skins[@]}"; do
          skin="${skin#"${skin%%[![:space:]]*}"}"
          skin="${skin%"${skin##*[![:space:]]}"}"
          [[ -n "$skin" ]] && uniq_skins+=("$skin")
        done
        echo "    ✓ ${#uniq_skins[@]} valid skin names after cleaning"

        echo ""
        if [ "${#uniq_skins[@]}" -eq 0 ]; then
          echo "→ No added/modified skins detected."
          {
            echo "changed_skins_file<<EOF"
            echo ""
            echo "EOF"
          } >> "$GITHUB_OUTPUT"
        else
          echo "[Writing Changed Skins to File]"
          changed_skins_file=$(mktemp)
          printf "%s\n" "${uniq_skins[@]}" > "$changed_skins_file"
          echo "    ✓ Skins written to $changed_skins_file"

          {
            echo "changed_skins_file<<EOF"
            echo "$changed_skins_file"
            echo "EOF"
          } >> "$GITHUB_OUTPUT"
        fi

        echo ""
        echo "[Detect Changed Skin Directories Complete — ${#uniq_skins[@]} skins processed, ${#deleted_skins[@]} skins deleted]"
