import sys
import os

SCRIPTS_DIR = os.path.dirname(os.path.abspath(__file__))
ROOT_DIR = os.path.dirname(SCRIPTS_DIR)

if ROOT_DIR not in sys.path:
    sys.path.insert(0, ROOT_DIR)

from pinscrape.database import get_all_db_files, get_db_connection


def delete_ai_content():
    db_files = get_all_db_files()
    if not db_files:
        print("No database files found.")
        return

    for db_path in db_files:
        conn = get_db_connection(str(db_path))
        cursor = conn.execute("UPDATE posts SET ai_content = NULL, ai_title = NULL, updated_at = datetime('now')")
        affected = cursor.rowcount
        conn.commit()
        conn.close()
        print(f"Cleared {affected} row(s) in {db_path.name}")


if __name__ == "__main__":
    confirm = input("This will clear all AI Title and AI Content from all databases. Continue? (y/N): ")
    if confirm.lower() == 'y':
        delete_ai_content()
        print("Done.")
    else:
        print("Cancelled.")
