# compile.py
import os
import subprocess
import sys

def compile_to_exe():
    """Compile the IPTV Manager to executable"""
    
    # Create PyInstaller spec file content
    spec_content = """
# -*- mode: python ; coding: utf-8 -*-

block_cipher = None

a = Analysis(
    ['main.py'],
    pathex=[],
    binaries=[],
    datas=[
        ('templates/*', 'templates'),
        ('static/*', 'static'),
        ('config.py', '.'),
        ('uploads', 'uploads'),
        ('database', 'database'),
        ('streams', 'streams'),
        ('logs', 'logs')
    ],
    hiddenimports=[],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    win_no_prefer_redirects=False,
    win_private_assemblies=False,
    cipher=block_cipher,
    noarchive=False,
)

pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)

exe = EXE(
    pyz,
    a.scripts,
    a.binaries,
    a.zipfiles,
    a.datas,
    [],
    name='IPTV_Manager',
    debug=False,
    bootloader_ignore_signals=False,
    strip=False,
    upx=True,
    upx_exclude=[],
    runtime_tmpdir=None,
    console=False,  # Set to True if you want to see console output
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
)
"""
    
    # Write spec file
    with open('iptv_manager.spec', 'w') as f:
        f.write(spec_content)
    
    # Run PyInstaller
    try:
        subprocess.run(['pyinstaller', 'iptv_manager.spec', '--onefile', '--noconfirm'], check=True)
        print("Compilation completed successfully!")
        print("Executable created in: dist/IPTV_Manager.exe")
    except subprocess.CalledProcessError as e:
        print(f"Compilation failed: {e}")
    except FileNotFoundError:
        print("PyInstaller not found. Please install it with: pip install pyinstaller")

if __name__ == '__main__':
    compile_to_exe()