import os
import sys
import traceback

BASE_DIR = '/home/lwrxjoqoie/api.shapesa.com'

# Weka BASE_DIR mwanzo kabisa mwa sys.path
if BASE_DIR not in sys.path:
    sys.path.insert(0, BASE_DIR)

# MUHIMU: Folda ya 'apps' inaongezwa mwishoni (append) ili isizibe folda kuu ya 'core'
apps_dir = os.path.join(BASE_DIR, 'apps')
if os.path.isdir(apps_dir) and apps_dir not in sys.path:
    sys.path.append(apps_dir)

# Elekeza Django settings
os.environ['DJANGO_SETTINGS_MODULE'] = 'core.settings'

try:
    from django.core.wsgi import get_wsgi_application
    application = get_wsgi_application()
except Exception:
    err = traceback.format_exc()
    debug_html = f"""<!DOCTYPE html>
<html>
<head><title>Django Startup Diagnostic</title></head>
<body style="font-family: monospace; padding: 20px; background: #fff; color: #222;">
    <h2 style="color: #c00;">Django Startup Diagnostic Report</h2>
    <h3>Error Traceback:</h3>
    <pre style="background: #f4f4f4; padding: 15px; border-left: 4px solid #c00; overflow-x: auto;">{err}</pre>
</body>
</html>"""
    def application(environ, start_response):
        start_response('500 Internal Server Error', [('Content-Type', 'text/html; charset=utf-8')])
        return [debug_html.encode('utf-8')]
