import os
import sys

# Pata njia halisi ya folda hii ilipo
CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))

# Sehemu zinazowezekana kuwa na 'core' na 'manage.py'
candidate_dirs = [
    CURRENT_DIR,
    os.path.join(CURRENT_DIR, 'backend'),
    '/home/lwrxjoqoie/api.shapesa.com',
    '/home/lwrxjoqoie/api.shapesa.com/backend',
]

project_root = None
for candidate in candidate_dirs:
    if os.path.isdir(os.path.join(candidate, 'core')) and os.path.isfile(os.path.join(candidate, 'core', 'settings.py')):
        project_root = candidate
        break

# Ikiwa bado haijaonekana, tafuta folda yoyote yenye core/settings.py
if not project_root:
    for root, dirs, files in os.walk(CURRENT_DIR):
        if 'settings.py' in files and os.path.basename(root) == 'core':
            project_root = os.path.dirname(root)
            break

if not project_root:
    project_root = CURRENT_DIR

# Hakikisha folda ya mradi na apps zipo mwanzo wa sys.path
if project_root not in sys.path:
    sys.path.insert(0, project_root)

apps_dir = os.path.join(project_root, 'apps')
if os.path.isdir(apps_dir) and apps_dir not in sys.path:
    sys.path.insert(0, apps_dir)

# Elekeza Django settings
os.environ['DJANGO_SETTINGS_MODULE'] = 'core.settings'

# Anzisha WSGI Application
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()