#!/usr/bin/env python3
"""Reset calls with status -1 (invalid URLs) to status 0 (ready for transcription)"""

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()
        
        # Check how many calls with status -1
        cursor.execute("SELECT COUNT(*) as count FROM `2222_raw_calls` WHERE status = -1")
        result = cursor.fetchone()
        
        if result:
            count_invalid = result[0]
            print(f"✓ Found {count_invalid} calls with status -1 (invalid URLs)")
            
            if count_invalid > 0:
                # Reset them to status 0 (ready for transcription)
                cursor.execute("UPDATE `2222_raw_calls` SET status = 0 WHERE status = -1")
                conn.commit()
                print(f"✓ Reset {count_invalid} calls to status 0")
                
                # Verify the reset
                cursor.execute("SELECT COUNT(*) as count FROM `2222_raw_calls` WHERE status = 0")
                result = cursor.fetchone()
                if result:
                    count_ready = result[0]
                    print(f"✓ SUCCESS! Now {count_ready} calls are ready for transcription (status = 0)")
            else:
                print("✓ No calls to reset.")
        
        cursor.close()
        conn.close()
        
    except Exception as e:
        print(f"✗ Error: {e}")
        import traceback
        traceback.print_exc()
        sys.exit(1)

if __name__ == "__main__":
    main()
