# -*- mode: python ; coding: utf-8 -*-

import sys
import os
from pathlib import Path

# This is a PyInstaller spec file. It is used to bundle the Python backend
# into a single executable that can be shipped with the Electron app.

# The project_root is the 'fourdst/' directory that contains 'electron/', 'fourdst/', etc.
# SPECPATH is a variable provided by PyInstaller that contains the absolute path
# to the directory containing the spec file.
project_root = Path(SPECPATH).parent

# We need to add the project root to the path so that PyInstaller can find the 'fourdst' module.
sys.path.insert(0, str(project_root))

# Platform-specific configurations
platform = sys.platform
print(f"Building for platform: {platform}")

# Determine executable name based on platform
exe_name = 'fourdst-backend'
if platform == 'win32':
    exe_name += '.exe'

# The main script to be bundled.
analysis = Analysis(['bridge.py'],
               pathex=[str(project_root)],
               binaries=[],
               # Add any modules that PyInstaller might not find automatically.
               hiddenimports=[
                   # Core dependencies
                   'docker',
                   'docker.api',
                   'docker.client',
                   'docker.errors',
                   'docker.models',
                   'docker.types',
                   'docker.utils',
                   # Cryptography dependencies
                   'cryptography',
                   'cryptography.hazmat',
                   'cryptography.hazmat.primitives',
                   'cryptography.hazmat.primitives.asymmetric',
                   'cryptography.hazmat.primitives.asymmetric.rsa',
                   'cryptography.hazmat.primitives.asymmetric.ed25519',
                   'cryptography.hazmat.primitives.asymmetric.padding',
                   'cryptography.hazmat.primitives.hashes',
                   'cryptography.hazmat.primitives.serialization',
                   'cryptography.exceptions',
                   # YAML dependencies
                   'yaml',
                   'yaml.loader',
                   'yaml.dumper',
                   # Core Python modules that might be missed
                   'pathlib',
                   'tempfile',
                   'zipfile',
                   'tarfile',
                   'hashlib',
                   'datetime',
                   'json',
                   'subprocess',
                   'shutil',
                   'logging',
                   # Platform-specific modules
                   'platform',
                   'os',
                   'sys',
                   # fourdst modules
                   'fourdst',
                   'fourdst.core',
                   'fourdst.core.bundle',
                   'fourdst.core.build',
                   'fourdst.core.config',
                   'fourdst.core.platform',
                   'fourdst.core.utils'
               ],
               hookspath=[],
               runtime_hooks=[],
               excludes=[
                   # Exclude unnecessary modules to reduce size
                   'tkinter',
                   'matplotlib',
                   'numpy',
                   'scipy',
                   'pandas',
                   'jupyter',
                   'IPython'
               ],
               win_no_prefer_redirects=False,
               win_private_assemblies=False,
               cipher=None,
               noarchive=False)

pyz = PYZ(analysis.pure, analysis.zipped_data,
             cipher=None)

exe = EXE(pyz, 
          analysis.scripts,
          [],
          exclude_binaries=True,
          name=exe_name,
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          console=True )

coll = COLLECT(exe,
               analysis.binaries,
               analysis.zipfiles,
               analysis.datas,
               strip=False,
               upx=True,
               upx_exclude=[],
               name='fourdst-backend')
