import sys
import os

def build_web(filename):
    print(f"🌐 [Sykus Web Engine] Compiling '{filename}' to Web...")
    html_content = f"<!DOCTYPE html>\n<html>\n<head><title>Sykus Web App</title></head>\n<body style=\"font-family: Arial; text-align: center; margin-top: 50px;\">\n    <h1>Built with Sykus v5.1.1</h1>\n    <p>Your code from {filename} is running on the web!</p>\n</body>\n</html>"
    with open("index.html", "w") as f:
        f.write(html_content)
    print("✅ Web build complete! File saved as 'index.html'.")

def build_apk(filename):
    print(f"📱 [Sykus App Engine] Compiling '{filename}' to Android APK...")
    kivy_code = f"from kivy.app import App\nfrom kivy.uix.label import Label\nclass SykusApp(App):\n    def build(self):\n        return Label(text='Sykus App: {filename}')\nif __name__ == '__main__':\n    SykusApp().run()"
    with open("sykus_app.py", "w") as f:
        f.write(kivy_code)
    print("✅ Android Native Code generated: 'sykus_app.py'.")
    print("⚠️ Use 'buildozer' to generate the final .apk later.")

def main():
    if len(sys.argv) == 1:
        print("Sykus v5.1.1 (Web & APK Builder Active) 🔥")
        print("syk>>> ", end="")
        try: input() 
        except: pass
        return

    command = sys.argv[1]

    if command in ["--version", "-v"]:
        print("Sykus Version 5.1.1")
    elif command == "build":
        if len(sys.argv) < 4:
            print("❌ Error: Use 'syk build web app.syk' or 'syk build apk app.syk'")
            sys.exit(1)
        target = sys.argv[2]
        filename = sys.argv[3]
        
        if not os.path.exists(filename):
            with open(filename, "w") as f: f.write("print('Hello Sykus')")
            print(f"⚠️ Created dummy file '{filename}' for testing.")
            
        if target == "web": build_web(filename)
        elif target == "apk": build_apk(filename)
        else: print("❌ Unknown target. Use 'web' or 'apk'.")
    else:
        print(f"▶️ Running {command}...")

if __name__ == "__main__":
    main()
