#!/usr/bin/env python3
"""
Clean up S3 buckets - delete old data and keep only 2025 data
"""

import boto3
import json
from datetime import datetime
from storage.config import load_config_from_env
from storage.s3_adapter import S3Storage

def cleanup_s3_buckets():
    """Clean up S3 buckets, keeping only 2025 data"""
    try:
        cfg = load_config_from_env()
        s3_client = boto3.client('s3')
        
        print("🧹 Starting S3 cleanup...")
        print(f"📦 Target bucket: {cfg.s3_bucket_processed}")
        
        # List all objects in the bucket
        response = s3_client.list_objects_v2(Bucket=cfg.s3_bucket_processed)
        
        if 'Contents' not in response:
            print("✅ Bucket is already empty")
            return
        
        objects = response['Contents']
        print(f"📁 Found {len(objects)} objects in bucket")
        
        # Categorize objects by date
        old_objects = []
        current_objects = []
        
        current_date = datetime.now().strftime("%Y/%m/%d")
        current_year = datetime.now().strftime("%Y")
        
        for obj in objects:
            key = obj['Key']
            
            # Check if it's from 2025 (current year)
            if key.startswith(f"{current_year}/"):
                current_objects.append(key)
            else:
                old_objects.append(key)
        
        print(f"🗑️  Old objects to delete: {len(old_objects)}")
        print(f"✅ Current objects to keep: {len(current_objects)}")
        
        if old_objects:
            print("\n📋 Old objects to be deleted:")
            for obj in old_objects[:10]:  # Show first 10
                print(f"  - {obj}")
            if len(old_objects) > 10:
                print(f"  ... and {len(old_objects) - 10} more")
        
        # Delete old objects
        if old_objects:
            print(f"\n🗑️  Deleting {len(old_objects)} old objects...")
            
            # Delete in batches of 1000 (S3 limit)
            batch_size = 1000
            for i in range(0, len(old_objects), batch_size):
                batch = old_objects[i:i + batch_size]
                delete_objects = [{'Key': key} for key in batch]
                
                response = s3_client.delete_objects(
                    Bucket=cfg.s3_bucket_processed,
                    Delete={'Objects': delete_objects}
                )
                
                deleted_count = len(response.get('Deleted', []))
                print(f"  ✅ Deleted {deleted_count} objects (batch {i//batch_size + 1})")
        
        print(f"\n✅ Cleanup complete!")
        print(f"📊 Final stats:")
        print(f"  - Old objects deleted: {len(old_objects)}")
        print(f"  - Current objects kept: {len(current_objects)}")
        
        # Show remaining objects
        if current_objects:
            print(f"\n📁 Remaining objects:")
            for obj in current_objects[:5]:  # Show first 5
                print(f"  - {obj}")
            if len(current_objects) > 5:
                print(f"  ... and {len(current_objects) - 5} more")
        
    except Exception as e:
        print(f"❌ Error during cleanup: {e}")
        import traceback
        print(f"Full error: {traceback.format_exc()}")

if __name__ == "__main__":
    cleanup_s3_buckets()
