import sqlite3
import pandas as pd
import os

def export_email_list_to_csv(db_path: str, output_path: str = "email_list_export.csv"):
    if not os.path.exists(db_path):
        raise FileNotFoundError(f"Database not found: {db_path}")

    conn = sqlite3.connect(db_path)
    conn.row_factory = sqlite3.Row
    cursor = conn.cursor()

    try:
        cursor.execute("SELECT * FROM email_list")
        rows = cursor.fetchall()
        data = [dict(row) for row in rows]
        if not data:
            print("No data found in email_list table.")
            return

        df = pd.DataFrame(data)
        df.to_csv(output_path, index=False)
        print(f"✅ Exported {len(df)} rows to {output_path}")

    finally:
        conn.close()

if __name__ == "__main__":
    export_email_list_to_csv("/var/www/bennysblog/instance/blog.db")

