import asyncio
import os
import sys

# Ensure backend dir is in path
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")))

from common.sqlite_client import sqlite_client, init_sqlite, TABLE_INBOX

async def run_migration():
    print("Running migration for missing columns in inbox...")
    
    await init_sqlite()
    
    db = sqlite_client.db
    cursor = await db.execute(f"PRAGMA table_info({TABLE_INBOX})")
    rows = await cursor.fetchall()
    cols = [r[1] for r in rows]
    
    if "message_type" not in cols:
        print(f"Adding 'message_type' column to {TABLE_INBOX}...")
        await db.execute(f"ALTER TABLE {TABLE_INBOX} ADD COLUMN message_type TEXT")
        await db.commit()
        print("Column 'message_type' added successfully.")
    else:
        print(f"Column 'message_type' already exists in {TABLE_INBOX}.")
        
    await sqlite_client.close()
    print("Migration completed.")

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