import os
import mysql.connector
from dotenv import load_dotenv  # Import load_dotenv
from datetime import datetime, timedelta, timezone

# Current UTC time
now_utc = datetime.now(timezone.utc)

# Subtract 10 minutes
expiry_time = now_utc - timedelta(minutes=10)

print("10 minutes ago (UTC):", expiry_time)

# Load environment variables from .env file
load_dotenv()

# --- Configuration ---
# Variables are now loaded from the .env file using os.getenv()
DB_HOST = os.getenv("DB_HOST")
DB_USER = os.getenv("DB_USER")
DB_PASSWORD = os.getenv("DB_PASSWORD")
DB_NAME_PROD = os.getenv("DB_NAME_PROD")
DB_NAME_STAGE = os.getenv("DB_NAME_STAGE")
# DB_NAME_DEV = os.getenv("DB_NAME_DEV")

def start_remove(db_name):
    # Connect to MySQL
    connection = mysql.connector.connect(
        host=DB_HOST,
        user=DB_USER,
        password=DB_PASSWORD,
        database=db_name,
    )
    cursor = connection.cursor()


    # SQL query to delete a row
    delete_query = "DELETE FROM OTPDetails WHERE IsUsed = true || TimeStamp < %s"
    # Execute the query
    cursor.execute(delete_query, (expiry_time,))

    # Commit the changes
    connection.commit()

    print(f"{cursor.rowcount} row(s) deleted.")

    # Close connection
    cursor.close()


if __name__ == "__main__":
    databases_to_remove = [DB_NAME_PROD, DB_NAME_STAGE]
    for db_name in databases_to_remove:
        start_remove(db_name)
    # start_remove(DB_NAME_DEV)
