"""
Clean Data Files

Truncates CSV files to start from a specific date.
Useful for aligning data start times between TradingView exports and Python data
to ensure indicator warmup periods match.

Usage:
    python tools/clean_data.py --date 2017-10-01
"""
import pandas as pd
import os
import glob
import argparse
import sys

def clean_csv_files(start_date):
    # Define directories to scan
    target_dirs = ['.', 'data', 'strategies']
    # Ensure cutoff is timezone naive for comparison
    cutoff_ts = pd.to_datetime(start_date).tz_localize(None)
    
    print(f"Scanning for CSV files to truncate before {start_date}...")
    
    files_processed = 0
    
    for d in target_dirs:
        if not os.path.isdir(d):
            continue
            
        # Find all csv files
        csv_files = glob.glob(os.path.join(d, "*.csv"))
        
        for f in csv_files:
            try:
                # Read CSV
                df = pd.read_csv(f)
                
                # Identify date column
                date_col = None
                # Common names for date columns
                candidates = ['time', 'date', 'timestamp', 'datetime']
                
                for col in df.columns:
                    if col.lower() in candidates or 'time' in col.lower():
                        date_col = col
                        break
                
                if not date_col:
                    print(f"[SKIP] {f} (No date column found)")
                    continue
                
                # Parse dates
                # We use coerce to handle potential parsing errors
                dates = pd.to_datetime(df[date_col], errors='coerce')
                
                # Handle timezone naivety for comparison
                if dates.dt.tz is not None:
                    dates = dates.dt.tz_convert(None)
                
                # Check if any data exists before cutoff
                if dates.min() < cutoff_ts:
                    # Filter
                    mask = dates >= cutoff_ts
                    df_clean = df[mask]
                    
                    rows_removed = len(df) - len(df_clean)
                    
                    if rows_removed > 0:
                        print(f"[CLEAN] {f}: Removing {rows_removed} rows. New start: {df_clean[date_col].iloc[0]}")
                        df_clean.to_csv(f, index=False)
                        files_processed += 1
                    else:
                        print(f"[OK] {f}: All data is already after {start_date}")
                else:
                    print(f"[OK] {f}: Starts after {start_date}")
                    
            except Exception as e:
                print(f"[ERROR] {f}: {e}")
                
    print(f"\nDone. Processed {files_processed} files.")

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Clean CSV data before a specific date.")
    parser.add_argument("--date", type=str, default="2017-10-01", help="Cutoff date (YYYY-MM-DD)")
    args = parser.parse_args()
    
    clean_csv_files(args.date)
