#!/usr/bin/env python3
import sys
import os

def parse_syk(filename):
    ui = {"title": "Sykus App", "label": "Hello", "button": "Click", "raw_web": ""}
    try:
        with open(filename, 'r') as f:
            content = f.read()
            for line in content.split('\n'):
                if "=" in line and 'raw_web' not in line:
                    key, val = line.split("=", 1)
                    ui[key.strip()] = val.strip().strip('"\'')
            if 'raw_web = """' in content:
                ui['raw_web'] = content.split('raw_web = """')[1].split('"""')[0]
    except: pass
    return ui

def build_web(filename):
    ui = parse_syk(filename)
    print(f"🌐 [Sykus Web] Building {filename}...")
    html = f"<html><head><title>{ui['title']}</title></head><body style='background:#121212;color:white;text-align:center;padding:50px;'><h1>{ui['title']}</h1><p>{ui['label']}</p><button style='background:#ff0055;color:white;padding:10px;'>{ui['button']}</button><div>{ui['raw_web']}</div></body></html>"
    with open("index.html", "w") as f: f.write(html)
    print("✅ Web Build Success: index.html")

def build_apk(filename):
    ui = parse_syk(filename)
    print(f"📱 [Sykus APK] Source Ready for {filename}...")
    kivy = f"from kivy.app import App\nfrom kivy.uix.label import Label\nclass SykusApp(App):\n    def build(self): return Label(text='{ui['label']}')\nif __name__ == '__main__': SykusApp().run()"
    with open("sykus_app.py", "w") as f: f.write(kivy)
    print("✅ APK Source Ready: sykus_app.py")

def main():
    # 🌟 NEW: Default Interactive Mode (REPL)
    if len(sys.argv) == 1:
        print("Sykus v5.1.3 (The Interactive Engine) 🔥")
        print("Type 'exit()' to quit.")
        while True:
            try:
                code = input("syk <<< ")
                if code.lower() in ["exit()", "quit"]: break
                if code.strip(): 
                    # Simple Eval for testing
                    try: print(eval(code))
                    except: exec(code)
            except (EOFError, KeyboardInterrupt): break
        return

    # Builder Mode
    if len(sys.argv) >= 4 and sys.argv[1] == "build":
        target, filename = sys.argv[2], sys.argv[3]
        if target == "web": build_web(filename)
        elif target == "apk": build_apk(filename)
    else:
        print("Usage:\n  syk (for Interactive Mode)\n  syk build [web|apk] [file.syk]")

if __name__ == "__main__":
    main()
