# nsx/templates.py


# nsx/templates.py

# अब यूजर की app.py सिर्फ तुम्हारे पैकेज से इंजन को कॉल करेगी
APP_PY_TEMPLATE = """from nsx.ns14engine import run_nsx_app

if __name__ == '__main__':
    run_nsx_app()
"""

APP_NSX_TEMPLATE = """def render():
    return f'''
    <style>
        body {{ background: #111122; color: white; font-family: sans-serif; text-align: center; padding-top: 20%; }}
    </style>
    <body>
        <h1>NSX Framework Load हो रहा है...</h1>
        <script>
            setTimeout(() => {{ window.pywebview.api.navigate('HomeScreen'); }}, 1500);
        </script>
    </body>
    '''
"""

# ... (बाकी HomeScreen और ProfileScreen के टेम्पलेट वही रहेंगे)

APP_PY_TEMPLATE = """import webview
import os
import sys

def get_resource_path(relative_path):
    if hasattr(sys, '_MEIPASS'):
        return os.path.join(sys._MEIPASS, relative_path)
    return os.path.join(os.path.abspath("."), relative_path)

class BackendAPI:
    def __init__(self):
        self.window = None

    def set_window(self, window):
        self.window = window

    def navigate(self, screen_name, params=None):
        print(f"📡 Navigating to {screen_name} with params: {params}")
        html = compile_nsx(f"src/{{screen_name}}.nsx", params)
        self.window.load_html(html)

def compile_nsx(file_path, props=None):
    full_path = get_resource_path(file_path)
    if not os.path.exists(full_path):
        return f"<h1>404: {{file_path}} Not Found</h1>"

    with open(full_path, "r", encoding="utf-8") as f:
        nsx_code = f.read()
    
    env = {{"props": props}} if props else {{"props": {{}}}}
    
    try:
        exec(nsx_code, env)
        if "render" in env:
            return env["render"]()
    except Exception as e:
        return f"<h1>Rendering Error: {{e}}</h1>"
    
    return "<h1>Error: render() function missing!</h1>"

if __name__ == '__main__':
    api = BackendAPI()
    initial_html = compile_nsx('src/App.nsx')
    window = webview.create_window('NSX Professional App', html=initial_html, js_api=api, width=900, height=700, background_color='#111122')
    api.set_window(window)
    webview.start()
"""

APP_NSX_TEMPLATE = """def render():
    return f'''
    <style>
        body {{ background: #111122; color: white; font-family: sans-serif; text-align: center; padding-top: 20%; }}
    </style>
    <body>
        <h1>NSX Framework Load हो रहा है...</h1>
        <script>
            setTimeout(() => {{ window.pywebview.api.navigate('HomeScreen'); }}, 1500);
        </script>
    </body>
    '''
"""

HOME_NSX_TEMPLATE = """def render():
    return f'''
    <body style="background:#111122; color:white; text-align:center; font-family:sans-serif; padding:50px;">
        <h1>🏠 Home Screen</h1>
        <p>अपना नाम लिखें:</p>
        <input type="text" id="username" style="padding:10px; border-radius:5px;">
        <button onclick="window.pywebview.api.navigate('HomeScreen', {{'user_name': document.getElementById('username').value}})" 
                style="padding:10px; background:#61dafb; border:none; border-radius:5px; cursor:pointer;">
            Profile पर जाएँ
        </button>
    </body>
    '''
"""

PROFILE_NSX_TEMPLATE = """def render():
    user = props.get("user_name", "Guest")
    return f'''
    <body style="background:#0a0a1a; color:white; text-align:center; font-family:sans-serif; padding:50px;">
        <h1>👤 User Profile</h1>
        <h2 style="color:#61dafb;">स्वागत है, {user}!</h2>
        <button onclick="window.pywebview.api.navigate('HomeScreen')" style="background:none; border:1px solid #61dafb; color:#61dafb; cursor:pointer;">वापस जाएँ</button>
    </body>
    '''
"""