#!/usr/bin/env python3
"""
Script to apply the chain execution groups database schema to Supabase.
"""

import os
import asyncio
from dotenv import load_dotenv
from src.pit.storage.supabase_client import SupabaseManager

async def apply_schema():
    """Apply the chain execution groups schema to Supabase."""
    load_dotenv()
    
    # Check for required environment variables
    supabase_url = os.getenv("PIT_SUPABASE_URL")
    service_key = os.getenv("PIT_SUPABASE_SERVICE_ROLE_KEY")
    
    if not supabase_url or not service_key:
        print("❌ Missing required environment variables:")
        print("   - PIT_SUPABASE_URL")
        print("   - PIT_SUPABASE_SERVICE_ROLE_KEY")
        return
    
    try:
        # Create Supabase manager
        manager = SupabaseManager(supabase_url, service_key)
        
        # Get the schema
        schema = manager.get_database_schema()
        
        print("🔧 Applying Chain Execution Groups Schema to Supabase...")
        print("=" * 60)
        print(schema)
        print("=" * 60)
        
        # Note: This would require direct SQL execution capability
        # For now, we'll print the schema for manual application
        print("\n📋 To apply this schema:")
        print("1. Go to your Supabase dashboard")
        print("2. Navigate to SQL Editor")
        print("3. Copy and paste the schema above")
        print("4. Execute the SQL")
        
        print("\n✅ Schema ready for application!")
        
    except Exception as e:
        print(f"❌ Error applying schema: {e}")

if __name__ == "__main__":
    asyncio.run(apply_schema())
