"""Sync basic/ and pro/ packages with latest source files."""
import os
import shutil
from pathlib import Path

ROOT = Path(__file__).resolve().parent.parent
BASIC = ROOT / "basic"
PRO = ROOT / "pro"

def ensure_dir(path):
    path.parent.mkdir(parents=True, exist_ok=True)

def copy_file(src, dst):
    ensure_dir(dst)
    shutil.copy2(str(src), str(dst))
    print(f"  COPY {src.relative_to(ROOT)} -> {dst.relative_to(ROOT)}")

def remove_file(path):
    if path.exists():
        path.unlink()
        print(f"  DEL  {path.relative_to(ROOT)}")

def remove_dir(path):
    if path.exists():
        shutil.rmtree(str(path))
        print(f"  RMDIR {path.relative_to(ROOT)}")

print("=" * 60)
print("  BizNode Package Sync")
print("=" * 60)

# --- Step 1: Remove admin-only files from basic/ ---
print("\n[1] Removing admin-only files from basic/...")
admin_only_basic = [
    BASIC / "services" / "node_automonitor.py",
    BASIC / "services" / "node_orchestrator.py",
    BASIC / "services" / "reputation_service.py",
    BASIC / "services" / "agent_registry_service.py",
    BASIC / "services" / "support_email_service.py",
    BASIC / "services" / "hosted_bot_runner.py",
    BASIC / "scripts" / "telegram_bridge.py",
    BASIC / "scripts" / "deploy_node.py",
    BASIC / "scripts" / "setup_node.py",
    BASIC / "scripts" / "node_bootstrap.py",
]
for f in admin_only_basic:
    remove_file(f)

# --- Step 2: Remove admin-only files from pro/ ---
print("\n[2] Removing admin-only files/dirs from pro/...")
admin_only_pro_dirs = [
    PRO / "tools" / "admin",
    PRO / "videoautomation",
    PRO / "agent",
    PRO / "voiceover",
    PRO / "support",
]
for d in admin_only_pro_dirs:
    remove_dir(d)

admin_only_pro_files = [
    PRO / "generate_voiceover.py",
    PRO / "render_video.py",
    PRO / "services" / "node_automonitor.py",
    PRO / "services" / "node_orchestrator.py",
    PRO / "services" / "reputation_service.py",
    PRO / "services" / "agent_registry_service.py",
    PRO / "services" / "support_email_service.py",
    PRO / "services" / "hosted_bot_runner.py",
    PRO / "scripts" / "telegram_bridge.py",
    PRO / "scripts" / "deploy_node.py",
    PRO / "scripts" / "setup_node.py",
    PRO / "scripts" / "node_bootstrap.py",
]
for f in admin_only_pro_files:
    remove_file(f)

# --- Step 3: Sync files to BOTH basic/ and pro/ ---
print("\n[3] Syncing shared files to both basic/ and pro/...")
both_sync = [
    ("ui/server.py", "ui/server.py"),
    ("services/tier_manager.py", "services/tier_manager.py"),
    ("memory/database.py", "memory/database.py"),
    ("ui/static/support.html", "ui/static/support.html"),
]
for src_rel, dst_rel in both_sync:
    src = ROOT / src_rel
    if src.exists():
        copy_file(src, BASIC / dst_rel)
        copy_file(src, PRO / dst_rel)
    else:
        print(f"  SKIP {src_rel} (not found in root)")

# --- Step 4: Sync run_bot.py ---
print("\n[4] Syncing docker/bot/run_bot.py...")
bot_src = ROOT / "docker" / "bot" / "run_bot.py"
if bot_src.exists():
    copy_file(bot_src, BASIC / "docker" / "bot" / "run_bot.py")
    copy_file(bot_src, PRO / "docker" / "bot" / "run_bot.py")

# --- Step 5: Ensure setup_wizard.py and support_helper.py in both ---
print("\n[5] Ensuring setup_wizard.py and support_helper.py in both packages...")
for fname in ["setup_wizard.py", "support_helper.py"]:
    # These files live in basic/ as source of truth
    src = BASIC / fname
    dst = PRO / fname
    if src.exists():
        copy_file(src, dst)
    else:
        print(f"  SKIP {fname} (not found in basic/)")

# --- Step 6: Clean __pycache__ ---
print("\n[6] Cleaning __pycache__ directories...")
for pkg in [BASIC, PRO]:
    for cache_dir in pkg.rglob("__pycache__"):
        remove_dir(cache_dir)

print("\n" + "=" * 60)
print("  Sync complete!")
print("=" * 60)
