#!/usr/bin/env python3
"""
Export all folders to zip files in output directory
"""

import os
import zipfile
import shutil
from datetime import datetime


def export_folders_to_zip(base_dir: str = ".", output_dir: str = "output"):
    """Export all subdirectories to individual zip files"""
    
    # Create output directory
    os.makedirs(output_dir, exist_ok=True)
    
    # Get all subdirectories
    items = os.listdir(base_dir)
    folders = [item for item in items if os.path.isdir(item) and not item.startswith('.') and item != output_dir]
    
    if not folders:
        print("No folders found to export.")
        return
    
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    exported_count = 0
    
    print(f"{'='*60}")
    print(f"Exporting folders to ZIP")
    print(f"{'='*60}")
    print(f"Base directory: {os.path.abspath(base_dir)}")
    print(f"Output directory: {os.path.abspath(output_dir)}")
    print(f"Folders to export: {len(folders)}")
    print(f"{'='*60}\n")
    
    for folder in folders:
        folder_path = os.path.join(base_dir, folder)
        zip_filename = f"{folder}.zip"
        zip_path = os.path.join(output_dir, zip_filename)
        
        try:
            print(f"  Creating: {zip_filename}")
            with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
                for root, dirs, files in os.walk(folder_path):
                    # Skip hidden directories
                    dirs[:] = [d for d in dirs if not d.startswith('.')]
                    for file in files:
                        if not file.startswith('.'):
                            file_path = os.path.join(root, file)
                            arcname = os.path.relpath(file_path, base_dir)
                            zipf.write(file_path, arcname)
            print(f"    [OK] {zip_filename}")
            exported_count += 1
        except Exception as e:
            print(f"    [FAIL] {folder}: {e}")
    
    print(f"\n{'='*60}")
    print(f"Done! Exported {exported_count}/{len(folders)} folders")
    print(f"Output directory: {os.path.abspath(output_dir)}")
    print(f"{'='*60}")


if __name__ == "__main__":
    import argparse
    
    parser = argparse.ArgumentParser(description="Export all folders to zip files")
    parser.add_argument("--base-dir", type=str, default=".", help="Base directory to scan")
    parser.add_argument("--output-dir", type=str, default="output", help="Output directory for zip files")
    
    args = parser.parse_args()
    export_folders_to_zip(args.base_dir, args.output_dir)
