#!/usr/bin/env python3
"""Update Google Drive URLs to direct download format"""

import pymysql
from config import Config
import sys

def main():
    try:
        # Connect to database using Config
        conn = pymysql.connect(
            host=Config.DB_HOST,
            user=Config.DB_USER,
            password=Config.DB_PASSWORD,
            database=Config.DB_NAME,
            charset='utf8mb4'
        )
        cursor = conn.cursor()
        
        # Get current URLs for BID 2222
        cursor.execute("SELECT callid, recording_url FROM `2222_raw_calls` WHERE status = 0 LIMIT 10")
        results = cursor.fetchall()
        
        print(f"Found {len(results)} calls to update")
        
        # Update URLs with proper Google Drive direct download format
        updates = [
            (1, "https://drive.google.com/uc?id=1EmdGtAK2Jf8VzBRrYKDl5GkYMuUWZaEN&export=download"),
            (2, "https://drive.google.com/uc?id=1JdNZim2gt3QXtoOriKk_H8V7S1XCtoa6&export=download"),
            (3, "https://drive.google.com/uc?id=1a7KDVcClfgV5nBZjr5DRDTi69hxTQOW7&export=download"),
            (4, "https://drive.google.com/uc?id=15M6qUB1woN8T2t_eA_v0PHKIo2Pgb8Zs&export=download"),
        ]
        
        for callid, url in updates:
            cursor.execute(
                "UPDATE `2222_raw_calls` SET recording_url = %s WHERE callid = %s",
                (url, callid)
            )
            print(f"✓ Updated callid={callid} with direct download URL")
        
        conn.commit()
        print(f"\n✓ SUCCESS! Updated {len(updates)} calls with proper Google Drive direct download URLs")
        
        # Verify the updates
        cursor.execute("SELECT callid, recording_url FROM `2222_raw_calls` WHERE status = 0")
        results = cursor.fetchall()
        for row in results:
            print(f"  callid={row[0]}: {row[1]}")
        
        cursor.close()
        conn.close()
        
    except Exception as e:
        print(f"✗ Error: {e}")
        import traceback
        traceback.print_exc()
        sys.exit(1)

if __name__ == "__main__":
    main()
