#!/usr/bin/env python3
"""
Extract cluster data with raw posts for sandbox testing.
Runs Layer 1 (embedding + clustering) and saves full cluster data.
"""

import sys
import os
import json

# Add parent directory to path
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from analyze import load_trends_from_db, layer1_embed_and_cluster, NumpyEncoder
from channel_config import get_channel

SANDBOX_DIR = os.path.dirname(os.path.abspath(__file__))
OUTPUT_DIR = os.path.join(os.path.dirname(SANDBOX_DIR), 'output')

def extract_clusters_for_channel(channel_id: str):
    """Run Layer 1 and save cluster data for a channel."""
    print(f"\n{'='*60}")
    print(f"Extracting clusters for: {channel_id}")
    print(f"{'='*60}")

    # Get channel config
    channel = get_channel(channel_id)
    if not channel:
        print(f"Channel '{channel_id}' not found in config")
        return None

    print(f"Channel: {channel.name}")

    # Load trends for this channel
    trends = load_trends_from_db(channel_id=channel_id)
    print(f"Loaded {len(trends)} items from database")

    if len(trends) < 10:
        print(f"Not enough items for clustering")
        return None

    # Run Layer 1
    clusters_data = layer1_embed_and_cluster(trends)

    # Save to sandbox with NumpyEncoder for numpy types
    output_path = os.path.join(SANDBOX_DIR, f'{channel_id}_clusters.json')
    with open(output_path, 'w') as f:
        json.dump(clusters_data, f, indent=2, cls=NumpyEncoder)

    print(f"Saved {len(clusters_data.get('clusters', []))} clusters to {output_path}")

    return clusters_data

def main():
    channels_to_extract = ['brain_is_lying', 'money_traps']

    for channel in channels_to_extract:
        extract_clusters_for_channel(channel)

    print("\n" + "="*60)
    print("Cluster extraction complete!")
    print("="*60)

if __name__ == "__main__":
    main()
