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

import os
import sys
from PyInstaller.utils.hooks import collect_data_files, collect_submodules

block_cipher = None

# Obtener directorio base de manera segura
if hasattr(sys, '_MEIPASS'):
    base_dir = sys._MEIPASS
else:
    base_dir = os.path.abspath('.')

# Configuración principal de datos
datas = [
    # Base de datos
    (os.path.join(base_dir, 'db.sqlite3'), '.'),

    # Recursos de Django y dependencias
    *collect_data_files('django.contrib.admin', include_py_files=True),
    *collect_data_files('intendencia', include_py_files=True, includes=['**/templates/**/*']),
    *collect_data_files('xhtml2pdf'),
    *collect_data_files('reportlab'),

    # Templates manuales (backup)
    (os.path.join(base_dir, 'intendencia', 'templates'), 'intendencia/templates'),
]

# Configuración de análisis
a = Analysis(
    ['launcher.py'],
    pathex=[
        base_dir,
        os.path.join(base_dir, 'sistema'),
        os.path.join(base_dir, 'intendencia'),
    ],
    binaries=[],
    datas=datas,
    hiddenimports=[
        # Django
        'django.core.handlers.wsgi',
        'django.core.signals',
        'django.contrib.admin.apps',
        'django.contrib.auth.apps',
        'django.contrib.contenttypes.apps',
        'django.contrib.sessions.apps',
        'django.contrib.messages.apps',
        'django.contrib.staticfiles.apps',
        
        # App específica
        'intendencia.apps',
        'intendencia.views',
        'intendencia.urls',
        'intendencia.context_processors',
        
        # Dependencias
        'waitress',
        *collect_submodules('reportlab.graphics.barcode'),
        *collect_submodules('xhtml2pdf'),
        'reportlab.graphics.charts.legends',
        'reportlab.graphics.widgets',
        'reportlab.pdfbase._fontdata_enc_macexpert',
        'reportlab.pdfbase._fontdata_enc_macroman',
    ],
    hookspath=[],
    runtime_hooks=[],
    excludes=[
        'tkinter', 'unittest', 'pydoc', 'pygments',
        'psycopg2', 'numpy', 'pandas', 'scipy', 'matplotlib'
    ],
    cipher=block_cipher,
    noarchive=False,
)

# Configuración del PYZ
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)

# Configuración del EXE
exe = EXE(
    pyz,
    a.scripts,
    a.binaries,
    a.zipfiles,
    a.datas,
    name='Intendencia',
    debug=False,
    bootloader_ignore_signals=True,
    strip=False,
    upx=True,
    console=False,
    icon=os.path.join(base_dir, 'icono.ico') if os.path.exists(os.path.join(base_dir, 'icono.ico')) else None,
    disable_windowed_traceback=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
)