# -*- mode: python -*-
import os, glob, importlib, shutil
from PyInstaller.utils.hooks import (
    collect_data_files,
    collect_dynamic_libs,
    collect_submodules,
)

block_cipher = None

# Get the spec file directory (project root)
SPEC_ROOT = os.path.dirname(os.path.abspath(SPEC))

# 1) ImGui assets (fonts, shaders, etc.)
datas = collect_data_files('imgui_bundle', include_py_files=False)

# 2) Add tools folder and icon files (bundled inside _internal)
datas += [
    (os.path.join(SPEC_ROOT, 'tools'), 'tools'),                           # Copy entire tools folder
]

# Add all bynd-*.ico files
for ico in glob.glob(os.path.join(SPEC_ROOT, 'bynd-*.ico')):
    datas.append((ico, '.'))

# 2) Native libs for hidapi + the ImGui C‐extension
binaries = []
# • hidapi → also picks up hidapi.dll if that wrapper is used
binaries += collect_dynamic_libs('hidapi')
# • imgui_bundle → picks up _imgui_bundle*.pyd
binaries += collect_dynamic_libs('imgui_bundle')

# As a safety-net, glob any .pyd for imgui_bundle
try:
    pkg = importlib.import_module('imgui_bundle')
    pkg_dir = os.path.dirname(pkg.__file__)
    for pyd in glob.glob(os.path.join(pkg_dir, '_imgui_bundle*.pyd')):
        binaries.append((pyd, '.'))
except ImportError:
    pass

# 3) Pull in all Python modules for dependencies
hiddenimports = []
hiddenimports += collect_submodules('hidapi')
hiddenimports += collect_submodules('imgui_bundle')
hiddenimports += collect_submodules('plyer')
hiddenimports += collect_submodules('steamvr')
hiddenimports += collect_submodules('pydantic')
hiddenimports += collect_submodules('pydantic_settings')
hiddenimports += collect_submodules('yaml')
hiddenimports += collect_submodules('hid')

# Add specific plyer facades for Windows notifications
hiddenimports += ['plyer.platforms.win.notification']

a = Analysis(
    ['quick_updater.py'],
    pathex=['.'],
    binaries=binaries,
    datas=datas,
    hiddenimports=hiddenimports,
    hookspath=[],
    runtime_hooks=[],
    excludes=[],
    win_no_prefer_redirects=False,
    win_private_assemblies=False,
    cipher=block_cipher,
)

pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)

exe = EXE(
    pyz,
    a.scripts,
    [],                  # no in-EXE binaries
    exclude_binaries=True,
    name='quick_updater',
    debug=False,
    strip=False,
    upx=True,
    console=True,        # False → GUI only
)

coll = COLLECT(
    exe,
    a.binaries,          # contains hidapi.dll, other wrappers, _imgui_bundle.pyd
    a.zipfiles,
    a.datas,             # imgui_bundle/assets
    strip=False,
    upx=True,
    name='quick_updater',
)

# Post-build: Copy config file to output directory (next to exe, not in _internal)
import atexit
def copy_config_to_dist():
    dist_dir = os.path.join(SPEC_ROOT, 'dist', 'quick_updater')
    src_config = os.path.join(SPEC_ROOT, 'quick_updater_config.yaml')
    dst_config = os.path.join(dist_dir, 'quick_updater_config.yaml')
    if os.path.exists(dist_dir) and os.path.exists(src_config):
        shutil.copy2(src_config, dst_config)
        print(f"Copied config file to {dst_config}")

atexit.register(copy_config_to_dist)
