#!/bin/bash

# スクリプトの場所 (scripts/) からプロジェクトルートを動的に取得
ROOT_DIR=$(cd "$(dirname "$0")/.." && pwd)
BASE_DIR="$ROOT_DIR/organized_strudel"
SOURCE_DIR="$ROOT_DIR/strudel"

mkdir -p "$BASE_DIR"

echo "Starting Strudel code categorization..."

for file in "$SOURCE_DIR"/*.md; do
    # ドキュメントファイル自体は除外
    [[ "$file" == *"Strudel Making Sound.md"* ]] && continue
    [[ "$file" == *"Strudel Study.md"* ]] && continue
    [[ "$file" == *"/script/"* ]] && continue
    [ -e "$file" ] || continue

    filename=$(basename "$file")
    echo "Analyzing $filename..."

    # gemini-cli を使用してカテゴリを判定
    category=$(cat "$file" | gemini-cli "Analyze this Strudel code. 
    Based on functions like s(), note(), fm(), degrade(), or effects like lpf(), 
    classify this into exactly one of these categories: 
    'Sampling', 'Synthesis', 'Generative', 'Ambient_FX'. 
    Return only the category name as a single word.")

    # カテゴリが取得できなかった場合のデフォルト
    if [ -z "$category" ]; then
        category="Unclassified"
    fi

    target_path="$BASE_DIR/$category"
    mkdir -p "$target_path"
    
    cp "$file" "$target_path/"
    echo "-> Moved to $category/"
done