#!/bin/bash

# ============================================
# Nostr Playlist Fetcher for Hugo
# ============================================

# Configuration
PUBKEY="3f11abb2e235da2d4dda5d6deb2f123173476a745a3ca56895a1d0f632a42f40"
RELAY="wss://drops.basspistol.org"
DATA_DIR="data/playlists"
RAW_DIR="data/playlists/raw"

mkdir -p "$DATA_DIR" "$RAW_DIR"

log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1"; }

# ============================================
# Step 1: Fetch playlists and tracks
# ============================================

log "Fetching playlist events (kind 34139) from $RELAY..."
nak req -k 34139 -a "$PUBKEY" "$RELAY" > "$RAW_DIR/playlists-raw.jsonl"

log "Fetching ALL tracks (kind 36787) from $PUBKEY..."
nak req -k 36787 -a "$PUBKEY" "$RELAY" > "$RAW_DIR/tracks-raw.jsonl"

# ============================================
# Step 2: Convert JSONL to JSON arrays
# ============================================

log "Converting raw data to JSON arrays..."

# Convert jsonl to json arrays
cat "$RAW_DIR/playlists-raw.jsonl" | jq -s '.' > "$RAW_DIR/playlists-raw.json"
cat "$RAW_DIR/tracks-raw.jsonl" | jq -s '.' > "$RAW_DIR/tracks-raw.json"

# ============================================
# Step 3: Build playlist data with jq
# ============================================

log "Building playlist data structure..."

# Build the playlists.json using the json files
jq -n \
    --slurpfile playlists "$RAW_DIR/playlists-raw.json" \
    --slurpfile tracks "$RAW_DIR/tracks-raw.json" \
    '{
        playlists: [
            $playlists[] | .[] | 
            select(.kind == 34139) |
            {
                id: .id,
                created_at: .created_at,
                name: (.tags[] | select(.[0] == "title") | .[1] // ""),
                description: (.tags[] | select(.[0] == "description") | .[1] // ""),
                image: (.tags[] | select(.[0] == "image") | .[1] // ""),
                tracks: [
                    .tags[] | 
                    select(.[0] == "a") | .[1] as $ref |
                    $tracks[] | .[] | 
                    select(
                        ($ref | split(":")[2]) == (.tags[] | select(.[0] == "d") | .[1])
                    ) |
                    {
                        id: .id,
                        created_at: .created_at,
                        title: (.tags[] | select(.[0] == "title") | .[1] // "Untitled"),
                        artist: (.tags[] | select(.[0] == "artist") | .[1] // "Unknown Artist"),
                        album: (.tags[] | select(.[0] == "album") | .[1] // ""),
                        url: (.tags[] | select(.[0] == "url") | .[1] // ""),
                        image: (.tags[] | select(.[0] == "image") | .[1] // ""),
                        duration: (.tags[] | select(.[0] == "duration") | .[1] // ""),
                        tags: [.tags[] | select(.[0] == "t") | .[1]]
                    }
                ]
            }
        ]
    }' > "$DATA_DIR/playlists.json"

# ============================================
# Step 4: Generate flattened tracks file
# ============================================

log "Generating flattened tracks file..."

cat "$RAW_DIR/tracks-raw.json" | jq '
    .[] | {
        id: .id,
        created_at: .created_at,
        title: (.tags[] | select(.[0] == "title") | .[1] // "Untitled"),
        artist: (.tags[] | select(.[0] == "artist") | .[1] // "Unknown Artist"),
        album: (.tags[] | select(.[0] == "album") | .[1] // ""),
        url: (.tags[] | select(.[0] == "url") | .[1] // ""),
        image: (.tags[] | select(.[0] == "image") | .[1] // ""),
        duration: (.tags[] | select(.[0] == "duration") | .[1] // ""),
        tags: [.tags[] | select(.[0] == "t") | .[1]],
        d_tag: (.tags[] | select(.[0] == "d") | .[1] // "")
    }
' | jq -s '.' > "$DATA_DIR/tracks.json"

# ============================================
# Step 5: Cleanup raw jsonl files
# ============================================

rm -f "$RAW_DIR"/*.jsonl

# ============================================
# Step 6: Summary
# ============================================

PLAYLIST_COUNT=$(cat "$DATA_DIR/playlists.json" | jq '.playlists | length' 2>/dev/null || echo "0")
TRACK_COUNT=$(cat "$DATA_DIR/tracks.json" | jq 'length' 2>/dev/null || echo "0")

echo ""
echo "========================================"
echo "📊 Playlist Summary"
echo "========================================"
echo "  📀 Playlists: $PLAYLIST_COUNT"
echo "  🎵 Total tracks found: $TRACK_COUNT"
echo "  📁 Data saved to: $DATA_DIR/"
echo "========================================"

echo ""
echo "📝 Playlist Names:"
cat "$DATA_DIR/playlists.json" | jq -r '.playlists[].name' 2>/dev/null | while read -r name; do
    if [ -n "$name" ]; then
        echo "  🎵 $name"
    fi
done
