#!/usr/bin/env python3
"""
ComfyUI-MotionCapture prestartup script.

Automatically copies example assets and workflows to ComfyUI directories on startup.
Runs before ComfyUI's main initialization.
"""

import os
import shutil
from pathlib import Path


def copy_assets():
    """Copy all files from assets/ to ComfyUI/input/"""
    try:
        # Determine paths
        custom_node_dir = Path(__file__).parent
        comfyui_dir = custom_node_dir.parent.parent
        input_dir = comfyui_dir / "input"
        assets_src = custom_node_dir / "assets"

        # Check if assets directory exists
        if not assets_src.exists():
            print("[MotionCapture] No assets/ directory found, skipping asset copy")
            return

        # Create input directory if it doesn't exist
        input_dir.mkdir(parents=True, exist_ok=True)

        # Copy all files from assets/
        copied_count = 0
        skipped_count = 0

        for item in assets_src.iterdir():
            # Skip hidden files and directories (like .ipynb_checkpoints)
            if item.name.startswith('.'):
                continue

            # Skip directories (only copy files)
            if item.is_dir():
                continue

            # Destination path
            dest = input_dir / item.name

            # Skip if file already exists
            if dest.exists():
                skipped_count += 1
                continue

            # Copy file
            try:
                shutil.copy2(item, dest)
                print(f"[MotionCapture] Copied asset: {item.name} -> {dest}")
                copied_count += 1
            except Exception as e:
                print(f"[MotionCapture] Failed to copy {item.name}: {e}")

        # Print summary
        if copied_count > 0:
            print(f"[MotionCapture] Copied {copied_count} asset file(s) to {input_dir}")
        if skipped_count > 0:
            print(f"[MotionCapture] Skipped {skipped_count} existing asset file(s)")

    except Exception as e:
        print(f"[MotionCapture] Error copying assets: {e}")

# Run on import
if __name__ == "__main__":
    print("[MotionCapture] Running prestartup script...")
    copy_assets()
    print("[MotionCapture] Prestartup script completed")
else:
    # Also run when imported by ComfyUI
    print("[MotionCapture] Running prestartup script...")
    copy_assets()
    print("[MotionCapture] Prestartup script completed")