
import asyncio
import os
import sys
import logging

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

from app.core.database import init_db, execute_query, execute_update
from app.services.ollama_service import ollama_service

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

async def main(conv_id):
    init_db()
    
    # Get transcript
    rows = execute_query("SELECT transcript FROM conversations WHERE id = %s", (conv_id,))
    if not rows or not rows[0]['transcript']:
        print("No transcript found")
        return

    transcript = rows[0]['transcript']
    print(f"Transcript length: {len(transcript)}")
    
    # Override endpoint for Docker Mac access
    ollama_service.endpoint = os.environ.get("OLLAMA_ENDPOINT", "http://host.docker.internal:11434")
    print(f"Using Ollama endpoint: {ollama_service.endpoint}")
    
    summary = await ollama_service.generate_summary(transcript)
    print(f"Summary generated: {summary}")
    
    execute_update("UPDATE conversations SET summary = %s WHERE id = %s", (summary, conv_id))
    print("Database updated")

if __name__ == "__main__":
    asyncio.run(main(1))
