from config.db import get_db_connection
from datetime import datetime
import calendar

conn = get_db_connection()
cursor = conn.cursor()

month = 6
year = 2026
total_days = calendar.monthrange(year, month)[1]

cursor.execute("SELECT DISTINCT uid FROM power_monitoring")
devices = [row['uid'] for row in cursor.fetchall()]
print(f"Devices found: {devices}")

for uid in devices:
    cursor.execute("""
        SELECT DATE(timestamp) AS date, (MAX(energy) - MIN(energy)) AS daily_energy
        FROM power_monitoring
        WHERE uid = %s AND MONTH(timestamp) = %s AND YEAR(timestamp) = %s
        GROUP BY DATE(timestamp)
    """, (uid, month, year))
    energy_rows = cursor.fetchall()
    
    device_total_energy = sum(row['daily_energy'] for row in energy_rows if row['daily_energy'])
    device_active_days = len(energy_rows)
    print(f"Device: {uid}, Active Days: {device_active_days}, Total Energy: {device_total_energy}")
    print(f"Energy rows details: {energy_rows}")

conn.close()
