#!/usr/bin/env python3
"""
Subprocess wrapper for ImgInn API-based operations.
Uses the API-based module instead of DOM scraping.

Supports both single content_type (backward compatible) and multi content_types mode.
"""

import sys
import json
from pathlib import Path

# Add parent directory to path
sys.path.insert(0, str(Path(__file__).parent.parent))

from wrappers.base_subprocess_wrapper import (
    create_download_wrapper, setup_signal_handlers, set_database_reference,
    stderr_log
)


def create_imginn_api_downloader(config, unified_db):
    """Factory function to create an ImgInn API downloader instance."""
    from modules.imginn_api_module import ImgInnAPIDownloader

    return ImgInnAPIDownloader(
        headless=config.get('headless', True),
        show_progress=True,
        use_database=True,
        log_callback=None,
        unified_db=unified_db
    )


# Create the single-content download function using the factory
run_imginn_api_download = create_download_wrapper("ImgInnAPI", "imginn_api", create_imginn_api_downloader)


def run_imginn_api_download_multi(config: dict) -> dict:
    """Run multi-content-type download by calling download() for each content type."""
    from modules.unified_database import UnifiedDatabase
    from modules.monitor_wrapper import log_download_result

    setup_signal_handlers("ImgInnAPI")

    required_keys = ['username', 'content_types', 'output_dirs']
    missing_keys = [key for key in required_keys if key not in config]
    if missing_keys:
        return {'status': 'error', 'message': f'Missing required config keys: {missing_keys}', 'results': {}}

    db_path = config.get('db_path', '/opt/media-downloader/database/media_downloader.db')
    unified_db = UnifiedDatabase(db_path, use_pool=False)
    set_database_reference(unified_db)

    try:
        downloader = create_imginn_api_downloader(config, unified_db)

        username = config['username']
        content_types = config['content_types']
        output_dirs = config['output_dirs']

        stderr_log(f"Processing Instagram multi ({', '.join(content_types)}) for @{username}", "info")

        # Create temp directories
        for ct, dir_path in output_dirs.items():
            Path(dir_path).mkdir(parents=True, exist_ok=True)

        results = {}
        total_count = 0

        for ct in content_types:
            output_dir = output_dirs.get(ct)
            if not output_dir:
                results[ct] = {'count': 0, 'pending_downloads': []}
                continue

            try:
                count = downloader.download(
                    username=username,
                    content_type=ct,
                    days_back=config.get('days_back', 3),
                    max_downloads=config.get('max_downloads', 50),
                    output_dir=output_dir,
                    phrase_config=config.get('phrase_config'),
                    defer_database=True
                )

                pending_downloads = downloader.get_pending_downloads()
                downloader.clear_pending_downloads()

                results[ct] = {
                    'count': count or 0,
                    'pending_downloads': pending_downloads
                }
                total_count += count or 0

            except Exception as e:
                stderr_log(f"ImgInn API download error for {ct}: {e}", "error")
                import traceback
                stderr_log(traceback.format_exc(), "error")
                results[ct] = {'count': 0, 'pending_downloads': []}

        log_download_result("imginn_api", username, total_count, error=None)

        return {
            'status': 'success',
            'results': results
        }

    except Exception as e:
        stderr_log(f"ImgInn API multi-download error: {e}", "error")
        import traceback
        stderr_log(traceback.format_exc(), "error")
        log_download_result("imginn_api", config.get('username', ''), 0, error=str(e))
        return {
            'status': 'error',
            'message': str(e),
            'results': {}
        }
    finally:
        if unified_db:
            try:
                unified_db.close()
            except Exception:
                pass


if __name__ == '__main__':
    config_json = sys.stdin.read()
    config = json.loads(config_json)

    if 'content_types' in config:
        result = run_imginn_api_download_multi(config)
    else:
        result = run_imginn_api_download(config)

    print(json.dumps(result))
    sys.exit(0 if result.get('status') == 'success' else 1)
