'''
  ____   ___  _     _ _       
 / ___| / _ \| |   (_) |_ ___ 
 \___ \| | | | |   | | __/ _ \
  ___) | |_| | |___| | ||  __/
 |____/ \__\_\_____|_|\__\___|
                              
Script to flush (delete) data from a sqlite database

/usr/bin/python3 /var/www/moduleair_pro_4g/sqlite/flush_old_data.py

Available table are

data_NPM
data_NPM_5channels
data_BME280
data_envea
timestamp_table
data_CO2

'''

import sqlite3
import datetime


# Connect to the SQLite database
conn = sqlite3.connect("/var/www/moduleair_pro_4g/sqlite/sensors.db")
cursor = conn.cursor()

#GET RTC TIME from SQlite
cursor.execute("SELECT * FROM timestamp_table LIMIT 1")
row = cursor.fetchone()  # Get the first (and only) row

if row:
    rtc_time_str = row[1]  # Assuming timestamp is stored as TEXT (YYYY-MM-DD HH:MM:SS)
    print(f"[INFO] Last recorded timestamp: {rtc_time_str}")

    # Convert last_updated to a datetime object
    last_updated = datetime.datetime.strptime(rtc_time_str, "%Y-%m-%d %H:%M:%S")

    # Calculate the cutoff date (3 months before last_updated)
    cutoff_date = last_updated - datetime.timedelta(days=60)
    cutoff_date_str = cutoff_date.strftime("%Y-%m-%d %H:%M:%S")

    print(f"[INFO] Deleting records older than: {cutoff_date_str}")

    # List of tables to delete old data from
    tables_to_clean = ["data_NPM", "data_NPM_5channels", "data_BME280", "data_envea", "data_CO2"]

    # Loop through each table and delete old data
    for table in tables_to_clean:
        delete_query = f"DELETE FROM {table} WHERE timestamp < ?"
        cursor.execute(delete_query, (cutoff_date_str,))
        print(f"[INFO] Deleted old records from {table}")

    # **Commit changes before running VACUUM**
    conn.commit()
    print("[INFO] Changes committed successfully!")

    # Now it's safe to run VACUUM
    print("[INFO] Running VACUUM to optimize database space...")
    cursor.execute("VACUUM")

    print("[SUCCESS] Old data flushed successfully!")

else:
    print("[ERROR] No timestamp found in timestamp_table.")


# Close the database connection
conn.close()
