name: "Prepare Assets"
description: "Setup environment dirs and create directories for assets"

inputs:
  all_skins:
    description: "JSON array of all skins"
    required: true

outputs:
  user_repository:
    description: "Path of the repository (relative inside container)"
    value: ${{ steps.repo.outputs.user_repository }}

runs:
  using: "composite"
  steps:
    - name: Extract Repository path
      id: repo
      shell: bash
      run: |
        echo "Extracting repository path..."
        USER_REPOSITORY="${{ github.workspace }}"
        USER_REPOSITORY="${USER_REPOSITORY#/workspace/}"
        USER_REPOSITORY="${USER_REPOSITORY%/}"
        echo "Repository path extracted: $USER_REPOSITORY"
        echo "user_repository=$USER_REPOSITORY" >> $GITHUB_OUTPUT

    - name: Set XDG_RUNTIME_DIR
      shell: bash
      run: |
        echo "Setting XDG_RUNTIME_DIR..."
        mkdir -p /tmp/xdg_runtime_dir
        chmod 0700 /tmp/xdg_runtime_dir
        echo "XDG_RUNTIME_DIR=/tmp/xdg_runtime_dir" >> "$GITHUB_ENV"
        echo "XDG_RUNTIME_DIR set."

    - name: Create directories for assets
      shell: bash
      run: |
        echo "Creating base directories for assets..."
        mkdir -p "$REPO_SCREENSHOT_DIR" "$REPO_MOD_ICONS_DIR" "$REPO_RANKING_PANEL_DIR" "$OSK_PATH" "$REPO_THUMBNAIL_DIR"

        echo '${{ inputs.all_skins }}' | jq -r '.[]' | while IFS= read -r skin; do
          [ -z "$skin" ] && continue
          echo " → Creating subdirs for '$skin'…"
          mkdir -p \
            "$REPO_SCREENSHOT_DIR/$skin" \
            "$REPO_MOD_ICONS_DIR/$skin" \
            "$REPO_RANKING_PANEL_DIR/$skin" \
            "$OSK_PATH/$skin" \
            "$REPO_THUMBNAIL_DIR/$skin"
        done

        echo "All asset directories created."

    - name: Move Skin files to Danser Skins directory
      shell: bash
      run: |
        echo "Moving Skin files to Danser Skins directory..."
        mkdir -p "$DANSER_SKINS_DIR"
        mv "$SKINS_DIR"/* "$DANSER_SKINS_DIR"
        echo "Skin files moved."
