#!/usr/bin/env python3
"""
Dynamic script to auto-discover and process all financial statements
"""

import os
import subprocess
import sys

def main():
    """
    Main function to dynamically discover and process all financial statements
    """
    import argparse
    
    parser = argparse.ArgumentParser(description='Dynamically process all financial statements')
    parser.add_argument('--output-dir', default=None, 
                       help='Directory to save CSV output files')
    
    args = parser.parse_args()
    
    # Get paths
    script_dir = os.path.dirname(os.path.abspath(__file__))
    project_root = os.path.dirname(script_dir)
    data_dir = os.path.join(project_root, 'data/pdf')
    
    # Set output directory
    output_dir = args.output_dir or os.path.join(project_root, 'output/csv')
    os.makedirs(output_dir, exist_ok=True)
    
    print(f"\n{'='*60}")
    print(f"Dynamic Financial Statement Processor")
    print(f"Data Directory: {os.path.abspath(data_dir)}")
    print(f"Output Directory: {os.path.abspath(output_dir)}")
    
    # Build command
    cmd = [sys.executable, os.path.join(script_dir, 'dynamic_processor.py'), 
           '--data-dir', data_dir, '--output-dir', output_dir]
    
    # Run the dynamic processor
    try:
        result = subprocess.run(cmd, check=True, capture_output=True)
        print(f"\nDiscovery Results:")
        print(result.stdout)
        
        if result.returncode == 0:
            print(f"\n{'='*60}")
            print(f"Dynamic Processing Complete: CSV files saved to {os.path.abspath(output_dir)}")
        else:
            print(f"\nError during dynamic processing: exit code {result.returncode}")
    except subprocess.CalledProcessError as e:
        print(f"\nError running dynamic processor: {e}")

if __name__ == "__main__":
    from datetime import datetime
    
    # Add date to print
    print(f"Date: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
    print(f"{'='*60}")
    
    main()