# scripts/clean_logs.py
import os
import time
from datetime import datetime, timedelta

# Define the log directory and the age limit (in days)
LOG_DIR = "logs"
DAYS_TO_KEEP = 5

def clean_old_logs():
    """
    Deletes log files older than a specified number of days.
    """
    now = datetime.now()
    cutoff = now - timedelta(days=DAYS_TO_KEEP)
    
    if not os.path.exists(LOG_DIR):
        print("Log directory does not exist. Exiting.")
        return

    for filename in os.listdir(LOG_DIR):
        file_path = os.path.join(LOG_DIR, filename)
        
        # Ensure it's a file and not a directory
        if os.path.isfile(file_path):
            # Get the file's modification time
            mod_time = datetime.fromtimestamp(os.path.getmtime(file_path))
            
            if mod_time < cutoff:
                print(f"Deleting old log file: {filename}")
                os.remove(file_path)

if __name__ == "__main__":
    clean_old_logs()