#!/usr/bin/env python3
"""
Cleanup script for expired files and pastes in Sharey
Supports both local storage and Backblaze B2 storage backends
"""
import os
import sys
import json
from datetime import datetime, timezone
from pathlib import Path

# Add src directory to Python path
src_path = Path(__file__).parent / "src"
sys.path.insert(0, str(src_path))

from config import config
from storage import StorageManager


def cleanup_local_storage(storage_manager):
    """Clean up expired files from local storage"""
    print("🧹 Cleaning up local storage...")
    
    backend = storage_manager.backend
    base_path = backend.base_path
    deleted_count = 0
    
    # Check files directory
    files_dir = base_path / "files"
    if files_dir.exists():
        for file_path in files_dir.glob("*"):
            if file_path.is_file() and not file_path.name.endswith('.meta'):
                meta_file = file_path.with_suffix(file_path.suffix + '.meta')
                
                if meta_file.exists():
                    try:
                        with open(meta_file, 'r') as f:
                            metadata = json.load(f)
                        
                        expires_at = metadata.get('expires_at')
                        if expires_at:
                            expiry_time = datetime.fromisoformat(expires_at.replace('Z', '+00:00'))
                            if datetime.now() > expiry_time:
                                # Delete both file and metadata
                                file_path.unlink()
                                meta_file.unlink()
                                print(f"🗑️ Deleted expired file: {file_path.name}")
                                deleted_count += 1
                    except Exception as e:
                        print(f"❌ Error processing {file_path.name}: {e}")
    
    # Check pastes directory
    pastes_dir = base_path / "pastes"
    if pastes_dir.exists():
        for file_path in pastes_dir.glob("*.txt"):
            if file_path.is_file():
                meta_file = file_path.with_suffix('.txt.meta')
                
                if meta_file.exists():
                    try:
                        with open(meta_file, 'r') as f:
                            metadata = json.load(f)
                        
                        expires_at = metadata.get('expires_at')
                        if expires_at:
                            expiry_time = datetime.fromisoformat(expires_at.replace('Z', '+00:00'))
                            if datetime.now() > expiry_time:
                                # Delete both file and metadata
                                file_path.unlink()
                                meta_file.unlink()
                                print(f"🗑️ Deleted expired paste: {file_path.name}")
                                deleted_count += 1
                    except Exception as e:
                        print(f"❌ Error processing {file_path.name}: {e}")
    
    return deleted_count


def cleanup_b2_storage(storage_manager):
    """Clean up expired files from Backblaze B2 storage"""
    print("🧹 Cleaning up B2 storage...")
    
    deleted_count = 0
    
    try:
        # List all files using the storage manager
        all_files = storage_manager.list_files("")
        
        for file_path in all_files:
            try:
                # Get metadata for this file
                metadata = storage_manager.get_metadata(file_path)
                if not metadata:
                    continue
                    
                expires_at = metadata.get('expires_at')
                if expires_at:
                    expiry_time = datetime.fromisoformat(expires_at.replace('Z', '+00:00'))
                    current_time = datetime.now(timezone.utc).replace(tzinfo=None)
                    
                    if current_time > expiry_time:
                        print(f"🗑️ Deleting expired B2 file: {file_path}")
                        if storage_manager.delete_file(file_path):
                            deleted_count += 1
                        else:
                            print(f"❌ Failed to delete B2 file: {file_path}")
                            
            except Exception as e:
                print(f"❌ Error processing B2 file {file_path}: {e}")
                
    except Exception as e:
        print(f"❌ Error listing B2 files: {e}")
        return 0
    
    return deleted_count


def main():
    """Main cleanup function"""
    print("🧹 Sharey Cleanup Script")
    print("=" * 40)
    print(f"⏰ Started at: {datetime.now().isoformat()}")
    
    try:
        # Initialize storage manager
        storage_manager = StorageManager(config)
        backend_info = storage_manager.get_backend_info()
        
        print(f"📁 Storage backend: {backend_info['type']}")
        
        # Run appropriate cleanup based on storage type
        if backend_info['type'] == 'local':
            deleted_count = cleanup_local_storage(storage_manager)
        elif backend_info['type'] == 'b2':
            deleted_count = cleanup_b2_storage(storage_manager)
        else:
            print(f"❌ Unknown storage backend: {backend_info['type']}")
            sys.exit(1)
        
        print("=" * 40)
        if deleted_count > 0:
            print(f"✅ Cleanup completed! Deleted {deleted_count} expired items.")
        else:
            print("✅ Cleanup completed! No expired items found.")
        
        print(f"⏰ Finished at: {datetime.now().isoformat()}")
        
    except Exception as e:
        print(f"❌ Cleanup failed: {e}")
        sys.exit(1)


if __name__ == "__main__":
    main()
